Don't use expect
This commit is contained in:
parent
6d8c959d9e
commit
14e103e04c
44
src/main.rs
44
src/main.rs
|
@ -24,6 +24,7 @@ use colored::Colorize;
|
||||||
use crate::parsers::slice;
|
use crate::parsers::slice;
|
||||||
|
|
||||||
const AUTHOR: &str = "Daniele Tricoli <eriol@mornie.org>";
|
const AUTHOR: &str = "Daniele Tricoli <eriol@mornie.org>";
|
||||||
|
const ERROR_PARSING: &str = "An error occurred while parsing:";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = App::new("conf-form")
|
let matches = App::new("conf-form")
|
||||||
|
@ -48,32 +49,26 @@ fn main() {
|
||||||
.value_name("FILE"),
|
.value_name("FILE"),
|
||||||
).get_matches();
|
).get_matches();
|
||||||
|
|
||||||
let config_file = fs::read_to_string(matches.value_of("config").unwrap())
|
// We can unwrap because config is required.
|
||||||
.expect("Can't read the configuration file.");
|
let config_file = matches.value_of("config").unwrap();
|
||||||
|
let config = read_file(config_file);
|
||||||
|
|
||||||
let mut config = match slice::parse(&config_file) {
|
let mut config = match slice::parse(&config) {
|
||||||
Ok(config) => config,
|
Ok(config) => config,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!(
|
println!("{}: {}:\n{}", ERROR_PARSING.red().bold(), &config_file, err);
|
||||||
"{}:\n{}",
|
|
||||||
"An error occurred parsing configuration file:".red().bold(),
|
|
||||||
err
|
|
||||||
);
|
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let profile_file = fs::read_to_string(matches.value_of("profile").unwrap())
|
// We can unwrap because profile is required.
|
||||||
.expect("Can't read the profile file.");
|
let profile_file = matches.value_of("profile").unwrap();
|
||||||
|
let profile = read_file(profile_file);
|
||||||
|
|
||||||
let profile: BTreeMap<String, String> = match serde_yaml::from_str(&profile_file) {
|
let profile: BTreeMap<String, String> = match serde_yaml::from_str(&profile) {
|
||||||
Ok(profile) => profile,
|
Ok(profile) => profile,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!(
|
println!("{}: {}: {}", ERROR_PARSING.red().bold(), &profile_file, err);
|
||||||
"{}: {}",
|
|
||||||
"An error occurred parsing profile file:".red().bold(),
|
|
||||||
err
|
|
||||||
);
|
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -82,3 +77,20 @@ fn main() {
|
||||||
|
|
||||||
config.print();
|
config.print();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read the content of a file and return it as String.
|
||||||
|
// In case of errors exit the process with return code 1.
|
||||||
|
fn read_file(f: &str) -> String {
|
||||||
|
match fs::read_to_string(&f) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(err) => {
|
||||||
|
println!(
|
||||||
|
"{} {}: {}",
|
||||||
|
"An error occurred while reading:".red().bold(),
|
||||||
|
&f,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue