1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum LedStripError<E> {
8 BufferTooSmall { required: usize, capacity: usize },
10 Operation(E),
12}
13
14pub type LedStripResult<T, E> = Result<T, LedStripError<E>>;
16
17impl<E> From<E> for LedStripError<E> {
18 fn from(error: E) -> Self {
19 Self::Operation(error)
20 }
21}
22
23impl<E: core::fmt::Display> core::fmt::Display for LedStripError<E> {
24 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25 match self {
26 Self::BufferTooSmall { required, capacity } => {
27 write!(
28 f,
29 "buffer too small: required={required}, capacity={capacity}"
30 )
31 }
32 Self::Operation(inner) => write!(f, "operation error: {inner}"),
33 }
34 }
35}
36
37impl<E: core::error::Error + 'static> core::error::Error for LedStripError<E> {
38 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
39 match self {
40 Self::Operation(inner) => Some(inner),
41 _ => None,
42 }
43 }
44}
45
46impl<E> LedStripError<E> {
47 pub fn map_operation<F, E2>(self, f: F) -> LedStripError<E2>
52 where
53 F: FnOnce(E) -> E2,
54 {
55 match self {
56 Self::BufferTooSmall { required, capacity } => {
57 LedStripError::BufferTooSmall { required, capacity }
58 }
59 Self::Operation(e) => LedStripError::Operation(f(e)),
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67 use core::convert::Infallible;
68
69 #[derive(Debug, PartialEq, Eq)]
70 struct DummyOp(&'static str);
71
72 impl core::fmt::Display for DummyOp {
73 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
74 f.write_str(self.0)
75 }
76 }
77
78 impl core::error::Error for DummyOp {}
79
80 #[test]
81 fn display_buffer_too_small_includes_fields() {
82 let e: LedStripError<DummyOp> = LedStripError::BufferTooSmall {
83 required: 144,
84 capacity: 128,
85 };
86 let s = e.to_string();
87 assert!(s.contains("required=144"), "{s}");
88 assert!(s.contains("capacity=128"), "{s}");
89 }
90
91 #[test]
92 fn display_operation_wraps_inner() {
93 let e: LedStripError<DummyOp> = LedStripError::Operation(DummyOp("spi nack"));
94 assert!(e.to_string().contains("spi nack"));
95 }
96
97 #[test]
98 fn error_source_returns_inner_for_operation_only() {
99 let op: LedStripError<DummyOp> = LedStripError::Operation(DummyOp("boom"));
100 let src = core::error::Error::source(&op);
101 assert!(src.is_some());
102 assert_eq!(src.unwrap().to_string(), "boom");
103
104 let buf: LedStripError<DummyOp> = LedStripError::BufferTooSmall {
105 required: 1,
106 capacity: 0,
107 };
108 assert!(core::error::Error::source(&buf).is_none());
109 }
110
111 #[test]
112 fn from_generic_error_wraps_into_operation() {
113 let e: LedStripError<DummyOp> = LedStripError::from(DummyOp("oh no"));
114 match e {
115 LedStripError::Operation(inner) => assert_eq!(inner.0, "oh no"),
116 _ => panic!("expected Operation variant"),
117 }
118 }
119
120 #[test]
121 fn map_operation_preserves_buffer_too_small() {
122 let e: LedStripError<Infallible> = LedStripError::BufferTooSmall {
123 required: 10,
124 capacity: 5,
125 };
126 let mapped: LedStripError<&str> = e.map_operation(|_| unreachable!());
127 assert_eq!(
128 mapped,
129 LedStripError::BufferTooSmall {
130 required: 10,
131 capacity: 5,
132 }
133 );
134 }
135
136 #[test]
137 fn map_operation_wraps_operation_variant() {
138 let e: LedStripError<&str> = LedStripError::Operation("inner");
139 let mapped: LedStripError<usize> = e.map_operation(|s| s.len());
140 assert_eq!(mapped, LedStripError::Operation(5));
141 }
142}