keyset_font/
error.rs

1use std::fmt;
2
3use ttf_parser::FaceParsingError;
4
5/// A font error
6#[derive(Debug)]
7pub struct Error(FaceParsingError);
8
9impl fmt::Display for Error {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        write!(f, "{}", self.0)
12    }
13}
14
15impl std::error::Error for Error {
16    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
17        Some(&self.0)
18    }
19}
20
21impl From<FaceParsingError> for Error {
22    fn from(error: FaceParsingError) -> Self {
23        Self(error)
24    }
25}
26
27/// A [`Result`](std::result::Result) where the error type is [`Error`]
28pub type Result<T> = std::result::Result<T, Error>;
29
30#[cfg(test)]
31mod tests {
32    use std::error::Error as _;
33
34    use ttf_parser::Face;
35
36    use super::*;
37
38    #[test]
39    fn error_fmt() {
40        let error = crate::Font::from_ttf(b"invalid".to_vec()).unwrap_err();
41
42        assert_eq!(format!("{error}"), "unknown magic")
43    }
44
45    #[test]
46    fn error_source() {
47        let error = crate::Font::from_ttf(b"invalid".to_vec()).unwrap_err();
48
49        assert!(error.source().is_some());
50        assert_eq!(format!("{}", error.source().unwrap()), "unknown magic",)
51    }
52
53    #[test]
54    fn error_from() {
55        let result = Face::parse(b"invalid", 0);
56        let error: Error = result.unwrap_err().into();
57
58        assert_eq!(format!("{error}"), "unknown magic")
59    }
60}