1use std::io;
2
3use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error)]
10pub enum Error {
11 #[error("feature unimplemented: {0}")]
13 Unimplemented(&'static str),
14 #[error("validation failed: {0}")]
16 Validation(&'static str),
17 #[error("conflict: {0}")]
19 Conflict(&'static str),
20 #[error("not found: {0}")]
22 NotFound(&'static str),
23 #[error("invariant violated: {0}")]
25 Invariant(&'static str),
26 #[error("crypto error: {0}")]
28 Crypto(String),
29 #[error("io error: {0}")]
31 Io(#[from] io::Error),
32 #[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}