Skip to content

Commit

Permalink
refactor: use Astr<str> for commit type instead of custom impl
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Oct 24, 2020
1 parent fa24d64 commit bac60fd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/bin/coco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() -> Result<()> {
fn app<'a, 'b>() -> App<'a, 'b> {
let keys = CocoGitto::get_commit_metadata()
.iter()
.map(|(commit_type, _)| commit_type.get_key_str())
.map(|(commit_type, _)| commit_type.as_ref())
.collect::<Vec<&str>>();

App::new("Coco")
Expand Down
46 changes: 23 additions & 23 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ impl fmt::Display for Commit {
}
}

impl CommitType {
pub fn get_key_str(&self) -> &str {
impl AsRef<str> for CommitType {
fn as_ref(&self) -> &str {
match &self {
Feature => "feat",
BugFix => "fix",
Expand All @@ -313,10 +313,29 @@ impl CommitType {
}
}

impl From<&str> for CommitType {
fn from(commit_type: &str) -> Self {
match commit_type {
"feat" => Feature,
"fix" => BugFix,
"chore" => Chore,
"revert" => Revert,
"perf" => Performances,
"docs" => Documentation,
"style" => Style,
"refactor" => Refactoring,
"test" => Test,
"build" => Build,
"ci" => Ci,
other => Custom(other.to_string()),
}
}
}

impl ToString for CommitMessage {
fn to_string(&self) -> String {
let mut message = String::new();
message.push_str(&self.commit_type.get_key_str());
message.push_str(&self.commit_type.as_ref());

if let Some(scope) = &self.scope {
message.push_str(&format!("({})", scope));
Expand All @@ -340,28 +359,9 @@ impl ToString for CommitMessage {
}
}

impl From<&str> for CommitType {
fn from(commit_type: &str) -> Self {
match commit_type {
"feat" => Feature,
"fix" => BugFix,
"chore" => Chore,
"revert" => Revert,
"perf" => Performances,
"docs" => Documentation,
"style" => Style,
"refactor" => Refactoring,
"test" => Test,
"build" => Build,
"ci" => Ci,
other => Custom(other.to_string()),
}
}
}

impl fmt::Display for CommitType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get_key_str())
write!(f, "{}", self.as_ref())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ impl VersionIncrement {
"Found {} commit {} with type : {}",
"BREAKING CHANGE".red(),
commit.shorthand().blue(),
commit_type.get_key_str().yellow()
commit_type.as_ref().yellow()
),
(_, false) => println!(
"Skipping irrelevant commit {} with type : {}",
commit.shorthand().blue(),
commit.message.commit_type.get_key_str().yellow()
commit.message.commit_type.as_ref().yellow()
),
}
}
Expand Down

0 comments on commit bac60fd

Please sign in to comment.