-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aliases): implement aliases for commit types
This is a decent change. Now the CommitType enum is auto-generated via a macro. This macro also implements two helper methods, and one Trait. The helper methods return a Vec<'static str> of all "aliases" for specified variant if called like CommitType::Variant.aliases(). Or if called on the enum itself there the method returns ALL aliases such as CommitType::all_aliases(). The macro also implments the std::str::FromStr trait so you can parse from a string to a known enum type automatically. Say we defined a "feat" or "ft" alias of variant "Feature", if we had a string of "ft" or "feature" we could call some_str.parse::<CommitType>().unwrap() to get a CommitType::Feature, or if it failed to parse we get a CommitType::Unknown Closes #3
- Loading branch information
Showing
4 changed files
with
69 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// regex cheat thanks to https://github.com/BurntSushi | ||
macro_rules! regex( | ||
($s:expr) => (::regex::Regex::new($s).unwrap()); | ||
); | ||
|
||
// A macro creating an entry types, and their aliases | ||
// | ||
// This is a little hacky, because it expects an Unknown () variant | ||
// | ||
// TODO: de-dup with recursive calls | ||
macro_rules! commit_type_enum { | ||
(#[derive($($d:ident),+)] pub enum $e:ident { $($v:ident ( $($a:ident),* ) ),+ }) => { | ||
#[derive($($d,)+)] | ||
pub enum $e { | ||
$($v,)+ | ||
} | ||
|
||
impl $e { | ||
#[allow(dead_code)] | ||
pub fn aliases(&self) -> Vec<&'static str> { | ||
match *self { | ||
$($e::$v => vec![ | ||
$( stringify!($a) ),* | ||
],)+ | ||
} | ||
} | ||
#[allow(dead_code)] | ||
pub fn all_aliases() -> Vec<&'static str> { | ||
vec![ | ||
$( $( stringify!($a),)* )+ | ||
] | ||
} | ||
} | ||
impl ::std::str::FromStr for $e { | ||
type Err = $e; | ||
|
||
#[allow(dead_code)] | ||
fn from_str(s: &str) -> Result<Self,Self::Err> { | ||
match s { | ||
$(stringify!($v) $( | stringify!($a) )* => Ok($e::$v),)+ | ||
_ => Err($e::Unknown) | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters