crowbook_intl/
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
3// this file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::error;
6use std::result;
7use std::fmt;
8
9/// Internal ErrorType
10#[derive(Debug, PartialEq)]
11enum ErrorType {
12    Default,
13    Parse,
14}
15
16/// Result type (returned by most methods of this library)
17pub type Result<T> = result::Result<T, Error>;
18
19#[derive(Debug, PartialEq)]
20/// Error type returned by methods of this library
21pub struct Error {
22    msg: String,
23    variant: ErrorType
24}
25
26impl Error {
27    /// Creates a new default error
28    pub fn new<S: Into<String>>(msg: S) -> Error {
29        Error {
30            msg: msg.into(),
31            variant: ErrorType::Default,
32        }
33    }
34
35    /// Creates a new parse error
36    pub fn parse<S: Into<String>>(msg: S) -> Error {
37        Error {
38            msg: msg.into(),
39            variant: ErrorType::Parse,
40        }
41    }
42}
43
44impl error::Error for Error {
45    fn description(&self) -> &str {
46        &self.msg
47    }
48}
49
50impl fmt::Display for Error {
51    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52        match self.variant {
53            ErrorType::Parse => write!(f, "Error parsing localization file: {}", self.msg),
54            _ => write!(f, "{}", self.msg),
55        }
56    }
57}
58