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    /// A non-blocking lock acquisition would block: the advisory lock file
45    /// is held by another cooperating writer. Distinctly matchable so
46    /// fail-fast consumers can map contention to their own taxonomy (#105).
47    LockWouldBlock {
48        path: std::path::PathBuf,
49    },
50}
51
52impl fmt::Display for StorageError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            StorageError::Io(msg) => write!(f, "IO error: {}", msg),
56            StorageError::Serde(msg) => write!(f, "Serialization error: {}", msg),
57            StorageError::Parquet(msg) => write!(f, "Parquet error: {}", msg),
58            StorageError::Invalid(msg) => write!(f, "Invalid data: {}", msg),
59            StorageError::Lance(msg) => write!(f, "Lance error: {}", msg),
60            StorageError::QueryError(msg) => write!(f, "Query error: {}", msg),
61            StorageError::InvalidState(msg) => write!(f, "Invalid state: {}", msg),
62            StorageError::UnsupportedFormat(msg) => write!(f, "Unsupported format: {}", msg),
63            StorageError::UnsupportedFiletype(msg) => write!(f, "Unsupported filetype: {}", msg),
64            StorageError::DimensionMismatch { expected, found } => {
65                write!(
66                    f,
67                    "Dimension mismatch: expected {}, found {}",
68                    expected, found
69                )
70            }
71            StorageError::Overflow(msg) => write!(f, "Overflow error: {}", msg),
72            StorageError::LockWouldBlock { path } => {
73                write!(f, "Lock would block: {} is held by another holder", path.display())
74            }
75        }
76    }
77}
78
79impl std::error::Error for StorageError {}
80
81pub type StorageResult<T> = Result<T, StorageError>;
82
83// Logging harness
84use std::sync::Once;
85
86static INIT: Once = Once::new();
87
88pub fn init() {
89    INIT.call_once(|| {
90        // Read RUST_LOG env variable, default to "info" if not set
91        let env = env_logger::Env::default().default_filter_or("debug");
92
93        // don't panic if called multiple times across binaries
94        let _ = env_logger::Builder::from_env(env).try_init();
95    });
96}