sqlitegraph_cli/
client.rs1use sqlitegraph::backend::GraphBackend;
2use sqlitegraph::backend::SqliteGraphBackend;
3
4pub enum CliClient {
6 Sqlite(SqliteGraphBackend),
7 #[cfg(feature = "native-v3")]
8 V3(sqlitegraph::backend::native::v3::V3Backend),
9}
10
11impl CliClient {
12 pub fn open(backend: super::cli::BackendType, path: &std::path::Path) -> anyhow::Result<Self> {
14 match backend {
15 super::cli::BackendType::Sqlite => {
16 let graph = sqlitegraph::SqliteGraph::open(path)?;
17 Ok(Self::Sqlite(SqliteGraphBackend::from_graph(graph)))
18 }
19 #[cfg(feature = "native-v3")]
20 super::cli::BackendType::V3 => {
21 use sqlitegraph::backend::native::v3::V3Backend;
22 let backend = if path.exists() {
23 V3Backend::open(path)?
24 } else {
25 V3Backend::create(path)?
26 };
27 Ok(Self::V3(backend))
28 }
29 }
30 }
31
32 pub fn open_in_memory(backend: super::cli::BackendType) -> anyhow::Result<Self> {
34 match backend {
35 super::cli::BackendType::Sqlite => {
36 let graph = sqlitegraph::SqliteGraph::open_in_memory()?;
37 Ok(Self::Sqlite(SqliteGraphBackend::from_graph(graph)))
38 }
39 #[cfg(feature = "native-v3")]
40 super::cli::BackendType::V3 => {
41 anyhow::bail!("V3 backend does not support in-memory mode")
42 }
43 }
44 }
45
46 pub fn backend(&self) -> &dyn GraphBackend {
48 match self {
49 Self::Sqlite(b) => b,
50 #[cfg(feature = "native-v3")]
51 Self::V3(b) => b,
52 }
53 }
54
55 pub fn sqlite_graph(&self) -> Option<&sqlitegraph::SqliteGraph> {
57 match self {
58 Self::Sqlite(b) => Some(b.graph()),
59 #[cfg(feature = "native-v3")]
60 Self::V3(_) => None,
61 }
62 }
63
64 pub fn sqlite_backend(&self) -> Option<&SqliteGraphBackend> {
66 match self {
67 Self::Sqlite(b) => Some(b),
68 #[cfg(feature = "native-v3")]
69 Self::V3(_) => None,
70 }
71 }
72
73 pub fn backend_name(&self) -> &'static str {
75 match self {
76 Self::Sqlite(_) => "sqlite",
77 #[cfg(feature = "native-v3")]
78 Self::V3(_) => "v3",
79 }
80 }
81
82 pub fn node_count(&self) -> anyhow::Result<usize> {
84 let ids = self.backend().entity_ids()?;
85 Ok(ids.len())
86 }
87}