azul_simplecss/
error.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use std::fmt;
6
7/// Position of an error.
8///
9/// Position indicates row/line and column. Starting positions is 1:1.
10#[derive(Clone,Copy,PartialEq)]
11pub struct ErrorPos {
12    #[allow(missing_docs)]
13    pub row: usize,
14    #[allow(missing_docs)]
15    pub col: usize,
16}
17
18impl ErrorPos {
19    /// Constructs a new error position.
20    pub fn new(row: usize, col: usize) -> ErrorPos {
21        ErrorPos {
22            row: row,
23            col: col,
24        }
25    }
26}
27
28impl fmt::Debug for ErrorPos {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        write!(f, "{}:{}", &self.row, &self.col)
31    }
32}
33
34/// List of all supported errors.
35#[derive(Clone,Copy,PartialEq)]
36pub enum Error {
37    /// The steam ended earlier than we expected.
38    ///
39    /// Should only appear on invalid input data.
40    UnexpectedEndOfStream(ErrorPos),
41    /// Can appear during moving along the data stream.
42    InvalidAdvance {
43        /// The advance step.
44        expected: isize,
45        /// Full length of the steam.
46        total: usize,
47        /// Absolute stream position.
48        pos: ErrorPos,
49    },
50    /// Unsupported token.
51    UnsupportedToken(ErrorPos),
52    /// Unknown token.
53    UnknownToken(ErrorPos),
54}
55
56impl fmt::Debug for Error {
57
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match *self {
60            Error::UnexpectedEndOfStream(ref pos) =>
61                write!(f, "Unexpected end of stream at {:?}", pos),
62            Error::InvalidAdvance{ref expected, ref total, ref pos} =>
63                write!(f, "Attempt to advance to the pos {} from {:?}, but total len is {}",
64                       expected, pos, total),
65            Error::UnsupportedToken(ref pos) =>
66                write!(f, "Unsupported token at {:?}", pos),
67            Error::UnknownToken(ref pos) =>
68                write!(f, "Unknown token at {:?}", pos),
69        }
70    }
71}