-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom string representation.
- Loading branch information
Showing
7 changed files
with
334 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,100 @@ | ||
use syn::{DeriveInput, Meta}; | ||
|
||
pub(crate) enum Case { | ||
Camel, | ||
Snake, | ||
None, | ||
} | ||
|
||
pub(crate) struct Attributes { | ||
pub(crate) case: Option<Case>, | ||
pub(crate) prefix: Option<String>, | ||
pub(crate) suffix: Option<String>, | ||
} | ||
|
||
impl Attributes { | ||
pub(crate) fn new(ast: &DeriveInput) -> Self { | ||
let mut new = Self { | ||
case: None, | ||
prefix: None, | ||
suffix: None, | ||
}; | ||
|
||
ast.attrs.iter().for_each(|attr| match &attr.meta { | ||
Meta::Path(_) => { | ||
panic!("Unexpected argument"); | ||
} | ||
Meta::List(list) => { | ||
let path = list | ||
.path | ||
.segments | ||
.iter() | ||
.map(|s| s.ident.to_string()) | ||
.collect::<Vec<_>>(); | ||
|
||
if path == vec!["enum_stringify"] { | ||
let mut tokens = list.tokens.clone().into_iter(); | ||
|
||
while let Some(attribute_type) = tokens.next() { | ||
let attribute_type = attribute_type.to_string(); | ||
|
||
if tokens.next().expect("type must be specified").to_string() != "=" { | ||
panic!("too many arguments"); | ||
} | ||
let value = tokens.next().expect("value must be specified").to_string(); | ||
|
||
match attribute_type.as_str() { | ||
"case" => { | ||
let case = match value.as_str() { | ||
"camel" => Case::Camel, | ||
"snake" => Case::Snake, | ||
_ => Case::None, | ||
}; | ||
new.case = Some(case); | ||
} | ||
"prefix" => { | ||
new.prefix = Some(value); | ||
} | ||
"suffix" => { | ||
new.suffix = Some(value); | ||
} | ||
_ => { | ||
panic!("Attribute not supported"); | ||
} | ||
} | ||
|
||
if let Some(comma_separator) = tokens.next() { | ||
if comma_separator.to_string() != "," { | ||
panic!("Expected a commaseparated attribute list"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Meta::NameValue(_) => { | ||
panic!("Unexpected argument"); | ||
} | ||
}); | ||
|
||
new | ||
} | ||
|
||
pub(crate) fn apply(&self, names: &Vec<&syn::Ident>) -> Vec<syn::Ident> { | ||
let mut new_names = Vec::new(); | ||
for name in names { | ||
let mut new_name = String::new(); | ||
if let Some(prefix) = &self.prefix { | ||
new_name.push_str(prefix); | ||
} | ||
|
||
// Add here case logic | ||
new_name.push_str(&name.to_string()); | ||
|
||
if let Some(suffix) = &self.suffix { | ||
new_name.push_str(suffix); | ||
} | ||
new_names.push(syn::Ident::new(&new_name, name.span())); | ||
} | ||
new_names | ||
} | ||
} |
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,90 @@ | ||
use std::str::FromStr; | ||
|
||
#[derive(Debug, PartialEq, enum_stringify::EnumStringify)] | ||
#[enum_stringify(suffix = Suff)] | ||
enum Number1 { | ||
Zero, | ||
One, | ||
Two, | ||
} | ||
|
||
#[test] | ||
fn test_suffix_to_string() { | ||
assert_eq!(Number1::Zero.to_string(), "ZeroSuff"); | ||
assert_eq!(Number1::One.to_string(), "OneSuff"); | ||
assert_eq!(Number1::Two.to_string(), "TwoSuff"); | ||
} | ||
#[test] | ||
fn test_suffix_from_str() { | ||
assert_eq!(Number1::from_str("ZeroSuff"), Ok(Number1::Zero)); | ||
assert_eq!(Number1::from_str("OneSuff"), Ok(Number1::One)); | ||
assert_eq!(Number1::from_str("TwoSuff"), Ok(Number1::Two)); | ||
} | ||
|
||
#[derive(Debug, PartialEq, enum_stringify::EnumStringify)] | ||
#[enum_stringify(prefix = Pref)] | ||
enum Number2 { | ||
Zero, | ||
One, | ||
Two, | ||
} | ||
|
||
#[test] | ||
fn test_prefix_to_string() { | ||
assert_eq!(Number2::Zero.to_string(), "PrefZero"); | ||
assert_eq!(Number2::One.to_string(), "PrefOne"); | ||
assert_eq!(Number2::Two.to_string(), "PrefTwo"); | ||
} | ||
|
||
#[test] | ||
fn test_prefix_from_str() { | ||
assert_eq!(Number2::from_str("PrefZero"), Ok(Number2::Zero)); | ||
assert_eq!(Number2::from_str("PrefOne"), Ok(Number2::One)); | ||
assert_eq!(Number2::from_str("PrefTwo"), Ok(Number2::Two)); | ||
} | ||
|
||
#[derive(Debug, PartialEq, enum_stringify::EnumStringify)] | ||
#[enum_stringify(prefix = Pref, suffix = Suff)] | ||
enum Number3 { | ||
Zero, | ||
One, | ||
Two, | ||
} | ||
|
||
#[test] | ||
fn test_prefix_suffix_to_string() { | ||
assert_eq!(Number3::Zero.to_string(), "PrefZeroSuff"); | ||
assert_eq!(Number3::One.to_string(), "PrefOneSuff"); | ||
assert_eq!(Number3::Two.to_string(), "PrefTwoSuff"); | ||
} | ||
|
||
#[test] | ||
fn test_prefix_suffix_from_str() { | ||
assert_eq!(Number3::from_str("PrefZeroSuff"), Ok(Number3::Zero)); | ||
assert_eq!(Number3::from_str("PrefOneSuff"), Ok(Number3::One)); | ||
assert_eq!(Number3::from_str("PrefTwoSuff"), Ok(Number3::Two)); | ||
} | ||
|
||
// Testing commutativity of prefix and suffix | ||
|
||
#[derive(Debug, PartialEq, enum_stringify::EnumStringify)] | ||
#[enum_stringify(suffix = Suff, prefix = Pref)] | ||
enum Number4 { | ||
Zero, | ||
One, | ||
Two, | ||
} | ||
|
||
#[test] | ||
fn test_suffix_prefix_to_string() { | ||
assert_eq!(Number4::Zero.to_string(), "PrefZeroSuff"); | ||
assert_eq!(Number4::One.to_string(), "PrefOneSuff"); | ||
assert_eq!(Number4::Two.to_string(), "PrefTwoSuff"); | ||
} | ||
|
||
#[test] | ||
fn test_suffix_prefix_from_str() { | ||
assert_eq!(Number4::from_str("PrefZeroSuff"), Ok(Number4::Zero)); | ||
assert_eq!(Number4::from_str("PrefOneSuff"), Ok(Number4::One)); | ||
assert_eq!(Number4::from_str("PrefTwoSuff"), Ok(Number4::Two)); | ||
} |
Oops, something went wrong.