From 7cd9da9f3d89cd62d5ae922539d915bdaf7477d0 Mon Sep 17 00:00:00 2001 From: n0m1s Date: Sun, 5 Jan 2020 21:40:59 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Settings=20struct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 22 ++++------------------ src/settings.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 src/settings.rs diff --git a/src/main.rs b/src/main.rs index 61942d5..4aa2515 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,10 @@ +mod settings; mod gitmoji; -use std::path::PathBuf; - -fn config_path() -> PathBuf { - let mut config_dir = dirs::config_dir().expect("cannot open config dir"); - config_dir.push("gitmoji-rust"); - - config_dir -} +use settings::Settings; fn main() { - //path to the "gitmoji" git repo - let mut repo_path = config_path(); - repo_path.push("gitmoji"); - let repo_path = repo_path; - - //json file path - let mut json_path = config_path(); - json_path.push("gitmoji.json"); - let json_path = json_path; + let settings = Settings::new(None); - gitmoji::update(&gitmoji::Url::default_github(), &repo_path, &json_path).unwrap(); + gitmoji::update(&gitmoji::Url::default_github(), &settings.repo_path, &settings.json_path).unwrap(); } diff --git a/src/settings.rs b/src/settings.rs new file mode 100644 index 0000000..989aaa2 --- /dev/null +++ b/src/settings.rs @@ -0,0 +1,32 @@ +use std::path::PathBuf; + +pub struct Settings { + pub config_path : PathBuf, + pub repo_path : PathBuf, + pub json_path : PathBuf, +} + +impl Settings { + pub fn new(config_dir: Option) -> Settings { + let config_dir = match config_dir { + Some(x) => x, + None => { + let mut dir = dirs::config_dir().expect("cannot open config dir"); + dir.push("gitmoji-rust"); + dir + } + }; + + let mut repo_dir = config_dir.clone(); + repo_dir.push("gitmoji"); + + let mut json_file = config_dir.clone(); + json_file.push("gitmoji.json"); + + Settings { + config_path: config_dir, + repo_path: repo_dir, + json_path: json_file, + } + } +}