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
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Config {
|
|
|
|
pub http: ConfigHTTP,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct ConfigHTTP {
|
|
|
|
pub host: String,
|
|
|
|
pub port: u32,
|
|
|
|
pub cors: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)?)
|
|
|
|
}
|
|
|
|
}
|