mg_settings/errors/
mod.rs

1/*
2 * Copyright (c) 2017 Boucher, Antoni <bouanto@zoho.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22//! Parse and io error type.
23
24pub mod settings;
25
26use std::fmt::{self, Display, Formatter};
27use std::io;
28use std::result;
29
30use position::Pos;
31pub use self::settings::SettingError;
32use self::Error::{Msg, Parse, Setting};
33
34/// Parser result type.
35pub type Result<T> = result::Result<T, Error>;
36
37#[derive(Debug, PartialEq)]
38/// Parser or setting error.
39pub enum Error {
40    /// Other errors like input/output error.
41    Msg(String),
42    /// Parse error.
43    Parse(ParseError),
44    /// Error when getting/setting settings.
45    Setting(SettingError),
46}
47
48impl Display for Error {
49    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
50        match *self {
51            Msg(ref msg) => write!(formatter, "{}", msg),
52            Parse(ref error) => write!(formatter, "{}", error),
53            Setting(ref error) => write!(formatter, "{}", error),
54        }
55    }
56}
57
58impl<'a> Into<Error> for &'a str {
59    fn into(self) -> Error {
60        Msg(self.to_string())
61    }
62}
63
64impl Into<Error> for io::Error {
65    fn into(self) -> Error {
66        Msg(self.to_string())
67    }
68}
69
70/// A set of error types that can occur parsing the settings file.
71#[derive(Debug, PartialEq)]
72pub enum ErrorType {
73    /// A missing argument.
74    MissingArgument,
75    /// No command (or a comment) was entered.
76    NoCommand,
77    /// Parse error.
78    Parse,
79    /// Unknown command.
80    UnknownCommand,
81}
82
83/// Struct which holds information about an error at a specific position.
84#[derive(Debug, PartialEq)]
85pub struct ParseError {
86    /// The expected token.
87    pub expected: String,
88    pos: Pos,
89    /// The error type.
90    pub typ: ErrorType,
91    /// The unexpected token.
92    pub unexpected: String,
93}
94
95impl ParseError {
96    /// Create a new error.
97    #[allow(unknown_lints, new_ret_no_self)]
98    pub fn new(typ: ErrorType, unexpected: String, expected: String, pos: Pos) -> Error {
99        Error::Parse(ParseError {
100            expected: expected,
101            pos: pos,
102            typ: typ,
103            unexpected: unexpected,
104        })
105    }
106}
107
108impl Display for ParseError {
109    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
110        write!(formatter, "unexpected {}, expecting {} on {}", self.unexpected, self.expected, self.pos)
111    }
112}