add probably really terrible error handling
This commit is contained in:
parent
b6d0359f6c
commit
7315c2b154
2 changed files with 61 additions and 2 deletions
19
src/error.rs
Normal file
19
src/error.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use std::fmt;
|
||||
|
||||
pub struct Error {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
// Implement std::fmt::Display for Error
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.message) // user-facing output
|
||||
}
|
||||
}
|
||||
|
||||
// Implement std::fmt::Debug for Error
|
||||
impl fmt::Debug for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Error message: {}\n{{ file: {}, line: {} }}", self.message, file!(), line!()) // programmer-facing output
|
||||
}
|
||||
}
|
44
src/main.rs
44
src/main.rs
|
@ -1,8 +1,11 @@
|
|||
use clap::Parser;
|
||||
use std::{path::PathBuf, process::exit};
|
||||
use data::Config;
|
||||
use error::Error;
|
||||
use std::{fs::File, path::PathBuf, process::exit};
|
||||
mod build;
|
||||
mod cli;
|
||||
mod data;
|
||||
mod error;
|
||||
|
||||
fn main() {
|
||||
build::completion_builder().unwrap();
|
||||
|
@ -16,5 +19,42 @@ fn main() {
|
|||
exit(0);
|
||||
}
|
||||
|
||||
println!("Config path: {}", config_path.to_str().unwrap())
|
||||
println!("Config path: {}", config_path.to_str().unwrap());
|
||||
|
||||
let config: Config;
|
||||
|
||||
match get_config_from_path(config_path) {
|
||||
Ok(conf) => {
|
||||
config = conf;
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", e);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
// = get_config_from_path(config_path);
|
||||
|
||||
println!("{:?}", config);
|
||||
}
|
||||
|
||||
fn get_config_from_path(path: PathBuf) -> Result<Config, Error> {
|
||||
match File::open(&path) {
|
||||
Ok(f) => match serde_yml::from_reader(f) {
|
||||
Ok(conf) => return Ok(conf),
|
||||
Err(e) => {
|
||||
return Err(Error {
|
||||
message: format!(
|
||||
"*** Error deserializing config file ({:?}), it's likely deformed ***\n{}",
|
||||
path, e
|
||||
),
|
||||
});
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!("");
|
||||
return Err(Error {
|
||||
message: format!("*** Error opening config file ({:?}) ***\n{}", path, e),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue