Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ description = "Library to help with the XDG basedir spec"

[lib]
name = "xdg_basedir"

[dependencies]
error-chain = "^0.1"
6 changes: 3 additions & 3 deletions src/env_path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use error::*;
use error::{ErrorKind, Result};

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

/// Get an environment variable's value as a PathBuf.
Expand Down
68 changes: 11 additions & 57 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,22 @@
use std::convert::From;
use std::error;
use std::fmt;
use std::io;
use std::result;

pub type Result<T> = result::Result<T, Error>;

#[derive(Debug)]
pub struct Error {
error_kind: ErrorKind,
}

#[derive(Debug)]
pub enum ErrorKind {
Xdg(XdgError),
Io(io::Error)
}

#[derive(Debug)]
pub enum XdgError {
NoHomeDir,
InvalidPath,
IncorrectPermissions,
IncorrectOwner,
}

impl Error {
pub fn new(error_kind: ErrorKind) -> Error {
Error { error_kind: error_kind }
error_chain! {
types {
Error, ErrorKind, ChainErr, Result;
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.error_kind {
ErrorKind::Xdg(ref e) => write!(f, "Xdg error: {:?}", e),
ErrorKind::Io(ref e) => e.fmt(f),
}
links {
}
}

impl error::Error for Error {
fn description(&self) -> &str {
match self.error_kind {
ErrorKind::Xdg(_) => "Xdg error",
ErrorKind::Io(ref e) => e.description(),
}
foreign_links {
io::Error, Io, "IO error";
}
}

impl From<XdgError> for Error {
fn from(error: XdgError) -> Error {
Error { error_kind: ErrorKind::Xdg(error) }
}
}

impl From<io::Error> for Error {
fn from(error: io::Error) -> Error {
Error { error_kind: ErrorKind::Io(error) }
}
}

impl From<XdgError> for Result<()> {
fn from(error: XdgError) -> Result<()> {
Err(Error::from(error))
errors {
NoHomeDir {
description("Could not find home dir")
display("$HOME not defined or is not valid")
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
//! Alternate implementation and some initial source borrowed from [rust-xdg](https://github.com/o11c/rust-xdg).
//! The APIs provided by ```rust-xdg``` and ```xdg-basedir``` are different.

#[macro_use]
extern crate error_chain;

pub mod dirs;
pub mod error;

Expand Down