utils (config): add custom error type
This commit is contained in:
parent
d469d105f1
commit
0099e93ad3
|
@ -34,17 +34,6 @@ version = "1.0.56"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27"
|
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]]
|
[[package]]
|
||||||
name = "atoi"
|
name = "atoi"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
@ -213,6 +202,26 @@ dependencies = [
|
||||||
"crypto-common",
|
"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]]
|
[[package]]
|
||||||
name = "dotenv"
|
name = "dotenv"
|
||||||
version = "0.15.0"
|
version = "0.15.0"
|
||||||
|
@ -407,7 +416,7 @@ name = "homedisk-utils"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"dirs",
|
||||||
"hex",
|
"hex",
|
||||||
"serde",
|
"serde",
|
||||||
"sha-1",
|
"sha-1",
|
||||||
|
@ -741,6 +750,17 @@ dependencies = [
|
||||||
"bitflags",
|
"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]]
|
[[package]]
|
||||||
name = "ring"
|
name = "ring"
|
||||||
version = "0.16.20"
|
version = "0.16.20"
|
||||||
|
|
|
@ -26,4 +26,4 @@ features = ["max_level_debug", "release_max_level_warn"]
|
||||||
|
|
||||||
[dependencies.homedisk-utils]
|
[dependencies.homedisk-utils]
|
||||||
path = "../utils"
|
path = "../utils"
|
||||||
features = ["database"]
|
features = ["full"]
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
use homedisk_utils::database;
|
use homedisk_utils::{database::Database, config::Config};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
init();
|
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() {
|
fn init() {
|
||||||
|
|
|
@ -4,10 +4,10 @@ version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["crypto", "config", "database"]
|
full = ["crypto", "config", "database"]
|
||||||
crypto = ["sha-1", "sha2", "hex"]
|
crypto = ["sha-1", "sha2", "hex"]
|
||||||
config = ["toml", "anyhow"]
|
config = ["toml", "anyhow", "dirs"]
|
||||||
database = ["async-trait", "sqlx"]
|
database = ["sqlx"]
|
||||||
|
|
||||||
[dependencies.serde]
|
[dependencies.serde]
|
||||||
version = "1.0.136"
|
version = "1.0.136"
|
||||||
|
@ -31,11 +31,11 @@ optional = true
|
||||||
[dependencies.anyhow]
|
[dependencies.anyhow]
|
||||||
version = "1.0.56"
|
version = "1.0.56"
|
||||||
optional = true
|
optional = true
|
||||||
|
[dependencies.dirs]
|
||||||
|
version = "4.0.0"
|
||||||
|
optional = true
|
||||||
|
|
||||||
# database
|
# database
|
||||||
[dependencies.async-trait]
|
|
||||||
version = "0.1.53"
|
|
||||||
optional = true
|
|
||||||
[dependencies.sqlx]
|
[dependencies.sqlx]
|
||||||
version = "0.5.13"
|
version = "0.5.13"
|
||||||
features = ["runtime-tokio-rustls", "sqlite"]
|
features = ["runtime-tokio-rustls", "sqlite"]
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use anyhow::Error;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
// configuration file path
|
use super::Error;
|
||||||
static CONFIG_PATH: &str = "config.toml";
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
@ -21,7 +19,11 @@ pub struct ConfigHTTP {
|
||||||
impl Config {
|
impl Config {
|
||||||
/// parse configuration file
|
/// parse configuration file
|
||||||
pub fn parse() -> Result<Config, Error> {
|
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)?)
|
Ok(toml::from_str(&config)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
mod config;
|
||||||
|
mod error;
|
||||||
|
|
||||||
|
pub use {config::*, error::*};
|
Loading…
Reference in New Issue