esvg/
error.rs

1//! Error types used by this library
2use ::quick_xml;
3use quick_xml::events::attributes::AttrError;
4use std::num::{ParseFloatError, ParseIntError};
5use std::str;
6use thiserror::Error;
7
8/// All the possible errors returned by this library
9#[derive(Error, Debug)]
10pub enum Error {
11    /// When parsing an angle and its not between 0 and 360 degrees
12    #[error("Angle out of range: {0:?}")]
13    AngleOutOfRange(f64),
14    /// When trying to parse a paper name and it doesn't make sense or it is not implemented yet
15    #[error("Unknown paper: {0:?}")]
16    UnknownPaper(String),
17    #[error("A style tag is badly formed")]
18    MalformedStyle,
19    #[error("Could not parse an integer: {0:?}")]
20    ParseInt(ParseIntError),
21    #[error("Could not parse a float: {0:?}")]
22    ParseFloat(ParseFloatError),
23    #[error("Could not parse a bool: {0:?}")]
24    ParseBool(str::ParseBoolError),
25    #[error("An io error: {0:?}")]
26    IOError(std::io::Error),
27    #[error("An XML error: {0:?}")]
28    XMLError(quick_xml::Error),
29    #[error("An XML attribute error: {0:?}")]
30    XMLAttrError(AttrError),
31    /// Attempted to parse something that wasn't a valid xml document
32    #[error("Tried to parse document but it was empty")]
33    EmptyDocument,
34    #[error("A utf-8 encoding error: {0:?}")]
35    UTF8Error(str::Utf8Error),
36    /// A problem trying to parse a hex colour, likely the value is too short
37    #[error("Invalid colour '{0:?}'")]
38    ColourError(String),
39}
40
41impl From<ParseIntError> for Error {
42    fn from(other: ParseIntError) -> Self {
43        Error::ParseInt(other)
44    }
45}
46
47impl From<ParseFloatError> for Error {
48    fn from(other: ParseFloatError) -> Self {
49        Error::ParseFloat(other)
50    }
51}
52
53impl From<str::ParseBoolError> for Error {
54    fn from(other: str::ParseBoolError) -> Self {
55        Error::ParseBool(other)
56    }
57}
58
59impl From<std::io::Error> for Error {
60    fn from(other: std::io::Error) -> Self {
61        Error::IOError(other)
62    }
63}
64
65impl From<quick_xml::Error> for Error {
66    fn from(other: quick_xml::Error) -> Self {
67        Error::XMLError(other)
68    }
69}
70
71impl From<AttrError> for Error {
72    fn from(other: AttrError) -> Self {
73        Error::XMLAttrError(other)
74    }
75}
76
77impl From<str::Utf8Error> for Error {
78    fn from(other: str::Utf8Error) -> Self {
79        Error::UTF8Error(other)
80    }
81}