Skip to content

Commit 4abec09

Browse files
authored
Merge pull request #3 from TheEppicJR/add-default
Added the ability to use #[default] and added Default in the derive trait
2 parents 703fdc7 + 4ec558f commit 4abec09

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,25 @@ fn concat<T>(mut v1: Vec<T>, mut v2: Vec<T>) -> Vec<T> {
187187
v1
188188
}
189189

190+
fn check_for_default(triples: &mut Vec<(TokenStream, Ident, TokenTree)>) {
191+
let mut default_position: Option<usize> = None;
192+
for (attributes, _variant_name, variant_value) in triples.into_iter() {
193+
if attributes.to_string().contains("default") {
194+
if default_position.is_some() {
195+
// error!("Multiple variants marked as default");
196+
}
197+
default_position = Some(variant_value.to_string().parse::<usize>().unwrap());
198+
}
199+
}
200+
if !default_position.is_some() {
201+
// No default specified, so we'll just use the first variant
202+
triples[0].0.extend(vec![
203+
punct_token('#'),
204+
bracket_token(vec![ident_token("default")]).into(),
205+
]);
206+
}
207+
}
208+
190209
#[proc_macro]
191210
pub fn primitive_enum(tokens: TokenStream) -> TokenStream {
192211
let mut iter = tokens.into_iter();
@@ -302,6 +321,7 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream {
302321
offset += 1;
303322
triples.push((variant_attributes, variant_name, value));
304323
}
324+
check_for_default(&mut triples); // make sure there's a default, if the user didn't specify one
305325
triples
306326
};
307327

@@ -322,7 +342,6 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream {
322342
ident_token("repr"),
323343
paren_token(repr_type.clone()),
324344
]));
325-
326345
// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
327346
out.push(punct_token('#'));
328347
out.push(bracket_token(vec![
@@ -339,8 +358,11 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream {
339358
ident_token("Eq"),
340359
punct_token(','),
341360
ident_token("Hash"),
361+
punct_token(','),
362+
ident_token("Default"),
342363
]),
343364
]));
365+
344366
out.push(ident_token("pub"));
345367
out.push(ident_token("enum"));
346368
out.push(TokenTree::Ident(enum_identifier.clone()));

tests/sample.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,22 @@ mod tests {
196196
assert_eq!(MarkerType::from_name("Markerboxes"), Some(MarkerType::Markerboxes));
197197
assert_eq!(MarkerType::from_name("Markerpitlane"), Some(MarkerType::Markerpitlane));
198198
}
199+
200+
primitive_enum! { MarkerType2 u32 ;
201+
A, // 0
202+
B, // 1
203+
C, // 2
204+
D, // 3
205+
E, // 4
206+
#[default]
207+
F, // 5
208+
G, // 6
209+
}
210+
211+
#[test]
212+
fn test_enum_default() {
213+
assert_eq!(MarkerType2::default(), MarkerType2::F);
214+
assert_eq!(MarkerType2::from(0), Some(MarkerType2::A));
215+
assert_eq!(MyEnum::default(), MyEnum::A);
216+
}
199217
}

0 commit comments

Comments
 (0)