use std::borrow::Cow;
use std::convert::From;
use std::error::Error;
use std::io;
macro_rules! impl_display {
($enum:ident, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
impl ::std::fmt::Display for $enum {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use self::$enum::*;
match &self {
$(
$variant => write!(f, "{}", $fmt_string),
)+
}
}
}
};
}
#[derive(Debug)]
pub enum FontLoadingError {
UnknownFormat,
NoSuchFontInCollection,
Parse,
NoFilesystem,
Io(io::Error),
}
impl Error for FontLoadingError {}
impl_display! { FontLoadingError, {
UnknownFormat => "unknown format",
NoSuchFontInCollection => "no such font in the collection",
Parse => "parse error",
NoFilesystem => "no filesystem present",
Io(e) => format!("I/O error: {}", e),
}
}
impl From<io::Error> for FontLoadingError {
fn from(error: io::Error) -> FontLoadingError {
FontLoadingError::Io(error)
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum GlyphLoadingError {
NoSuchGlyph,
PlatformError,
}
impl Error for GlyphLoadingError {}
impl_display! { GlyphLoadingError, {
NoSuchGlyph => "no such glyph",
PlatformError => "platform error",
}
}
#[cfg(target_family = "windows")]
impl From<winapi::um::winnt::HRESULT> for GlyphLoadingError {
fn from(_err: winapi::um::winnt::HRESULT) -> GlyphLoadingError {
GlyphLoadingError::PlatformError
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum SelectionError {
NotFound,
CannotAccessSource {
reason: Option<Cow<'static, str>>,
},
}
impl Error for SelectionError {}
impl_display! { SelectionError, {
NotFound => "no font found",
CannotAccessSource { reason: ref maybe_cow } => maybe_cow.as_deref().unwrap_or("failed to access source")
}
}