#[cfg(feature = "mfek")]
pub mod mfek;
mod string;
pub use string::*;
use crate::point::PointType;
use std::error::Error;
use std::fmt::{Formatter, Display};
use std::io;
use std::rc::Rc;
use xmltree::{ParseError, Error as XMLTreeError};
#[cfg(feature = "glifserde")]
use plist::Error as PlistError;
pub type GlifParserResult<T> = Result<T, GlifParserError>;
#[derive(Debug, Clone)]
pub enum GlifParserError {
GlifFileIoError(Option<Rc<io::Error>>),
GlifOutlineHasBadPointType{ci: usize, pi: usize, ptype: PointType},
GlifContourHasBadPointType{pi: usize, ptype: PointType},
GlifFilenameNotSet(String),
GlifFilenameInsane(String),
GlifComponentsCyclical(String),
GlifLibError,
GlifNotUtf8,
XmlParseError(String),
PedanticXmlParseError(String),
XmlWriteError(String),
GlifInputError(String),
ImageNotLoaded,
ImageNotPNG,
ImageNotDecodable,
ImageIoError(Option<Rc<io::Error>>),
ColorNotRGBA,
TypeConversionError{req_type: &'static str, req_variant: String},
ContourLenOneUnexpected,
ContourLenZeroUnexpected,
PointIdxOutOfBounds{idx: usize, len: usize},
ContourNoPrevious(usize),
ContourNoNext(usize),
}
impl Display for GlifParserError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "glifparser error: {}", match self {
Self::GlifFileIoError(ioe) => {
format!("System error when loading glif file: {:?}", ioe)
},
Self::GlifOutlineHasBadPointType{ci, pi, ptype} => {
format!("Bad point type {:?} in outline @({}, {}))", ptype, ci, pi)
},
Self::GlifContourHasBadPointType{pi, ptype} => {
format!("Bad point type {:?} in contour @{})", ptype, pi)
},
Self::GlifFilenameNotSet(s) => {
format!("Glyph filename not set: {}", &s)
},
Self::GlifFilenameInsane(s) => {
format!("Glyph filename not sane: {}", &s)
},
Self::GlifNotUtf8 => {
format!("Glyph not utf-8")
},
Self::GlifComponentsCyclical(s) => {
format!("Glyph components are cyclical: {}", &s)
},
Self::GlifLibError => {
format!("Glif <lib> invalid")
},
Self::XmlParseError(s) | Self::XmlWriteError(s) => {
format!("XML error: {}", &s)
},
Self::PedanticXmlParseError(s) => {
format!("XML error (requested pedantry, would not normally be an error): {}", &s)
},
Self::GlifInputError(s) => {
format!("Glif format spec error: {}", &s)
},
Self::ImageNotLoaded => {
format!("Tried to access data for image whose data hasn't been loaded")
},
Self::ImageNotPNG => {
format!("Image not formatted as PNG. The glif file format only supports PNG. If you want to support other types, you have to work on the data yourself.")
},
Self::ImageNotDecodable => {
format!("Image not decodable")
},
Self::ImageIoError(ioe) => {
format!("System error when loading image: {:?}", ioe)
},
Self::ColorNotRGBA => {
format!("Color not RGBA")
},
Self::TypeConversionError { req_type, req_variant } => {
format!("Type conversion error: {} not in {}", req_variant, req_type)
}
Self::PointIdxOutOfBounds { idx, len } => {
format!("The point index {} is out of bounds as self.len() == {}", idx, len)
}
Self::ContourLenOneUnexpected => {
format!("On a contour of length one, there's no previous/next point")
}
Self::ContourLenZeroUnexpected => {
format!("On an empty invalid contour (len == 0), there's no previous/next point")
}
Self::ContourNoPrevious(len) => {
format!("Asked for previous index of 0 on an open contour (len {})", len)
}
Self::ContourNoNext(len) => {
format!("Asked for next index of last point, {}, on an open contour", len)
}
})
}
}
impl From<ParseError> for GlifParserError {
fn from(e: ParseError) -> Self {
Self::XmlParseError(format!("{}", e))
}
}
impl From<XMLTreeError> for GlifParserError {
fn from(e: XMLTreeError) -> Self {
Self::XmlWriteError(format!("{}", e))
}
}
#[cfg(feature = "glifserde")]
impl From<PlistError> for GlifParserError {
fn from(_e: PlistError) -> Self {
GlifParserError::GlifLibError
}
}
impl From<std::string::FromUtf8Error> for GlifParserError {
fn from(_: std::string::FromUtf8Error) -> Self {
Self::GlifNotUtf8
}
}
impl Error for GlifParserError {}