animate/path/
error.rs

1use std::error;
2use std::fmt;
3
4/// List of all errors.
5#[derive(Debug)]
6pub enum Error {
7    /// An input data ended earlier than expected.
8    ///
9    /// Should only appear on invalid input data.
10    /// Errors in a valid XML should be handled by errors below.
11    UnexpectedEndOfStream,
12
13    /// An input text contains unknown data.
14    UnexpectedData(usize),
15
16    /// A provided string doesn't have a valid data.
17    ///
18    /// For example, if we try to parse a color form `zzz`
19    /// string - we will get this error.
20    /// But if we try to parse a number list like `1.2 zzz`,
21    /// then we will get `InvalidNumber`, because at least some data is valid.
22    InvalidValue,
23
24    /// An invalid/unexpected character.
25    ///
26    /// The first byte is an actual one, others - expected.
27    ///
28    /// We are using a single value to reduce the struct size.
29    InvalidChar(Vec<u8>, usize),
30
31    /// An unexpected character instead of an XML space.
32    ///
33    /// The first string is an actual one, others - expected.
34    ///
35    /// We are using a single value to reduce the struct size.
36    InvalidString(Vec<String>, usize),
37
38    /// An invalid number.
39    InvalidNumber(usize),
40
41    /// A viewBox with a negative or zero size.
42    InvalidViewbox,
43}
44
45impl fmt::Display for Error {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        match *self {
48            Error::UnexpectedEndOfStream => {
49                write!(f, "unexpected end of stream")
50            }
51            Error::UnexpectedData(pos) => {
52                write!(f, "unexpected data at position {}", pos)
53            }
54            Error::InvalidValue => {
55                write!(f, "invalid value")
56            }
57            Error::InvalidChar(ref chars, pos) => {
58                // Vec<u8> -> Vec<String>
59                let list: Vec<String> = chars
60                    .iter()
61                    .skip(1)
62                    .map(|c| String::from_utf8(vec![*c]).unwrap())
63                    .collect();
64
65                write!(
66                    f,
67                    "expected '{}' not '{}' at position {}",
68                    list.join("', '"),
69                    chars[0] as char,
70                    pos
71                )
72            }
73            Error::InvalidString(ref strings, pos) => {
74                write!(
75                    f,
76                    "expected '{}' not '{}' at position {}",
77                    strings[1..].join("', '"),
78                    strings[0],
79                    pos
80                )
81            }
82            Error::InvalidNumber(pos) => {
83                write!(f, "invalid number at position {}", pos)
84            }
85            Error::InvalidViewbox => {
86                write!(f, "viewBox should have a positive size")
87            }
88        }
89    }
90}
91
92impl error::Error for Error {
93    fn description(&self) -> &str {
94        "an SVG data parsing error"
95    }
96}
97
98/// An alias to `Result<T, Error>`.
99pub(crate) type Result<T> = ::std::result::Result<T, Error>;