From 7315c2b154f46b727110196ee66d0312182da7bb Mon Sep 17 00:00:00 2001 From: askiiart Date: Thu, 15 May 2025 21:56:43 -0500 Subject: [PATCH] add probably really terrible error handling --- src/error.rs | 19 +++++++++++++++++++ src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..eecaba5 --- /dev/null +++ b/src/error.rs @@ -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 + } +} diff --git a/src/main.rs b/src/main.rs index b841440..871c4b5 100644 --- a/src/main.rs +++ b/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 { + 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), + }); + } + } }