hightower_kv/
error.rs

1use std::io;
2
3use thiserror::Error;
4
5/// Result type alias using the crate's Error type
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error types for the key-value store
9#[derive(Debug, Error)]
10pub enum Error {
11    /// Feature not yet implemented
12    #[error("feature unimplemented: {0}")]
13    Unimplemented(&'static str),
14    /// Validation failed for input data
15    #[error("validation failed: {0}")]
16    Validation(&'static str),
17    /// Conflict occurred (e.g., duplicate key)
18    #[error("conflict: {0}")]
19    Conflict(&'static str),
20    /// Requested item was not found
21    #[error("not found: {0}")]
22    NotFound(&'static str),
23    /// Internal invariant was violated
24    #[error("invariant violated: {0}")]
25    Invariant(&'static str),
26    /// Cryptographic operation failed
27    #[error("crypto error: {0}")]
28    Crypto(String),
29    /// I/O operation failed
30    #[error("io error: {0}")]
31    Io(#[from] io::Error),
32    /// Serialization or deserialization failed
33    #[error("serialization error: {0}")]
34    Serialization(String),
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use std::io;
41
42    #[test]
43    fn unimplemented_error_message_is_stable() {
44        let err = Error::Unimplemented("test");
45        assert_eq!(format!("{err}"), "feature unimplemented: test");
46    }
47
48    #[test]
49    fn validation_error_message_is_stable() {
50        let err = Error::Validation("missing field");
51        assert_eq!(format!("{err}"), "validation failed: missing field");
52    }
53
54    #[test]
55    fn conflict_error_message_is_stable() {
56        let err = Error::Conflict("duplicate");
57        assert_eq!(format!("{err}"), "conflict: duplicate");
58    }
59
60    #[test]
61    fn not_found_error_message_is_stable() {
62        let err = Error::NotFound("missing");
63        assert_eq!(format!("{err}"), "not found: missing");
64    }
65
66    #[test]
67    fn invariant_error_message_is_stable() {
68        let err = Error::Invariant("stale");
69        assert_eq!(format!("{err}"), "invariant violated: stale");
70    }
71
72    #[test]
73    fn crypto_error_message_is_stable() {
74        let err = Error::Crypto("boom".into());
75        assert_eq!(format!("{err}"), "crypto error: boom");
76    }
77
78    #[test]
79    fn io_error_display_is_prefixed() {
80        let err = Error::Io(io::Error::new(io::ErrorKind::Other, "oops"));
81        assert!(format!("{err}").contains("io error: oops"));
82    }
83
84    #[test]
85    fn serialization_error_display_is_prefixed() {
86        let err = Error::Serialization("bad".into());
87        assert_eq!(format!("{err}"), "serialization error: bad");
88    }
89}