Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Gwendal Fernet
@gwendall:matrix.org
[m]
Like it it's in the header every packet contains
Gwendal Fernet
@gwendall:matrix.org
[m]
I don't get what you mean. Every packet has a fixed header of 2 bytes. I though I should implement a fixed_header parser and other parser to parse the packet? But I don't get what the proper way of doing so. This is what I've started
cheako
@cheako:matrix.org
[m]
You should be calling be_u8 multiple times, once for each type.
Gwendal Fernet
@gwendall:matrix.org
[m]
So have a different parser for each fixed_header of each packet?
cheako
@cheako:matrix.org
[m]
Instead of a header followed by a type, it's one alternate type after another each beginning with a constant u8.
at the end, an error is generated when nothing looked like a valid type.
tanriol
@tanriol:matrix.org
[m]
Alternatively you can parse the header and then do a match kind { ... }, choosing the parser based on the kind and making an error manually if the kind is not a known one.
2 replies
IMHO, being able to use just normal Rust code when it's more clear than the Nom-native way is a feature of modern nom versions, not a bug.
One thing I'm missing in the functional nom is something like the legacy do_parse macro... I wonder whether it could be a good idea to bring it back.
cheako
@cheako:matrix.org
[m]
You wouldn't get an error from be_u8, you'd just get some number outside the valid range.
tanriol
@tanriol:matrix.org
[m]
Not for be_u8, to avoid conflating "an unknown variant" with "a known variant rejected due to an internal error"
cheako
@cheako:matrix.org
[m]
I don't know much about nom's errors, but I feel them being context aware would be critical... I.E. you'd be able to tell the type u8 was where the matching failed.
tanriol
@tanriol:matrix.org
[m]
The problem is that alt, IIRC, would just tell you that "at position 0 all 15 possible branches failed" without telling you whether any one went beyond the first byte.
cheako
@cheako:matrix.org
[m]
ohh, IC.
Paul Wood
@plmwd
Hi all, so I'm a bit of a rust noob, and I'm really having a hard time understanding the lifetimes here https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=c2260098b1b7199477cf4f27b44dfd14. Sorry for not having a detailed explanation of my problem. I'm not sure what else more to say. Trying to read a file then parse that, but for some reason I'm getting lifetime errors when passing a &[u8] of a vec<u8>
2 replies
Paul Wood
@plmwd:matrix.org
[m]
I can paste the error when I’m at my computer again, but I remember it being E0515
Paul Wood
@plmwd:matrix.org
[m]
It does appear to be relating to ? And using a boxed dyn Error. Changed to use unwrap and it’s fine
cheako
@cheako:matrix.org
[m]
Add extern crate nom; as the first line.
Paul Wood
@plmwd:matrix.org
[m]
I’m guessing the IResult borrows the byte slice
cheako
@cheako:matrix.org
[m]
Since there is only the one ? I don't see why that should be dyn.
Paul Wood
@plmwd:matrix.org
[m]
It was just a contrived example from code at work so I didn’t have to think about errors since I’m just hacking around right now lol
But yeah I agree
cheako
@cheako:matrix.org
[m]
Also from experience, I tend to have better luck when I use enums in place of dyn.
Paul Wood
@plmwd:matrix.org
[m]
So, creating a ErrorKind enum for your app?
Thanks for pointing out the result issue
sayanarijit
@sayanarijit:matrix.org
[m]

This https://github.com/sayanarijit/expandvars-rs/blob/main/src/lib.rs#L97
fails with

thread 'tests::test_expandvars_get_default' panicked at 'assertion failed: `(left == right)`
  left: `"${FOO-default}"`,
 right: `"default"`', src/lib.rs:97:9

parser impl: https://github.com/sayanarijit/expandvars-rs/blob/main/src/parser.rs#L30

4 replies
I feel like I'm missing something very simple and basic... But can't put a finger on it.
cheako
@cheako:matrix.org
[m]
pares_fragemnt's call to parse_constant feels greedy, that it may consume more input than intended... Shouldn't it end on a `, unless under"`?
sayanarijit
@sayanarijit:matrix.org
[m]
cheako
@cheako:matrix.org
[m]
sayanarijit: You should start using permalinks, that contain the commit ID. There is an option for that in github.
sayanarijit
@sayanarijit:matrix.org
[m]
Ah right thanks...
cheako
@cheako:matrix.org
[m]
Now parse_closing_brace is explicitly gobbling your brace. The is nothing that matches a zero-width string, try using take_while in parse_constant.
Keegan Conlee
@dacid44:matrix.org
[m]
is there a difference between eof and all_consuming?
Other than where you'd use them?
tgadola
@tgadola
Does anyone know the best way to handle a binary protocol that has variable length fields. E.g. for the length field it is either one byte, or 2 bytes (if the first byte is greater than 127) similar for the type field.
xiretza
@xiretza:xiretza.xyz
[m]
that depends on the encoding that's being used, it sounds kind of like LEB128 to me
tgadola
@tgadola
its a custom format I believe
designed for compact storage. so some fields are a single byte whenever possible or 2 bytes when it cant be avoided
tanriol
@tanriol:matrix.org
[m]
The "has one more byte if this one is >127" sounds like LEB128, yes.
tgadola
@tgadola
interesting, I'll have to take a deeper look into the encoding
I havent heard of LEB128 before haha
cheako
@cheako:matrix.org
[m]
Is there a way to group lines with a common prefix, think the output of find? I feel like I'm looking for hashmap recursion.
cheako
@cheako:matrix.org
[m]
Should I parse to a collection of char slices and then copy those into a hashmap tree?
woile
@woile:matrix.org
[m]

hi people, I'm trying to do something like:

let build_string = take_while1(is_alphanumeric);
delimited(char('"'), build_string, char('"'))(input)

but I get this error:

type mismatch resolving <&str as InputTakeAtPosition>::Item == u8 expected char, found u8

any idea how can I fix it?

xiretza
@xiretza:xiretza.xyz
[m]
wrong is_alphanumeric, you want char::is_alphanumeric
woile
@woile:matrix.org
[m]
ohh thanks!
it worked, thanks!