use thiserror for impl Error and Display

This commit is contained in:
Namekuji 2023-05-13 05:22:28 -04:00
parent 31c425995f
commit e1a0ab7d25
4 changed files with 37 additions and 43 deletions

View file

@ -23,5 +23,9 @@ queue = { path = "crates/queue" }
config = { path = "crates/config" }
macros = { path = "crates/macros" }
lazy_static = "1.4.0"
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
tokio = { version = "1.28.1", features = ["full"] }
anyhow = "1.0.71"
[dev-dependencies]

View file

@ -9,4 +9,5 @@ edition = "2021"
once_cell = "1.17.1"
serde = { version = "1.0.160", features = [ "derive" ] }
serde_yaml = "0.9.21"
thiserror = "1.0.40"

View file

@ -1,4 +1,3 @@
use std::fmt::Display;
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;
@ -11,44 +10,16 @@ mod data;
pub use data::*;
// Config Errors
#[derive(Debug)]
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("The configuration has not been initialized yet")]
Uninitialized,
Deserialize(serde_yaml::Error),
FileError(io::Error),
#[error("Error when parsing config file: {0}")]
Deserialize(#[from] serde_yaml::Error),
#[error("Error when reading config file: {0}")]
FileError(#[from] io::Error),
}
macro_rules! generate_error_impl {
($t:ident, $o:ty) => {
impl From<$o> for Error {
fn from(value: $o) -> Self {
Self::$t(value)
}
}
};
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Error::*;
f.write_str(&{
match self {
Uninitialized => {
format!("The configuration has not been initialized yet: {:?}", self)
}
Deserialize(e) => format!("Error when parsing config file: {}", e),
FileError(e) => format!("Error when reading config file: {}", e),
}
})
}
}
generate_error_impl!(FileError, io::Error);
generate_error_impl!(Deserialize, serde_yaml::Error);
impl std::error::Error for Error {}
// Functions
fn fetch_config(path: &Path) -> Result<Config, Error> {
let mut buf = String::new();

View file

@ -1,12 +1,14 @@
use std::{
env, error, fmt,
env, fmt,
path::{Path, PathBuf},
};
use tracing::debug;
#[macro_use]
extern crate macros;
fn main() -> Result<(), Box<dyn error::Error>> {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env::set_var(
"CK_REPO_DIR",
PathBuf::from(env!("PWD"))
@ -14,17 +16,33 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.and_then(|p| p.parent())
.ok_or(fmt::Error)?,
);
// logging
let is_debug = match env::var_os("NODE_ENV") {
None => true,
Some(val) => val != "production",
};
let subscriber = tracing_subscriber::fmt();
if is_debug {
subscriber
.with_max_level(tracing::Level::DEBUG)
.pretty()
.init();
} else {
subscriber.with_max_level(tracing::Level::INFO).init();
}
// bootstrap
// ENV
// get config
config::init_config(
&Path::new(&env::var("CK_REPO_DIR")?)
let config_path = &Path::new(&env::var("CK_REPO_DIR")?)
.join(".config")
.join("default.yml"),
)?;
.join("default.yml");
debug!(target: "config", path = ?config_path, "Loading yaml file");
config::init_config(config_path)?;
eprintln!("{:?}", config::get_config()?);