Skip to content

Commit

Permalink
feat: create conventional message log
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 2, 2021
1 parent 00861c3 commit c09aa49
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
20 changes: 20 additions & 0 deletions src/domain/git/coco_commit_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ConventionalMessage {
pub type_: String,
pub scope: String,
pub breaking: bool,
pub subject: String,
}

impl Default for ConventionalMessage {
fn default() -> Self {
ConventionalMessage {
type_: "".to_string(),
scope: "".to_string(),
breaking: false,
subject: "".to_string(),
}
}
}
1 change: 1 addition & 0 deletions src/domain/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub use coco_commit::CocoCommit;

pub mod coco_branch;
pub mod coco_commit;
pub mod coco_commit_message;
28 changes: 17 additions & 11 deletions src/infrastructure/git/git_commit_message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::domain::git::coco_commit_message::ConventionalMessage;
use regex::Regex;

lazy_static! {
Expand All @@ -11,17 +12,22 @@ lazy_static! {
.unwrap();
}

pub struct ConventionalLog {
pub type_: String,
pub scope: String,
pub breaking: bool,
pub subject: String,
}
pub fn parse_builtin(message: &str) -> Option<ConventionalMessage> {
let mut commit_message = ConventionalMessage::default();
if let Some(caps) = CONVENTIONAL.captures(message) {
commit_message.type_ = (&caps["type"]).to_string();
commit_message.subject = (&caps["subject"]).to_string();
if let Some(_breaking) = caps.name("breaking") {
commit_message.breaking = true;
}
if let Some(_scope) = caps.name("scope") {
commit_message.scope = (&caps["scope"]).to_string();
}

pub fn parse_builtin(message: &str) {
if let Some(capts) = CONVENTIONAL.captures(message) {
println!("{:?}", capts);
return Some(commit_message);
}

return None;
}

#[cfg(test)]
Expand All @@ -30,7 +36,7 @@ mod test {

#[test]
fn should_parse_conventional_message() {
let message = "build: init project";
parse_builtin(message);
let msg = parse_builtin("build(zz): init project").unwrap();
assert_eq!("build", msg.type_);
}
}

0 comments on commit c09aa49

Please sign in to comment.