50 lines
1.2 KiB
Rust
50 lines
1.2 KiB
Rust
use serde::{
|
|
Deserialize,
|
|
Serialize,
|
|
};
|
|
use std::{
|
|
net::IpAddr,
|
|
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,
|
|
pub ip_address: IpAddr,
|
|
pub port: u16,
|
|
}
|
|
|
|
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(),
|
|
ip_address: crate::DEFAULT_IP_ADDRESS,
|
|
port: crate::DEFAULT_PORT,
|
|
}
|
|
}
|
|
} |