Skip to main content

clickhouse_loom/
capabilities.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use crate::OpsCapabilitiesOutput;
4
5/// Operations and request fields an application needs from the server.
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
7#[non_exhaustive]
8pub struct ProbeRequest {
9    pub ops: Vec<String>,
10    pub fields: BTreeMap<String, Vec<String>>,
11}
12
13impl ProbeRequest {
14    pub fn new() -> Self {
15        Self::default()
16    }
17
18    pub fn ops(mut self, ops: impl IntoIterator<Item = impl Into<String>>) -> Self {
19        self.ops = ops.into_iter().map(Into::into).collect();
20        self
21    }
22
23    /// Set the required request fields for one operation.
24    pub fn fields(
25        mut self,
26        op: impl Into<String>,
27        fields: impl IntoIterator<Item = impl Into<String>>,
28    ) -> Self {
29        self.fields
30            .insert(op.into(), fields.into_iter().map(Into::into).collect());
31        self
32    }
33}
34
35/// A capability comparison. Missing capabilities do not produce an error.
36#[derive(Clone, Debug, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct ProbeReport {
39    /// True when all requested operations and request fields are served.
40    pub ok: bool,
41    pub build_commit: String,
42    pub schema_version: String,
43    /// Preserves the requested order and duplicates.
44    pub missing_ops: Vec<String>,
45    /// Includes every requested field when its operation is not served.
46    pub missing_fields: BTreeMap<String, Vec<String>>,
47    /// Server operations absent from this SDK's registry, sorted by name.
48    pub unknown_to_sdk: Vec<String>,
49}
50
51pub(crate) fn compare(request: ProbeRequest, capabilities: OpsCapabilitiesOutput) -> ProbeReport {
52    let served: BTreeMap<_, _> = capabilities
53        .ops
54        .iter()
55        .map(|op| (op.name.as_str(), op))
56        .collect();
57    let missing_ops: Vec<_> = request
58        .ops
59        .into_iter()
60        .filter(|op| !served.contains_key(op.as_str()))
61        .collect();
62    let missing_fields: BTreeMap<_, _> = request
63        .fields
64        .into_iter()
65        .filter_map(|(name, fields)| {
66            let missing: Vec<_> = fields
67                .into_iter()
68                .filter(|field| {
69                    !served
70                        .get(name.as_str())
71                        .is_some_and(|op| op.request_fields.contains(field))
72                })
73                .collect();
74            (!missing.is_empty()).then_some((name, missing))
75        })
76        .collect();
77    let known: BTreeSet<_> = crate::wire_models::OP_POLICIES
78        .iter()
79        .map(|policy| policy.0)
80        .collect();
81    let unknown_to_sdk = served
82        .keys()
83        .filter(|name| !known.contains(**name))
84        .map(|name| (*name).to_owned())
85        .collect();
86    ProbeReport {
87        ok: missing_ops.is_empty() && missing_fields.is_empty(),
88        build_commit: capabilities.build_commit,
89        schema_version: capabilities.schema_version,
90        missing_ops,
91        missing_fields,
92        unknown_to_sdk,
93    }
94}