kevy_window/error.rs
1//! Why a cold (spilled) segment operation failed.
2
3use std::fmt;
4
5/// Why sealing entries into a cold segment, or reading them back, failed.
6/// The spill is derived data: the caller reports the error and the entries
7/// stay rebuildable from the rows.
8///
9/// ```
10/// use kevy_window::ColdError;
11///
12/// assert_eq!(ColdError::CorruptKey.to_string(), "corrupt cold key");
13/// ```
14#[derive(Debug)]
15#[non_exhaustive]
16pub enum ColdError {
17 /// The segment directory could not be created.
18 ///
19 /// ```
20 /// # use kevy_index::{IndexValue, Segment, WindowShape, WindowSpec};
21 /// # let dir = kevy_tmpdir::TmpDir::new("cold-error-doc");
22 /// let mut w = kevy_window::WindowRt::new(WindowSpec::new("ts", 100, 50), WindowShape::PlainI64);
23 /// let mut seg = Segment::new();
24 /// seg.apply(b"r:10", None, Some(IndexValue::I64(10)));
25 /// seg.apply(b"r:300", None, Some(IndexValue::I64(300)));
26 /// let file = dir.path().join("plain-file");
27 /// std::fs::write(&file, b"")?;
28 /// // a directory cannot be created under a regular file
29 /// let e = w.slide(b"t.ts", &mut seg, &file.join("segs")).unwrap_err();
30 /// assert!(matches!(e, kevy_window::ColdError::Io(_)));
31 /// assert_eq!(seg.stats().entries, 2); // the tree is untouched
32 /// # Ok::<(), Box<dyn std::error::Error>>(())
33 /// ```
34 Io(std::io::Error),
35 /// A segment or the manifest refused.
36 ///
37 /// ```
38 /// # use kevy_index::{IndexValue, Segment, WindowShape, WindowSpec};
39 /// # let dir = kevy_tmpdir::TmpDir::new("cold-error-doc");
40 /// let mut w = kevy_window::WindowRt::new(WindowSpec::new("ts", 100, 50), WindowShape::PlainI64);
41 /// let mut seg = Segment::new();
42 /// seg.apply(b"r:10", None, Some(IndexValue::I64(10)));
43 /// seg.apply(b"r:300", None, Some(IndexValue::I64(300)));
44 /// let file = dir.path().join("plain-file");
45 /// std::fs::write(&file, b"")?;
46 /// // the segment directory is a regular file, so its manifest will not open
47 /// let e = w.slide(b"t.ts", &mut seg, &file).unwrap_err();
48 /// assert!(matches!(e, kevy_window::ColdError::Seg(_)));
49 /// # Ok::<(), Box<dyn std::error::Error>>(())
50 /// ```
51 Seg(kevy_seg::SegError),
52 /// The segment just written would not reopen.
53 ///
54 /// ```
55 /// use kevy_window::ColdError;
56 /// let e = ColdError::Reopen { file: "idx-74-0.seg".into(), source: kevy_seg::SegError::Corrupt("footer") };
57 /// assert!(e.to_string().starts_with("reopen idx-74-0.seg: "));
58 /// assert!(std::error::Error::source(&e).is_some());
59 /// ```
60 Reopen {
61 /// The segment's file name.
62 ///
63 /// ```
64 /// use kevy_window::ColdError;
65 /// let e = ColdError::Reopen { file: "idx-74-0.seg".into(), source: kevy_seg::SegError::Corrupt("footer") };
66 /// let ColdError::Reopen { file, .. } = &e else { unreachable!() };
67 /// assert_eq!(file, "idx-74-0.seg");
68 /// ```
69 file: String,
70 /// Why it would not reopen.
71 ///
72 /// ```
73 /// use kevy_window::ColdError;
74 /// let e = ColdError::Reopen { file: "idx-74-0.seg".into(), source: kevy_seg::SegError::Corrupt("footer") };
75 /// let ColdError::Reopen { source, .. } = &e else { unreachable!() };
76 /// assert!(matches!(source, kevy_seg::SegError::Corrupt("footer")));
77 /// ```
78 source: kevy_seg::SegError,
79 },
80 /// A cold key that does not decode.
81 ///
82 /// ```
83 /// let e = kevy_window::ColdError::CorruptKey;
84 /// assert_eq!(e.to_string(), "corrupt cold key");
85 /// assert!(std::error::Error::source(&e).is_none());
86 /// ```
87 CorruptKey,
88 /// A cold payload that does not decode.
89 ///
90 /// ```
91 /// let e = kevy_window::ColdError::CorruptPayload;
92 /// assert_eq!(e.to_string(), "corrupt cold payload");
93 /// ```
94 CorruptPayload,
95}
96
97impl fmt::Display for ColdError {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 match self {
100 Self::Io(e) => write!(f, "{e}"),
101 Self::Seg(e) => write!(f, "{e}"),
102 Self::Reopen { file, source } => write!(f, "reopen {file}: {source}"),
103 Self::CorruptKey => f.write_str("corrupt cold key"),
104 Self::CorruptPayload => f.write_str("corrupt cold payload"),
105 }
106 }
107}
108
109impl std::error::Error for ColdError {
110 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
111 match self {
112 Self::Io(e) => Some(e),
113 Self::Seg(e) | Self::Reopen { source: e, .. } => Some(e),
114 Self::CorruptKey | Self::CorruptPayload => None,
115 }
116 }
117}
118
119impl From<kevy_seg::SegError> for ColdError {
120 fn from(e: kevy_seg::SegError) -> Self {
121 Self::Seg(e)
122 }
123}