pub mod settings;
use std::fmt::{self, Display, Formatter};
use std::io;
use std::result;
use position::Pos;
pub use self::settings::SettingError;
use self::Error::{Msg, Parse, Setting};
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug, PartialEq)]
pub enum Error {
Msg(String),
Parse(ParseError),
Setting(SettingError),
}
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match *self {
Msg(ref msg) => write!(formatter, "{}", msg),
Parse(ref error) => write!(formatter, "{}", error),
Setting(ref error) => write!(formatter, "{}", error),
}
}
}
impl<'a> Into<Error> for &'a str {
fn into(self) -> Error {
Msg(self.to_string())
}
}
impl Into<Error> for io::Error {
fn into(self) -> Error {
Msg(self.to_string())
}
}
#[derive(Debug, PartialEq)]
pub enum ErrorType {
MissingArgument,
NoCommand,
Parse,
UnknownCommand,
}
#[derive(Debug, PartialEq)]
pub struct ParseError {
pub expected: String,
pos: Pos,
pub typ: ErrorType,
pub unexpected: String,
}
impl ParseError {
#[allow(unknown_lints, new_ret_no_self)]
pub fn new(typ: ErrorType, unexpected: String, expected: String, pos: Pos) -> Error {
Error::Parse(ParseError {
expected: expected,
pos: pos,
typ: typ,
unexpected: unexpected,
})
}
}
impl Display for ParseError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
write!(formatter, "unexpected {}, expecting {} on {}", self.unexpected, self.expected, self.pos)
}
}