Skip to main content

scry_index/
error.rs

1//! Error types for scry-index.
2
3use std::fmt;
4
5/// Result type alias for scry-index operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during index operations.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[non_exhaustive]
12pub enum Error {
13    /// The input data is empty.
14    EmptyData,
15    /// The input data is not sorted.
16    NotSorted,
17    /// A key could not be converted to a model input.
18    InvalidKey {
19        /// Description of what went wrong.
20        detail: String,
21    },
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::EmptyData => write!(f, "input data is empty"),
28            Self::NotSorted => write!(f, "input data is not sorted"),
29            Self::InvalidKey { detail } => write!(f, "invalid key: {detail}"),
30        }
31    }
32}
33
34impl std::error::Error for Error {}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn error_display() {
42        assert_eq!(Error::EmptyData.to_string(), "input data is empty");
43        assert_eq!(Error::NotSorted.to_string(), "input data is not sorted");
44        assert_eq!(
45            Error::InvalidKey {
46                detail: "overflow".into()
47            }
48            .to_string(),
49            "invalid key: overflow"
50        );
51    }
52
53    #[test]
54    fn error_is_send_sync() {
55        fn assert_send_sync<T: Send + Sync>() {}
56        assert_send_sync::<Error>();
57    }
58}