2022-04-19 19:10:36 +00:00
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
use crate::option_return;
|
|
|
|
|
|
|
|
use super::types::Config;
|
|
|
|
|
|
|
|
impl Config {
|
2022-06-07 20:36:26 +00:00
|
|
|
/// Parse configuration file
|
2022-06-08 19:59:21 +00:00
|
|
|
/// ```ignore,rust
|
2022-06-08 19:16:12 +00:00
|
|
|
/// use homedisk_types::config::Config;
|
|
|
|
///
|
|
|
|
/// let config = Config::parse().unwrap();
|
|
|
|
/// ```
|
2022-04-19 19:10:36 +00:00
|
|
|
pub fn parse() -> Result<Config> {
|
2022-06-08 22:02:20 +00:00
|
|
|
// get file path of config file
|
2022-06-08 19:59:21 +00:00
|
|
|
let config_dir = option_return!(dirs::config_dir(), "find config dir")?;
|
2022-04-19 19:10:36 +00:00
|
|
|
let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy());
|
|
|
|
|
2022-06-08 22:02:20 +00:00
|
|
|
// read file content to string
|
2022-04-19 19:10:36 +00:00
|
|
|
let config = fs::read_to_string(config_path)?;
|
2022-06-07 20:36:26 +00:00
|
|
|
|
|
|
|
// parse config and return it
|
2022-04-19 19:10:36 +00:00
|
|
|
Ok(toml::from_str(&config)?)
|
|
|
|
}
|
|
|
|
}
|