Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Discard trailing punctuation before quoting fields #8

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn expand(input: DataEnum) -> Vec<ImplItem> {
})
};

let fields: Punctuated<Ident, Token![,]> = fields
let mut fields: Punctuated<Ident, Token![,]> = fields
.unnamed
.clone()
.into_pairs()
Expand All @@ -254,6 +254,17 @@ fn expand(input: DataEnum) -> Vec<ImplItem> {
})
.collect();

// Make sure that we don't have any trailing punctuation
// This ensure that if we have a single unnamed field,
// we will produce a value of the form `(v)`,
// not a single-element tuple `(v,)`
if let Some(mut pair) = fields.pop() {
if let Pair::Punctuated(v, _) = pair {
pair = Pair::End(v);
}
fields.extend(std::iter::once(pair));
}

items.extend(
Quote::new_call_site()
.quote_with(smart_quote!(
Expand Down
6 changes: 6 additions & 0 deletions tests/trailing_punct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use is_macro::Is;

#[derive(Debug, Is)]
pub enum Enum {
B(usize,),
}