Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/kleene

crates/kleene-index/src/error.rs

/// Why an index could not be built, stored or opened.
#[derive(Debug)]
#[non_exhaustive]
pub enum IndexError {
    /// Reading or writing the index directory.
    Io(std::io::Error),
    /// The set or an index file, as the GraphAr reader reported it.
    Gar(String),
    /// Input that breaks an index's rules (lengths, order, ranges).
    Bad(String),
}

/// A result with an [`IndexError`].
pub type Result<T> = std::result::Result<T, IndexError>;

impl std::fmt::Display for IndexError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IndexError::Io(e) => write!(f, "index files: {e}"),
            IndexError::Gar(e) => write!(f, "set: {e}"),
            IndexError::Bad(e) => write!(f, "index input: {e}"),
        }
    }
}

impl std::error::Error for IndexError {}

impl From<std::io::Error> for IndexError {
    fn from(e: std::io::Error) -> Self {
        IndexError::Io(e)
    }
}

impl From<graphar::GraphArError> for IndexError {
    fn from(e: graphar::GraphArError) -> Self {
        IndexError::Gar(e.to_string())
    }
}

impl From<kleene_graph::GraphError> for IndexError {
    fn from(e: kleene_graph::GraphError) -> Self {
        IndexError::Bad(e.to_string())
    }
}

impl From<kleene_sparse::SparseError> for IndexError {
    fn from(e: kleene_sparse::SparseError) -> Self {
        IndexError::Bad(e.to_string())
    }
}

/// The set reader's error (DataFusion's type) as an [`IndexError`].
pub(crate) fn gar(e: impl std::fmt::Display) -> IndexError {
    IndexError::Gar(e.to_string())
}

pub(crate) fn bad(why: impl Into<String>) -> IndexError {
    IndexError::Bad(why.into())
}