keyset_key/kle/
error.rs

1/// An error parsing a KLE layout
2#[derive(Debug)]
3pub enum Error {
4    /// An error in parsing the KLE JSON file
5    JsonParseError(serde_json::Error),
6    /// A key size not supported by `keyset`
7    UnsupportedKeySize {
8        /// The key's `w` value
9        w: f64,
10        /// The key's `h` value
11        h: f64,
12        /// The key's `x2` value
13        x2: f64,
14        /// The key's `y2` value
15        y2: f64,
16        /// The key's `w2` value
17        w2: f64,
18        /// The key's `h2` value
19        h2: f64,
20    },
21}
22
23impl std::fmt::Display for Error {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Self::UnsupportedKeySize {
27                w,
28                h,
29                x2,
30                y2,
31                w2,
32                h2,
33            } => write!(
34                f,
35                "unsupported non-standard key size \
36                (w: {w:.2}, h: {h:.2}, x2: {x2:.2}, y2: {y2:.2}, w2: {w2:.2}, h2: {h2:.2}). \
37                Note only ISO enter and stepped caps are supported as special cases"
38            ),
39            Self::JsonParseError(error) => error.fmt(f),
40        }
41    }
42}
43
44impl std::error::Error for Error {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            Self::UnsupportedKeySize { .. } => None,
48            Self::JsonParseError(error) => Some(error),
49        }
50    }
51}
52
53impl From<serde_json::Error> for Error {
54    fn from(error: serde_json::Error) -> Self {
55        Self::JsonParseError(error)
56    }
57}
58
59/// A [`std::result::Result`] with [`Error`] as it's error type
60pub type Result<T> = std::result::Result<T, Error>;
61
62#[cfg(test)]
63pub mod tests {
64    use std::error::Error as _;
65
66    use assert_matches::assert_matches;
67
68    use super::*;
69
70    #[test]
71    fn error_fmt() {
72        let unsupported_key_size = Error::UnsupportedKeySize {
73            w: 1.,
74            h: 1.,
75            x2: -0.25,
76            y2: 0.,
77            w2: 1.5,
78            h2: 1.,
79        };
80        assert_eq!(
81            format!("{unsupported_key_size}"),
82            "unsupported non-standard key size (w: 1.00, h: 1.00, x2: -0.25, y2: 0.00, w2: 1.50, \
83            h2: 1.00). Note only ISO enter and stepped caps are supported as special cases"
84        );
85
86        let json_parse_error: Error = serde_json::from_str::<i32>("error").unwrap_err().into();
87        assert_eq!(
88            format!("{json_parse_error}"),
89            "expected value at line 1 column 1"
90        );
91    }
92
93    #[test]
94    fn error_source() {
95        let unsupported_key_size = Error::UnsupportedKeySize {
96            w: 1.,
97            h: 1.,
98            x2: -0.25,
99            y2: 0.,
100            w2: 1.5,
101            h2: 1.,
102        };
103        assert!(unsupported_key_size.source().is_none());
104
105        let json_parse_error: Error = serde_json::from_str::<i32>("error").unwrap_err().into();
106        assert!(json_parse_error.source().is_some());
107    }
108
109    #[test]
110    fn error_from() {
111        let json_parse_error = serde_json::from_str::<i32>("error").unwrap_err();
112
113        assert_matches!(json_parse_error.into(), Error::JsonParseError(..));
114    }
115}