Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,3 +17,4 @@ serde_yaml = "0.9.13"
termination = "0.1.2"
thiserror = "1.0.37"
yaml-rust = "0.4.5"

1 change: 0 additions & 1 deletion src/lib.rs

This file was deleted.

63 changes: 63 additions & 0 deletions src/lib/config/generate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::{fs, path::PathBuf};

use super::{ConfigOptions, Part, Variant};

pub fn read_from_path(path: PathBuf) -> Option<ConfigOptions> {
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,
},
],
},
],
}
}
43 changes: 19 additions & 24 deletions src/config.rs → src/lib/config/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -47,38 +49,31 @@ pub struct SelectOption {
pub description: Option<String>,
}

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<Variant> {
&self.config.variants
}
}

impl Default for Config {
fn default() -> Self {
Config {
config: default_config(),
}
}
}
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions src/lib/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod config;
pub mod error;
pub mod git;
pub mod prompt;
Original file line number Diff line number Diff line change
Expand Up @@ -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<SelectOption> =
Expand Down
2 changes: 1 addition & 1 deletion src/prompt/gitmoji.rs → src/lib/prompt/gitmoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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()?;
Expand All @@ -29,7 +29,7 @@ fn main() -> Result<(), Error> {
git::commit(message);
}

return Ok(());
Ok(())
}

fn prompt_user(parts: &[Part]) -> String {
Expand All @@ -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(),
};
}
Expand Down
2 changes: 0 additions & 2 deletions src/utils/mod.rs

This file was deleted.