utils (config): add custom error type

This commit is contained in:
MedzikUser 2022-04-16 21:22:01 +02:00
parent d469d105f1
commit 0099e93ad3
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
7 changed files with 83 additions and 25 deletions

44
Cargo.lock generated
View File

@ -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"

View File

@ -26,4 +26,4 @@ features = ["max_level_debug", "release_max_level_warn"]
[dependencies.homedisk-utils]
path = "../utils"
features = ["database"]
features = ["full"]

View File

@ -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() {

View File

@ -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"]

View File

@ -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<Config, Error> {
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)?)
}
}

30
utils/src/config/error.rs Normal file
View File

@ -0,0 +1,30 @@
use std::fmt;
#[derive(Debug)]
pub enum Error {
UnknowConfigDir(),
Io(std::io::Error),
Toml(toml::de::Error),
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}
impl From<toml::de::Error> 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),
}
}
}

4
utils/src/config/mod.rs Normal file
View File

@ -0,0 +1,4 @@
mod config;
mod error;
pub use {config::*, error::*};