-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow referring to elided fields values in later fields' parsers with seq!
#406
Comments
_temporary
variables to seq!
seq!
seq!
seq!
I would love to support this but I'm unsure of a way to do this with |
Maybe not exactly as proposed, but can the following syntax idea work as an implementable alternative? seq! {MyStruct {
#[ignore] b0: be_u8,
li: success(b0).map(|n| n >> 6),
version: success(b0).map(|n| (n >> 3) & 0b111),
// ... etc
}} |
Sorry, lost track of this. If we do this, I'd at least like to see mirrored support for tuples as well. My main concern is with what to name it to make the intent clear. We aren't ignoring the parser, only the container initialization. Maybe |
I think I got this working for structs in #511, but I don't really know how to implement this for tuples. Currently we can do something like (from the docs):
Do we expect something like: |
@DJDuque thank for that work! I keep going back and forth on how we should handle this and if it should be handled. Especially seeing it in action, This isn't too say "no" but I want to exercise some caution here and would appreciate input on managing the balance with |
I agree that managing things can get out of hand if we try to force too much through macros, but I also think that referring to previous fields is common enough that it is reasonable for The current It would be good to have the opinion of other |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
#655, specifically 04e8444, added placeholder names for elided fields (e.g. |
One idea is using pub(crate) fn event_view<'a>(
endian: Endianness,
) -> impl Parser<&'a [u8], EventView<'a>, ContextError> {
seq! {EventView {
event_id: u16(endian),
trigger_mask: u16(endian),
serial_number: u32(endian),
timestamp: u32(endian),
event_size @ _ : u32(endian).verify(|&n| n >= 8),
banks_size @ _: u32(endian).verify(&n| n == event_size - 8),
bank_views: dispatch! {u32(endian);
1 => length_and_then(success(banks_size), repeat_till0(padded_bank(bank_16_view(endian)), eof)),
17 => length_and_then(success(banks_size), repeat_till0(padded_bank(bank_32_view(endian)), eof)),
49 => length_and_then(success(banks_size), repeat_till0(padded_bank(bank_32a_view(endian)), eof)),
_ => fail,
}.map(|(bank_views, _)| bank_views),
}}
} another option is to create our own syntax with pub(crate) fn event_view<'a>(
endian: Endianness,
) -> impl Parser<&'a [u8], EventView<'a>, ContextError> {
seq! {EventView {
event_id: u16(endian),
trigger_mask: u16(endian),
serial_number: u32(endian),
timestamp: u32(endian),
_ as event_size u32(endian).verify(|&n| n >= 8),
_ as banks_size: u32(endian).verify(&n| n == event_size - 8),
bank_views: dispatch! {u32(endian);
1 => length_and_then(success(banks_size), repeat_till0(padded_bank(bank_16_view(endian)), eof)),
17 => length_and_then(success(banks_size), repeat_till0(padded_bank(bank_32_view(endian)), eof)),
49 => length_and_then(success(banks_size), repeat_till0(padded_bank(bank_32a_view(endian)), eof)),
_ => fail,
}.map(|(bank_views, _)| bank_views),
}}
} The proposed is keyword also puts the binding on the left... |
Please complete the following tasks
winnow version
0.5.31
Describe your use case
Having
_temporary
variables that don't make it into the struct (but also don't get completely ignored like_
) could be beneficial for cases like this.The above
seq!
call could look something like:which is easier to follow and understand.
This will also allow us to use this macro to do stuff similar to
nom-derive
's anonimous fields mentioned here, e.g.The text was updated successfully, but these errors were encountered: