Skip to main content

genegraph_storage/
lib.rs

1#![allow(async_fn_in_trait)]
2pub mod catalog;
3pub mod commit;
4pub mod generations;
5pub mod graph;
6pub mod lance_storage_graph;
7pub mod lancefmt;
8pub mod metadata;
9pub mod traits;
10
11#[cfg(test)]
12mod tests;
13
14use std::fmt;
15
16// Error Handling harness
17//
18// `#[non_exhaustive]` lets this crate add error variants in future releases
19// without breaking downstream `match` expressions that carry a wildcard arm.
20#[derive(Debug)]
21#[non_exhaustive]
22pub enum StorageError {
23    Io(String),
24    Serde(serde_json::Error),
25    Parquet(String),
26    Invalid(String),
27    Lance(String),
28    QueryError(String),
29    /// A storage resource is in an unexpected state (e.g. the metadata path
30    /// passed to a `save_*` call does not match the instance metadata path).
31    InvalidState(String),
32    /// A filetype does not map to a known storage format.
33    UnsupportedFormat(String),
34    /// A key/filetype is not recognised as one of the supported file types.
35    UnsupportedFiletype(String),
36    /// Dimensions recorded in schema metadata do not match the dimensions
37    /// recorded in storage metadata.
38    DimensionMismatch {
39        expected: String,
40        found: String,
41    },
42    /// A numeric value exceeds the range of the storage type it is written to.
43    Overflow(String),
44}
45
46impl fmt::Display for StorageError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            StorageError::Io(msg) => write!(f, "IO error: {}", msg),
50            StorageError::Serde(msg) => write!(f, "Serialization error: {}", msg),
51            StorageError::Parquet(msg) => write!(f, "Parquet error: {}", msg),
52            StorageError::Invalid(msg) => write!(f, "Invalid data: {}", msg),
53            StorageError::Lance(msg) => write!(f, "Lance error: {}", msg),
54            StorageError::QueryError(msg) => write!(f, "Query error: {}", msg),
55            StorageError::InvalidState(msg) => write!(f, "Invalid state: {}", msg),
56            StorageError::UnsupportedFormat(msg) => write!(f, "Unsupported format: {}", msg),
57            StorageError::UnsupportedFiletype(msg) => write!(f, "Unsupported filetype: {}", msg),
58            StorageError::DimensionMismatch { expected, found } => {
59                write!(
60                    f,
61                    "Dimension mismatch: expected {}, found {}",
62                    expected, found
63                )
64            }
65            StorageError::Overflow(msg) => write!(f, "Overflow error: {}", msg),
66        }
67    }
68}
69
70impl std::error::Error for StorageError {}
71
72pub type StorageResult<T> = Result<T, StorageError>;
73
74// Logging harness
75use std::sync::Once;
76
77static INIT: Once = Once::new();
78
79pub fn init() {
80    INIT.call_once(|| {
81        // Read RUST_LOG env variable, default to "info" if not set
82        let env = env_logger::Env::default().default_filter_or("debug");
83
84        // don't panic if called multiple times across binaries
85        let _ = env_logger::Builder::from_env(env).try_init();
86    });
87}