diff --git a/Cargo.toml b/Cargo.toml index bd150f2..2eb422e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,9 @@ name = "git-commit-messenger" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "gcm" +path = "src/lib/lib.rs" [dependencies] duct = "0.13.5" @@ -15,3 +17,4 @@ serde_yaml = "0.9.13" termination = "0.1.2" thiserror = "1.0.37" yaml-rust = "0.4.5" + diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index b5614dd..0000000 --- a/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod utils; diff --git a/src/lib/config/generate.rs b/src/lib/config/generate.rs new file mode 100644 index 0000000..06f8d3b --- /dev/null +++ b/src/lib/config/generate.rs @@ -0,0 +1,63 @@ +use std::{fs, path::PathBuf}; + +use super::{ConfigOptions, Part, Variant}; + +pub fn read_from_path(path: PathBuf) -> Option { + let config_yml = fs::read_to_string(path).ok()?; + + serde_yaml::from_str(&config_yml).ok()? +} + +pub fn default_config() -> ConfigOptions { + ConfigOptions { + variants: vec![ + Variant { + name: String::from("Conventional Commits"), + parts: vec![ + Part::ConventionalType, + Part::TextInput { + prompt: String::from("Scope"), + pattern: Some(String::from("({0})")), + }, + Part::Literal { + value: String::from(": "), + }, + Part::TextInput { + prompt: String::from("Description"), + pattern: None, + }, + ], + }, + Variant { + name: String::from("Gitmoji"), + parts: vec![ + Part::Gitmoji, + Part::Space, + Part::TextInput { + prompt: String::from("Description"), + pattern: None, + }, + ], + }, + Variant { + name: String::from("CC + Gitmoji"), + parts: vec![ + Part::ConventionalType, + Part::TextInput { + prompt: String::from("Scope"), + pattern: Some(String::from(" ({0})")), + }, + Part::Literal { + value: String::from(": "), + }, + Part::Gitmoji, + Part::Space, + Part::TextInput { + prompt: String::from("Description"), + pattern: None, + }, + ], + }, + ], + } +} diff --git a/src/config.rs b/src/lib/config/mod.rs similarity index 66% rename from src/config.rs rename to src/lib/config/mod.rs index 033d26c..8846e1f 100644 --- a/src/config.rs +++ b/src/lib/config/mod.rs @@ -1,8 +1,10 @@ -use std::{fmt::Display, fs, path::PathBuf}; - use home::home_dir; - use serde::Deserialize; +use std::fmt::Display; + +use self::generate::{default_config, read_from_path}; + +mod generate; #[derive(Deserialize)] pub struct ConfigOptions { @@ -47,38 +49,31 @@ pub struct SelectOption { pub description: Option, } -const _TEST_YML: &str = "\ -- type: ConventionalType -- type: Literal - value: ' (' -- type: Select - prompt: 'Scope: ' - options: - - key: fileui5 - - key: listui5 -- type: Literal - value: '): ' -- type: Gitmoji -- type: Space -- type: TextInput - prompt: 'Description: ' -"; - pub struct Config { config: ConfigOptions, } impl Config { pub fn new() -> Self { - let mut config_path = PathBuf::from(home_dir().unwrap()); + let mut config_path = home_dir().unwrap(); config_path.push(".config/gcm/config.yml"); - let config_yml = fs::read_to_string(config_path).unwrap(); - let config = serde_yaml::from_str(&config_yml).unwrap(); - Config { config } + if let Some(config) = read_from_path(config_path) { + Config { config } + } else { + Default::default() + } } pub fn variants(&self) -> &Vec { &self.config.variants } } + +impl Default for Config { + fn default() -> Self { + Config { + config: default_config(), + } + } +} diff --git a/src/utils/error.rs b/src/lib/error/mod.rs similarity index 100% rename from src/utils/error.rs rename to src/lib/error/mod.rs diff --git a/src/utils/git.rs b/src/lib/git/mod.rs similarity index 100% rename from src/utils/git.rs rename to src/lib/git/mod.rs diff --git a/src/lib/lib.rs b/src/lib/lib.rs new file mode 100644 index 0000000..3c17e35 --- /dev/null +++ b/src/lib/lib.rs @@ -0,0 +1,4 @@ +pub mod config; +pub mod error; +pub mod git; +pub mod prompt; diff --git a/src/prompt/conventional_type.rs b/src/lib/prompt/conventional_type.rs similarity index 81% rename from src/prompt/conventional_type.rs rename to src/lib/prompt/conventional_type.rs index e044006..6b11c78 100644 --- a/src/prompt/conventional_type.rs +++ b/src/lib/prompt/conventional_type.rs @@ -2,7 +2,7 @@ use inquire::Select; use crate::config::SelectOption; -const CONVENTIONAL_TYPE_JSON: &str = include_str!("../data/conventional_types.json"); +const CONVENTIONAL_TYPE_JSON: &str = include_str!("../../data/conventional_types.json"); pub fn select_conventional_type() -> String { let conventional_types: Vec = diff --git a/src/prompt/gitmoji.rs b/src/lib/prompt/gitmoji.rs similarity index 90% rename from src/prompt/gitmoji.rs rename to src/lib/prompt/gitmoji.rs index 634884e..9a05e42 100644 --- a/src/prompt/gitmoji.rs +++ b/src/lib/prompt/gitmoji.rs @@ -9,7 +9,7 @@ struct Gitmoji { description: String, } -const GITMOJI_JSON: &str = include_str!("../data/gitmojis.json"); +const GITMOJI_JSON: &str = include_str!("../../data/gitmojis.json"); impl Display for Gitmoji { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/src/prompt/message_builder.rs b/src/lib/prompt/message_builder.rs similarity index 92% rename from src/prompt/message_builder.rs rename to src/lib/prompt/message_builder.rs index 35a0f06..caa0368 100644 --- a/src/prompt/message_builder.rs +++ b/src/lib/prompt/message_builder.rs @@ -19,12 +19,18 @@ pub struct MessageBuilder { message: String, } -impl MessageBuilder { - pub fn new() -> Self { +impl Default for MessageBuilder { + fn default() -> Self { MessageBuilder { message: String::from(""), } } +} + +impl MessageBuilder { + pub fn new() -> Self { + Default::default() + } pub fn consume(self) -> String { self.message @@ -68,7 +74,7 @@ impl MessageBuilder { } pub fn add_literal(&mut self, literal: &str) -> &Self { - self.add_text(&literal) + self.add_text(literal) } pub fn select_conventional_type(&mut self) -> &Self { @@ -78,7 +84,7 @@ impl MessageBuilder { } fn add_text(&mut self, text: &str) -> &Self { - self.message.push_str(&text); + self.message.push_str(text); self } diff --git a/src/prompt/mod.rs b/src/lib/prompt/mod.rs similarity index 100% rename from src/prompt/mod.rs rename to src/lib/prompt/mod.rs diff --git a/src/main.rs b/src/main.rs index c691efa..8aba7b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ -mod config; -mod prompt; -use config::{Config, Part}; +use gcm::{ + config::{Config, Part}, + error::Error, + git, + prompt::message_builder::MessageBuilder, +}; use inquire::{Confirm, Select}; -use crate::prompt::message_builder::MessageBuilder; -use git_commit_messenger::utils::{error::Error, git}; - #[termination::display] fn main() -> Result<(), Error> { git::check_ready()?; @@ -29,7 +29,7 @@ fn main() -> Result<(), Error> { git::commit(message); } - return Ok(()); + Ok(()) } fn prompt_user(parts: &[Part]) -> String { @@ -41,11 +41,11 @@ fn prompt_user(parts: &[Part]) -> String { prompt, options, pattern, - } => message_builder.select_prompt(&prompt, options, pattern), + } => message_builder.select_prompt(prompt, options, pattern), Part::Space => message_builder.add_literal(" "), Part::Gitmoji => message_builder.select_gitmoji(), - Part::Literal { value } => message_builder.add_literal(&value), - Part::TextInput { prompt, pattern } => message_builder.text_input(&prompt, &pattern), + Part::Literal { value } => message_builder.add_literal(value), + Part::TextInput { prompt, pattern } => message_builder.text_input(prompt, pattern), Part::ConventionalType => message_builder.select_conventional_type(), }; } diff --git a/src/utils/mod.rs b/src/utils/mod.rs deleted file mode 100644 index 1007cfd..0000000 --- a/src/utils/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod error; -pub mod git;