1use std::borrow::Cow;
14use std::convert::From;
15use std::error::Error;
16use std::io;
17
18macro_rules! impl_display {
19 ($enum:ident, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
20
21 impl ::std::fmt::Display for $enum {
22 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
23 use self::$enum::*;
24 match &self {
25 $(
26 $variant => write!(f, "{}", $fmt_string),
27 )+
28 }
29 }
30 }
31 };
32}
33
34#[derive(Debug)]
36pub enum FontLoadingError {
37 UnknownFormat,
39 NoSuchFontInCollection,
44 Parse,
46 NoFilesystem,
49 Io(io::Error),
51}
52
53impl Error for FontLoadingError {}
54
55impl_display! { FontLoadingError, {
56 UnknownFormat => "unknown format",
57 NoSuchFontInCollection => "no such font in the collection",
58 Parse => "parse error",
59 NoFilesystem => "no filesystem present",
60 Io(e) => format!("I/O error: {}", e),
61 }
62}
63
64impl From<io::Error> for FontLoadingError {
65 fn from(error: io::Error) -> FontLoadingError {
66 FontLoadingError::Io(error)
67 }
68}
69
70#[derive(Clone, Copy, PartialEq, Debug)]
72pub enum GlyphLoadingError {
73 NoSuchGlyph,
75 PlatformError,
77}
78
79impl Error for GlyphLoadingError {}
80
81impl_display! { GlyphLoadingError, {
82 NoSuchGlyph => "no such glyph",
83 PlatformError => "platform error",
84 }
85}
86
87#[cfg(target_family = "windows")]
88impl From<winapi::um::winnt::HRESULT> for GlyphLoadingError {
89 fn from(_err: winapi::um::winnt::HRESULT) -> GlyphLoadingError {
90 GlyphLoadingError::PlatformError
91 }
92}
93
94#[derive(Clone, PartialEq, Debug)]
96pub enum SelectionError {
97 NotFound,
99 CannotAccessSource {
101 reason: Option<Cow<'static, str>>,
103 },
104}
105
106impl Error for SelectionError {}
107
108impl_display! { SelectionError, {
109 NotFound => "no font found",
110 CannotAccessSource { reason: ref maybe_cow } => maybe_cow.as_deref().unwrap_or("failed to access source")
111 }
112}