Skip to content

Commit

Permalink
fix(DiskTokenStorage): make DiskTokenStorage::new() return a Result<>
Browse files Browse the repository at this point in the history
  • Loading branch information
dermesser committed Apr 25, 2016
1 parent 2cb5250 commit ae5e94b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,25 @@ pub struct DiskTokenStorage {
}

impl DiskTokenStorage {
pub fn new(location: &String) -> DiskTokenStorage {
pub fn new(location: &String) -> Result<DiskTokenStorage, io::Error> {
use std::fs;
let mut dts = DiskTokenStorage {
location: location.clone(),
tokens: HashMap::new(),
};

// best-effort
let _ = dts.load_from_file();
dts
let read_result = dts.load_from_file();

match read_result {
Result::Ok(()) => Result::Ok(dts),
Result::Err(e) => {
match e.kind() {
io::ErrorKind::NotFound => Result::Ok(dts), // File not found; ignore and create new one
_ => Result::Err(e), // e.g. PermissionDenied
}
}
}
}

fn load_from_file(&mut self) -> Result<(), io::Error> {
Expand Down

0 comments on commit ae5e94b

Please sign in to comment.