feat: validate configuration

This commit is contained in:
Marc Plano-Lesay 2025-05-21 22:02:42 +10:00
parent ee7d7971be
commit 48c670f455
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
33 changed files with 1076 additions and 165 deletions

47
src/errors.rs Normal file
View file

@ -0,0 +1,47 @@
use color_eyre::section::Section;
use std::fmt;
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum ErrorCategory {
Database,
Config,
Reddit,
Action,
Notification,
IO,
Unknown,
}
impl fmt::Display for ErrorCategory {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorCategory::Database => write!(f, "Database error"),
ErrorCategory::Config => write!(f, "Configuration error"),
ErrorCategory::Reddit => write!(f, "Reddit API error"),
ErrorCategory::Action => write!(f, "Action error"),
ErrorCategory::Notification => write!(f, "Notification error"),
ErrorCategory::IO => write!(f, "IO error"),
ErrorCategory::Unknown => write!(f, "Unknown error"),
}
}
}
#[macro_export]
macro_rules! define_error_fn {
($fn_name:ident, $category:expr) => {
/// Helper function to create an error with context
#[allow(dead_code)]
pub fn $fn_name<E: std::fmt::Display>(error: E, context: &str) -> color_eyre::eyre::Report {
color_eyre::eyre::eyre!("{}: {}", context, error).with_section(|| $category.to_string())
}
};
}
// Define all error helper functions using the macro
define_error_fn!(db_error, ErrorCategory::Database);
define_error_fn!(config_error, ErrorCategory::Config);
define_error_fn!(reddit_error, ErrorCategory::Reddit);
define_error_fn!(action_error, ErrorCategory::Action);
define_error_fn!(notification_error, ErrorCategory::Notification);
define_error_fn!(io_error, ErrorCategory::IO);