noise-server/src/config.rs

43 lines
1021 B
Rust

use serde::{
Deserialize,
Serialize,
};
use std::path::PathBuf;
use tokio::{
fs::File,
io::AsyncReadExt,
};
use crate::cli::Options;
#[derive(Debug, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct Config {
pub database_path: PathBuf,
}
impl Config {
pub async fn get(options: &Options) -> std::io::Result<Self> {
if options.no_config {
Ok(Config::default())
} else {
let mut config_file = File::open(&options.config).await?;
let mut config_string = String::new();
config_file.read_to_string(&mut config_string).await?;
toml::from_str(&config_string)
.map_err(|err| std::io::Error::new(
std::io::ErrorKind::InvalidData,
err
)
)
}
}
}
impl Default for Config {
fn default() -> Self {
Config {
database_path: crate::DEFAULT_DATABASE_PATH.into(),
}
}
}