Skip to content

Commit 9ff60e2

Browse files
Fix unsynn parsing for unit structs in braid macro
The Fields::Unit enum variant was not parseable by unsynn because empty enum variants have no associated parser. Changed to Fields::Unit(Nothing) where Nothing is unsynn's type that always succeeds without consuming tokens, allowing the enum parser to match unit structs like: #[braid(serde)] pub struct BasicExampleBuf;
1 parent 861dd26 commit 9ff60e2

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
.idea
33
Cargo.lock
4+
.tracey/

crates/strid-macros/src/codegen/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ fn create_field_if_none(fields: &mut crate::grammar::Fields) {
443443
use crate::grammar::Fields;
444444

445445
// If it's a unit struct, convert it to an unnamed tuple struct with String
446-
if matches!(fields, Fields::Unit) {
446+
if matches!(fields, Fields::Unit(_)) {
447447
// Parse a dummy struct to extract the fields structure
448448
let dummy_struct: proc_macro2::TokenStream = "struct Dummy(String);".parse().unwrap();
449449
let mut iter = dummy_struct.to_token_iter();
@@ -498,7 +498,7 @@ fn get_field_info<'a>(
498498
let field = &f.content[0].value;
499499
Ok((&field.ty, None, &field.attrs))
500500
}
501-
Fields::Unit => {
501+
Fields::Unit(_) => {
502502
Err("unit structs are not supported - struct must have at least one field".to_string())
503503
}
504504
}

crates/strid-macros/src/grammar.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ unsynn! {
121121
Named(BraceGroupContaining<CommaDelimitedVec<NamedField>>),
122122
/// Unnamed fields: `(i32, i32)`
123123
Unnamed(ParenthesisGroupContaining<CommaDelimitedVec<UnnamedField>>),
124-
/// Unit struct (no fields)
125-
#[allow(dead_code)]
126-
Unit,
124+
/// Unit struct (no fields) - Nothing always succeeds without consuming tokens
125+
Unit(Nothing),
127126
}
128127

129128
/// A named field in a struct.
@@ -167,7 +166,7 @@ impl ItemStruct {
167166
match &self.fields {
168167
Fields::Named(f) => f.content.is_empty(),
169168
Fields::Unnamed(f) => f.content.is_empty(),
170-
Fields::Unit => true,
169+
Fields::Unit(_) => true,
171170
}
172171
}
173172
}

0 commit comments

Comments
 (0)