1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Errors and result types.
//!
//! Types in this module will be used among multiple versions of parsers.

use std::{error, fmt, io};

use crate::pull_parser::SyntacticPosition;

pub use self::{
    data::{Compression, DataError},
    operation::OperationError,
    warning::Warning,
};

mod data;
mod operation;
mod warning;

/// Parsing result.
pub type Result<T> = std::result::Result<T, Error>;

/// Parsing error.
#[derive(Debug)]
pub struct Error {
    /// The real error.
    repr: Box<Repr>,
}

impl Error {
    /// Returns the error kind.
    pub fn kind(&self) -> ErrorKind {
        self.repr.error.kind()
    }

    /// Returns a reference to the inner error container.
    pub fn get_ref(&self) -> &ErrorContainer {
        &self.repr.error
    }

    /// Returns a reference to the inner error if the type matches.
    pub fn downcast_ref<T: 'static + error::Error>(&self) -> Option<&T> {
        self.repr.error.as_error().downcast_ref::<T>()
    }

    /// Returns the syntactic position if available.
    pub fn position(&self) -> Option<&SyntacticPosition> {
        self.repr.position.as_ref()
    }

    /// Creates a new `Error` with the given syntactic position info.
    pub(crate) fn with_position(error: ErrorContainer, position: SyntacticPosition) -> Self {
        Self {
            repr: Box::new(Repr::with_position(error, position)),
        }
    }

    /// Sets the syntactic position and returns the new error.
    pub(crate) fn and_position(mut self, position: SyntacticPosition) -> Self {
        self.repr.position = Some(position);
        self
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.repr.error.fmt(f)
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        self.repr.error.source()
    }
}

impl<T> From<T> for Error
where
    T: Into<ErrorContainer>,
{
    fn from(e: T) -> Self {
        Error {
            repr: Box::new(Repr::new(e.into())),
        }
    }
}

/// Internal representation of parsing error.
#[derive(Debug)]
struct Repr {
    /// Error.
    error: ErrorContainer,
    /// Syntactic position.
    position: Option<SyntacticPosition>,
}

impl Repr {
    /// Creates a new `Repr`.
    pub(crate) fn new(error: ErrorContainer) -> Self {
        Self {
            error,
            position: None,
        }
    }

    /// Creates a new `Repr` with the given syntactic position info.
    pub(crate) fn with_position(error: ErrorContainer, position: SyntacticPosition) -> Self {
        Self {
            error,
            position: Some(position),
        }
    }
}

/// Error kind for parsing errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorKind {
    /// Invalid data.
    ///
    /// With this error kind, the inner error must be [`DataError`].
    ///
    /// [`DataError`]: enum.DataError.html
    Data,
    /// I/O error.
    ///
    /// With this error kind, the inner error must be [`std::io::Error`].
    ///
    /// [`std::io::Error`]:
    /// https://doc.rust-lang.org/stable/std/io/struct.Error.html
    Io,
    /// Invalid operation.
    ///
    /// With this error kind, the inner error must be [`OperationError`].
    ///
    /// [`OperationError`]: enum.OperationError.html
    Operation,
    /// Critical warning.
    ///
    /// With this error kind, the inner error must be [`Warning`].
    ///
    /// [`Warning`]: enum.Warning.html
    Warning,
}

/// Parsing error container.
#[derive(Debug)]
pub enum ErrorContainer {
    /// Invalid data.
    Data(DataError),
    /// I/O error.
    Io(io::Error),
    /// Invalid operation.
    Operation(OperationError),
    /// Critical warning.
    Warning(Warning),
}

impl ErrorContainer {
    /// Returns the error kind of the error.
    pub fn kind(&self) -> ErrorKind {
        match self {
            ErrorContainer::Data(_) => ErrorKind::Data,
            ErrorContainer::Io(_) => ErrorKind::Io,
            ErrorContainer::Operation(_) => ErrorKind::Operation,
            ErrorContainer::Warning(_) => ErrorKind::Warning,
        }
    }

    /// Returns `&dyn std::error::Error`.
    pub fn as_error(&self) -> &(dyn 'static + error::Error) {
        match self {
            ErrorContainer::Data(e) => e,
            ErrorContainer::Io(e) => e,
            ErrorContainer::Operation(e) => e,
            ErrorContainer::Warning(e) => e,
        }
    }
}

impl error::Error for ErrorContainer {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        Some(self.as_error())
    }
}

impl fmt::Display for ErrorContainer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ErrorContainer::Data(e) => write!(f, "Data error: {}", e),
            ErrorContainer::Io(e) => write!(f, "I/O error: {}", e),
            ErrorContainer::Operation(e) => write!(f, "Invalid operation: {}", e),
            ErrorContainer::Warning(e) => write!(f, "Warning considered critical: {}", e),
        }
    }
}

impl From<io::Error> for ErrorContainer {
    fn from(e: io::Error) -> Self {
        ErrorContainer::Io(e)
    }
}

impl From<DataError> for ErrorContainer {
    fn from(e: DataError) -> Self {
        ErrorContainer::Data(e)
    }
}

impl From<OperationError> for ErrorContainer {
    fn from(e: OperationError) -> Self {
        ErrorContainer::Operation(e)
    }
}

impl From<Warning> for ErrorContainer {
    fn from(e: Warning) -> Self {
        ErrorContainer::Warning(e)
    }
}