Skip to main content

genegraph_storage/
lib.rs

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