Move get_config into config module

This commit is contained in:
Elias Schriefer 2021-07-12 10:59:13 +02:00
parent e32c0e2a70
commit aa021a9754
2 changed files with 26 additions and 23 deletions

View File

@ -3,6 +3,11 @@ use serde::{
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")]
@ -10,6 +15,25 @@ 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 {

View File

@ -3,11 +3,7 @@ use std::{
boxed::Box,
error::Error,
};
use tokio::{
sync::RwLock,
fs::File,
io::AsyncReadExt,
};
use tokio::sync::RwLock;
use crate::{
cli::Options,
config::Config,
@ -30,7 +26,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
return Ok(());
}
let mut config = get_config(&options).await?;
let mut config = Config::get(&options).await?;
if clap_matches.is_present("database-path") {
config.database_path = options.database_path;
@ -47,20 +43,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
Ok(())
}
async fn get_config(options: &Options) -> std::io::Result<Config> {
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
)
)
}
}