Skip to main content

rullst_orm/polyglot/
capability.rs

1/// Persistence backends exposed by the polyglot boundary.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3#[non_exhaustive]
4pub enum Backend {
5    /// Deterministic, in-process document backend for tests and local work.
6    Mock,
7    /// MongoDB document database.
8    MongoDb,
9    /// DuckDB in-process analytics database.
10    DuckDb,
11    /// Turso/libSQL edge database through its remote protocol.
12    Turso,
13    /// SurrealDB multi-model database through its HTTP protocol.
14    SurrealDb,
15    /// Qdrant dense-vector database through its HTTP protocol.
16    Qdrant,
17    /// Redis native data structures through its RESP protocol.
18    Redis,
19}
20
21/// Portable capabilities intentionally supported by an adapter.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
23#[non_exhaustive]
24pub enum Capability {
25    /// Typed document CRUD and bounded listing.
26    Documents,
27    /// Parameterized analytical statements and bounded result sets.
28    Analytics,
29    /// Explicit, bounded graph queries.
30    Graph,
31    /// Parameterized SQL over an edge-native remote protocol.
32    EdgeSql,
33    /// Typed relational model CRUD and bounded queries.
34    RelationalModels,
35    /// Dense-vector collection lifecycle and bounded nearest-neighbor queries.
36    Vectors,
37    /// Explicit hash, set, and sorted-set operations.
38    KeyValueStructures,
39}
40
41/// A backend's honest capability declaration.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub struct BackendCapabilities {
44    backend: Backend,
45    capabilities: &'static [Capability],
46}
47
48impl BackendCapabilities {
49    /// Creates an immutable capability declaration.
50    pub const fn new(backend: Backend, capabilities: &'static [Capability]) -> Self {
51        Self {
52            backend,
53            capabilities,
54        }
55    }
56
57    /// Returns the backend described by this declaration.
58    pub const fn backend(self) -> Backend {
59        self.backend
60    }
61
62    /// Returns the explicitly supported capabilities.
63    pub const fn capabilities(self) -> &'static [Capability] {
64        self.capabilities
65    }
66
67    /// Tests whether a capability is declared.
68    pub fn supports(self, expected: Capability) -> bool {
69        self.capabilities.contains(&expected)
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn capability_declarations_are_explicit() {
79        let declared = BackendCapabilities::new(Backend::Mock, &[Capability::Documents]);
80        assert_eq!(declared.backend(), Backend::Mock);
81        assert!(declared.supports(Capability::Documents));
82        assert!(!declared.supports(Capability::Graph));
83    }
84}