Skip to content
This repository has been archived by the owner on Apr 5, 2021. It is now read-only.

Commit

Permalink
impl std::error::Error for AppDirsError (closes #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
andybarron committed Oct 28, 2016
1 parent 17efb2e commit dadda6a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl AppDataType {
}
}

const ERR_NOT_SUPPORTED: &'static str = "App data directories not supported";
const ERR_INVALID_APP_INFO: &'static str = "Invalid app name or author";

/// Error type for any `app_dirs` operation.
#[derive(Debug)]
pub enum AppDirsError {
Expand All @@ -71,6 +74,36 @@ pub enum AppDirsError {
InvalidAppInfo,
}

impl std::fmt::Display for AppDirsError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
use AppDirsError::*;
match *self {
Io(ref e) => e.fmt(f),
NotSupported => f.write_str(ERR_NOT_SUPPORTED),
InvalidAppInfo => f.write_str(ERR_INVALID_APP_INFO),
}
}
}

impl std::error::Error for AppDirsError {
fn description(&self) -> &str {
use AppDirsError::*;
match *self {
Io(ref e) => e.description(),
NotSupported => "App data directories not supported",
InvalidAppInfo => "Invalid app name or author",
}
}
fn cause(&self) -> Option<&std::error::Error> {
use AppDirsError::*;
match *self {
Io(ref e) => Some(e),
NotSupported => None,
InvalidAppInfo => None,
}
}
}

impl From<std::io::Error> for AppDirsError {
fn from(e: std::io::Error) -> Self {
AppDirsError::Io(e)
Expand Down

0 comments on commit dadda6a

Please sign in to comment.