From 0099e93ad352c12cf013901d0d200e606d295993 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Sat, 16 Apr 2022 21:22:01 +0200 Subject: [PATCH] utils (config): add custom error type --- Cargo.lock | 44 +++++++++++++++++++++++--------- core/Cargo.toml | 2 +- core/src/main.rs | 6 +++-- utils/Cargo.toml | 12 ++++----- utils/src/{ => config}/config.rs | 10 +++++--- utils/src/config/error.rs | 30 ++++++++++++++++++++++ utils/src/config/mod.rs | 4 +++ 7 files changed, 83 insertions(+), 25 deletions(-) rename utils/src/{ => config}/config.rs (55%) create mode 100644 utils/src/config/error.rs create mode 100644 utils/src/config/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 6e0a9f7..ee925f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,17 +34,6 @@ version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27" -[[package]] -name = "async-trait" -version = "0.1.53" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed6aa3524a2dfcf9fe180c51eae2b58738348d819517ceadf95789c51fff7600" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "atoi" version = "0.4.0" @@ -213,6 +202,26 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dotenv" version = "0.15.0" @@ -407,7 +416,7 @@ name = "homedisk-utils" version = "0.0.0" dependencies = [ "anyhow", - "async-trait", + "dirs", "hex", "serde", "sha-1", @@ -741,6 +750,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "ring" version = "0.16.20" diff --git a/core/Cargo.toml b/core/Cargo.toml index 8021bb9..62f8a9e 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -26,4 +26,4 @@ features = ["max_level_debug", "release_max_level_warn"] [dependencies.homedisk-utils] path = "../utils" -features = ["database"] +features = ["full"] diff --git a/core/src/main.rs b/core/src/main.rs index b009fdd..bd57cc3 100644 --- a/core/src/main.rs +++ b/core/src/main.rs @@ -1,10 +1,12 @@ -use homedisk_utils::database; +use homedisk_utils::{database::Database, config::Config}; #[tokio::main] async fn main() { init(); - database::Database::connect().await.unwrap(); + let _config = Config::parse().expect("parse configuration file"); + + let _db = Database::connect().await.expect("open SQLite database"); } fn init() { diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 511428c..57e2e52 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -4,10 +4,10 @@ version = "0.0.0" edition = "2021" [features] -default = ["crypto", "config", "database"] +full = ["crypto", "config", "database"] crypto = ["sha-1", "sha2", "hex"] -config = ["toml", "anyhow"] -database = ["async-trait", "sqlx"] +config = ["toml", "anyhow", "dirs"] +database = ["sqlx"] [dependencies.serde] version = "1.0.136" @@ -31,11 +31,11 @@ optional = true [dependencies.anyhow] version = "1.0.56" optional = true +[dependencies.dirs] +version = "4.0.0" +optional = true # database -[dependencies.async-trait] -version = "0.1.53" -optional = true [dependencies.sqlx] version = "0.5.13" features = ["runtime-tokio-rustls", "sqlite"] diff --git a/utils/src/config.rs b/utils/src/config/config.rs similarity index 55% rename from utils/src/config.rs rename to utils/src/config/config.rs index 437fb30..6ba75c8 100644 --- a/utils/src/config.rs +++ b/utils/src/config/config.rs @@ -1,10 +1,8 @@ use std::fs; -use anyhow::Error; use serde::{Deserialize, Serialize}; -// configuration file path -static CONFIG_PATH: &str = "config.toml"; +use super::Error; #[derive(Debug, Serialize, Deserialize)] pub struct Config { @@ -21,7 +19,11 @@ pub struct ConfigHTTP { impl Config { /// parse configuration file pub fn parse() -> Result { - let config = fs::read_to_string(CONFIG_PATH)?; + // configuration file path + let config_dir = if dirs::config_dir() == None { return Err(Error::UnknowConfigDir()) } else { dirs::config_dir().unwrap() }; + let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy()); + + let config = fs::read_to_string(config_path)?; Ok(toml::from_str(&config)?) } } diff --git a/utils/src/config/error.rs b/utils/src/config/error.rs new file mode 100644 index 0000000..1185aec --- /dev/null +++ b/utils/src/config/error.rs @@ -0,0 +1,30 @@ +use std::fmt; + +#[derive(Debug)] +pub enum Error { + UnknowConfigDir(), + Io(std::io::Error), + Toml(toml::de::Error), +} + +impl From for Error { + fn from(err: std::io::Error) -> Self { + Error::Io(err) + } +} + +impl From for Error { + fn from(err: toml::de::Error) -> Self { + Error::Toml(err) + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::UnknowConfigDir() => write!(f, "failed to lock up a system configuration directory"), + Error::Io(err) => write!(f, "{}", err), + Error::Toml(err) => write!(f, "{}", err), + } + } +} diff --git a/utils/src/config/mod.rs b/utils/src/config/mod.rs new file mode 100644 index 0000000..0fdc1b5 --- /dev/null +++ b/utils/src/config/mod.rs @@ -0,0 +1,4 @@ +mod config; +mod error; + +pub use {config::*, error::*};