Skip to content

Commit

Permalink
Improve error message when template or parameter file is not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Mar 3, 2018
1 parent fea020d commit 908c215
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate ktmpl;
use std::collections::{HashMap};
use std::error::Error;
use std::fs::File;
use std::io::{Read, stdin};
use std::io::{ErrorKind, Read, stdin};
use std::process::exit;

use clap::{App, AppSettings, Arg, Values};
Expand Down Expand Up @@ -113,7 +113,12 @@ fn real_main() -> Result<(), String> {
if filename == "-" {
stdin().read_to_string(&mut template_data).map_err(|err| err.description().to_owned())?;
} else {
let mut file = File::open(filename).map_err(|err| err.description().to_owned())?;
let mut file = File::open(filename).map_err(|err| {
match err.kind() {
ErrorKind::NotFound => format!("File not found: {}", filename),
_ => err.description().to_owned(),
}
})?;
file.read_to_string(&mut template_data).map_err(|err| err.description().to_owned())?;
}

Expand Down
9 changes: 7 additions & 2 deletions src/parameter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::Read;
use std::io::{ErrorKind, Read};
use std::str::FromStr;

use base64::encode;
Expand Down Expand Up @@ -41,7 +41,12 @@ pub type ParameterValues = HashMap<String, ParameterValue>;

/// Loads `ParameterValues` from a file.
pub fn parameter_values_from_file(file_path: &str) -> Result<ParameterValues, String> {
let mut file = File::open(file_path).map_err(|err| err.description().to_owned())?;
let mut file = File::open(file_path).map_err(|err| {
match err.kind() {
ErrorKind::NotFound => format!("File not found: {}", file_path),
_ => err.description().to_owned(),
}
})?;

let mut contents = String::new();
file.read_to_string(&mut contents).map_err(|err| err.description().to_owned())?;
Expand Down

0 comments on commit 908c215

Please sign in to comment.