use thiserror for impl Error and Display
This commit is contained in:
parent
31c425995f
commit
e1a0ab7d25
4 changed files with 37 additions and 43 deletions
|
@ -23,5 +23,9 @@ queue = { path = "crates/queue" }
|
||||||
config = { path = "crates/config" }
|
config = { path = "crates/config" }
|
||||||
macros = { path = "crates/macros" }
|
macros = { path = "crates/macros" }
|
||||||
lazy_static = "1.4.0"
|
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]
|
[dev-dependencies]
|
||||||
|
|
|
@ -9,4 +9,5 @@ edition = "2021"
|
||||||
once_cell = "1.17.1"
|
once_cell = "1.17.1"
|
||||||
serde = { version = "1.0.160", features = [ "derive" ] }
|
serde = { version = "1.0.160", features = [ "derive" ] }
|
||||||
serde_yaml = "0.9.21"
|
serde_yaml = "0.9.21"
|
||||||
|
thiserror = "1.0.40"
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use std::fmt::Display;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -11,44 +10,16 @@ mod data;
|
||||||
pub use data::*;
|
pub use data::*;
|
||||||
|
|
||||||
// Config Errors
|
// Config Errors
|
||||||
#[derive(Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
#[error("The configuration has not been initialized yet")]
|
||||||
Uninitialized,
|
Uninitialized,
|
||||||
Deserialize(serde_yaml::Error),
|
#[error("Error when parsing config file: {0}")]
|
||||||
FileError(io::Error),
|
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
|
// Functions
|
||||||
fn fetch_config(path: &Path) -> Result<Config, Error> {
|
fn fetch_config(path: &Path) -> Result<Config, Error> {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
use std::{
|
use std::{
|
||||||
env, error, fmt,
|
env, fmt,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
use tracing::debug;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate macros;
|
extern crate macros;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
env::set_var(
|
env::set_var(
|
||||||
"CK_REPO_DIR",
|
"CK_REPO_DIR",
|
||||||
PathBuf::from(env!("PWD"))
|
PathBuf::from(env!("PWD"))
|
||||||
|
@ -14,17 +16,33 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
.and_then(|p| p.parent())
|
.and_then(|p| p.parent())
|
||||||
.ok_or(fmt::Error)?,
|
.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
|
// bootstrap
|
||||||
|
|
||||||
// ENV
|
// ENV
|
||||||
|
|
||||||
// get config
|
// get config
|
||||||
|
|
||||||
config::init_config(
|
let config_path = &Path::new(&env::var("CK_REPO_DIR")?)
|
||||||
&Path::new(&env::var("CK_REPO_DIR")?)
|
|
||||||
.join(".config")
|
.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()?);
|
eprintln!("{:?}", config::get_config()?);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue