Skip to main content

genegraph_storage/
lib.rs

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