Skip to content

Commit 14968c0

Browse files
committed
use error_chain and more standard error types
1 parent 3b62867 commit 14968c0

File tree

4 files changed

+20
-60
lines changed

4 files changed

+20
-60
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ description = "Library to help with the XDG basedir spec"
1111

1212
[lib]
1313
name = "xdg_basedir"
14+
15+
[dependencies]
16+
error-chain = "^0.1"

src/env_path.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use error::*;
1+
use error::{ErrorKind, Result};
22

3-
use std::convert::From;
3+
use std::convert::Into;
44
use std::env::{home_dir, split_paths};
55
use std::ffi::OsString;
66
use std::path::PathBuf;
@@ -11,7 +11,7 @@ pub fn get_path_or_default<'a, F>(get_env_var: &'a F, env_var: &'a str, default:
1111
{
1212
get_path(get_env_var, env_var)
1313
.or(home_dir().map(|p| p.join(default)))
14-
.ok_or(Error::from(XdgError::NoHomeDir))
14+
.ok_or(ErrorKind::NoHomeDir.into())
1515
}
1616

1717
/// Get an environment variable's value as a PathBuf.

src/error.rs

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,22 @@
11
use std::convert::From;
2-
use std::error;
3-
use std::fmt;
42
use std::io;
5-
use std::result;
63

7-
pub type Result<T> = result::Result<T, Error>;
8-
9-
#[derive(Debug)]
10-
pub struct Error {
11-
error_kind: ErrorKind,
12-
}
13-
14-
#[derive(Debug)]
15-
pub enum ErrorKind {
16-
Xdg(XdgError),
17-
Io(io::Error)
18-
}
19-
20-
#[derive(Debug)]
21-
pub enum XdgError {
22-
NoHomeDir,
23-
InvalidPath,
24-
IncorrectPermissions,
25-
IncorrectOwner,
26-
}
27-
28-
impl Error {
29-
pub fn new(error_kind: ErrorKind) -> Error {
30-
Error { error_kind: error_kind }
4+
error_chain! {
5+
types {
6+
Error, ErrorKind, ChainErr, Result;
317
}
32-
}
338

34-
impl fmt::Display for Error {
35-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36-
match self.error_kind {
37-
ErrorKind::Xdg(ref e) => write!(f, "Xdg error: {:?}", e),
38-
ErrorKind::Io(ref e) => e.fmt(f),
39-
}
9+
links {
4010
}
41-
}
4211

43-
impl error::Error for Error {
44-
fn description(&self) -> &str {
45-
match self.error_kind {
46-
ErrorKind::Xdg(_) => "Xdg error",
47-
ErrorKind::Io(ref e) => e.description(),
48-
}
12+
foreign_links {
13+
io::Error, Io, "IO error";
4914
}
50-
}
51-
52-
impl From<XdgError> for Error {
53-
fn from(error: XdgError) -> Error {
54-
Error { error_kind: ErrorKind::Xdg(error) }
55-
}
56-
}
5715

58-
impl From<io::Error> for Error {
59-
fn from(error: io::Error) -> Error {
60-
Error { error_kind: ErrorKind::Io(error) }
61-
}
62-
}
63-
64-
impl From<XdgError> for Result<()> {
65-
fn from(error: XdgError) -> Result<()> {
66-
Err(Error::from(error))
16+
errors {
17+
NoHomeDir {
18+
description("Could not find home dir")
19+
display("$HOME not defined or is not valid")
20+
}
6721
}
6822
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
//! Alternate implementation and some initial source borrowed from [rust-xdg](https://github.com/o11c/rust-xdg).
1818
//! The APIs provided by ```rust-xdg``` and ```xdg-basedir``` are different.
1919
20+
#[macro_use]
21+
extern crate error_chain;
22+
2023
pub mod dirs;
2124
pub mod error;
2225

0 commit comments

Comments
 (0)