HomeDisk/utils/src/config/parser.rs

40 lines
911 B
Rust
Raw Normal View History

2022-04-16 18:19:38 +00:00
use std::fs;
use serde::{Deserialize, Serialize};
2022-04-16 19:22:01 +00:00
use super::Error;
2022-04-16 18:19:38 +00:00
2022-04-19 13:14:17 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
2022-04-16 18:19:38 +00:00
pub struct Config {
pub http: ConfigHTTP,
2022-04-19 13:14:17 +00:00
pub jwt: ConfigJWT,
2022-04-16 18:19:38 +00:00
}
2022-04-19 13:14:17 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
2022-04-16 18:19:38 +00:00
pub struct ConfigHTTP {
pub host: String,
pub cors: Vec<String>,
2022-04-16 18:19:38 +00:00
}
2022-04-19 13:14:17 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigJWT {
pub secret: String,
}
2022-04-16 18:19:38 +00:00
impl Config {
/// parse configuration file
pub fn parse() -> Result<Config, Error> {
2022-04-16 19:22:01 +00:00
// configuration file path
2022-04-16 19:23:56 +00:00
let config_dir = if dirs::config_dir() == None {
return Err(Error::UnknowConfigDir());
} else {
dirs::config_dir().unwrap()
};
2022-04-16 19:22:01 +00:00
let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy());
let config = fs::read_to_string(config_path)?;
2022-04-16 18:19:38 +00:00
Ok(toml::from_str(&config)?)
}
}