Skip to content

Commit

Permalink
Merge pull request #153 from sharkdp/error_handling
Browse files Browse the repository at this point in the history
Implement Error for LoadingError
  • Loading branch information
trishume authored Apr 30, 2018
2 parents ca699a8 + 2914e1d commit ac7e7c8
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub mod html;
mod escape;

use std::io::Error as IoError;
use std::error::Error;
use std::fmt;
#[cfg(all(feature = "yaml-load", feature = "parsing"))]
use parsing::ParseSyntaxError;
use highlighting::{ParseThemeError, SettingsError};
Expand Down Expand Up @@ -102,3 +104,41 @@ impl From<ParseSyntaxError> for LoadingError {
LoadingError::ParseSyntax(error)
}
}

impl fmt::Display for LoadingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use LoadingError::*;

match *self {
WalkDir(ref error) => error.fmt(f),
Io(ref error) => error.fmt(f),
_ => write!(f, "{}", self.description()),
}
}
}

impl Error for LoadingError {
fn description(&self) -> &str {
use LoadingError::*;

match *self {
WalkDir(ref error) => error.description(),
Io(ref error) => error.description(),
#[cfg(feature = "yaml-load")]
ParseSyntax(_) => "Invalid syntax file",
ParseTheme(_) => "Invalid syntax theme",
ReadSettings(_) => "Invalid syntax theme settings",
BadPath => "Invalid path",
}
}

fn cause(&self) -> Option<&Error> {
use LoadingError::*;

match *self {
WalkDir(ref error) => Some(error),
Io(ref error) => Some(error),
_ => None,
}
}
}

0 comments on commit ac7e7c8

Please sign in to comment.