From aa021a9754be596ac3b888d36d98edcc7cb2bdf5 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Mon, 12 Jul 2021 10:59:13 +0200 Subject: [PATCH] Move get_config into config module --- src/config.rs | 24 ++++++++++++++++++++++++ src/main.rs | 25 ++----------------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/config.rs b/src/config.rs index 42dda81..d378d44 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 { + 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 { diff --git a/src/main.rs b/src/main.rs index e3c1252..4c25055 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { 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; @@ -46,21 +42,4 @@ async fn main() -> Result<(), Box> { sqlite::load_template(sqlite_connection.get_mut()).await?; Ok(()) -} - -async fn get_config(options: &Options) -> std::io::Result { - 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 - ) - ) - } } \ No newline at end of file