quickleaf/
error.rs

1//! Error types for cache operations.
2//!
3//! This module defines the error types that can occur during cache operations.
4
5use std::fmt::{Debug, Display};
6
7/// Errors that can occur during cache operations.
8///
9/// # Examples
10///
11/// ```
12/// use quickleaf::Error;
13/// use quickleaf::Cache;
14/// use quickleaf::valu3::traits::ToValueBehavior;
15///
16/// let mut cache = Cache::new(10);
17///
18/// // Trying to remove a non-existent key returns an error
19/// match cache.remove("nonexistent") {
20///     Err(Error::KeyNotFound) => println!("Key not found as expected"),
21///     _ => panic!("Expected KeyNotFound error"),
22/// }
23/// ```
24#[derive(PartialEq)]
25pub enum Error {
26    /// The specified sort key was not found during list operations.
27    ///
28    /// This can occur when using `start_after_key` in `ListProps` with a key
29    /// that doesn't exist in the cache.
30    ///
31    /// # Examples
32    ///
33    /// ```
34    /// use quickleaf::Error;
35    /// use quickleaf::Cache;
36    /// use quickleaf::ListProps;
37    /// use quickleaf::valu3::traits::ToValueBehavior;
38    ///
39    /// let mut cache = Cache::new(10);
40    /// cache.insert("existing_key", "value");
41    ///
42    /// let props = ListProps::default().start_after_key("nonexistent_key");
43    /// match cache.list(props) {
44    ///     Err(Error::SortKeyNotFound) => println!("Sort key not found"),
45    ///     _ => panic!("Expected SortKeyNotFound error"),
46    /// }
47    /// ```
48    SortKeyNotFound,
49
50    /// A cache with the same identifier already exists.
51    ///
52    /// This error is currently not used in the main API but reserved for
53    /// future functionality.
54    CacheAlreadyExists,
55
56    /// A sort key already exists.
57    ///
58    /// This error is currently not used in the main API but reserved for
59    /// future functionality.
60    SortKeyExists,
61
62    /// A table with the same name already exists.
63    ///
64    /// This error is currently not used in the main API but reserved for
65    /// future functionality.
66    TableAlreadyExists,
67
68    /// The specified key was not found in the cache.
69    ///
70    /// This occurs when trying to remove a key that doesn't exist.
71    ///
72    /// # Examples
73    ///
74    /// ```
75    /// use quickleaf::Error;
76    /// use quickleaf::Cache;
77    /// use quickleaf::valu3::traits::ToValueBehavior;
78    ///
79    /// let mut cache = Cache::new(10);
80    ///
81    /// match cache.remove("missing_key") {
82    ///     Err(Error::KeyNotFound) => println!("Key not found"),
83    ///     Err(_) => println!("Other error"),
84    ///     Ok(_) => panic!("Expected an error"),
85    /// }
86    /// ```
87    KeyNotFound,
88}
89
90impl Display for Error {
91    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
92        match self {
93            Error::SortKeyNotFound => write!(f, "Sort key not found"),
94            Error::CacheAlreadyExists => write!(f, "Cache already exists"),
95            Error::SortKeyExists => write!(f, "Sort key exists"),
96            Error::TableAlreadyExists => write!(f, "Table already exists"),
97            Error::KeyNotFound => write!(f, "Key not found"),
98        }
99    }
100}
101
102impl Debug for Error {
103    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
104        Display::fmt(&self, f)
105    }
106}
107
108impl std::error::Error for Error {}