Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Error for LoadingError #153

Merged
merged 2 commits into from
Apr 30, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 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,45 @@ 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),
#[cfg(feature = "yaml-load")]
ParseSyntax(_) => write!(f, "Invalid syntax file"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cases could be implemented as _ => write!(f, self.description()) instead of duplicating the messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, I didn't think it would work in this direction (because Error depends on fmt::Display).. but that's much better, of course. Updated.

ParseTheme(_) => write!(f, "Invalid syntax theme"),
ReadSettings(_) => write!(f, "Invalid syntax theme settings"),
BadPath => write!(f, "Invalid path"),
}
}
}

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,
}
}
}