Skip to content

Output does not work with other existing tools #18

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

Open
computermouth opened this issue Mar 13, 2025 · 1 comment
Open

Output does not work with other existing tools #18

computermouth opened this issue Mar 13, 2025 · 1 comment

Comments

@computermouth
Copy link

computermouth commented Mar 13, 2025

I checked out another person's branch, from PR #16 . It solves the nightly compilation issues, and completes without a problem.

However, writing out the payload, and checking it with external tools, I encountered some errors.

    let stdout = io::stdout();
    let mut handle = stdout.lock();

    handle.write_all(&buf).unwrap();
    handle.flush().unwrap();
$ cargo +nightly run > out.mp
serialized 49 bytes

$ mpk msgpacker.mp --to-json
"Kuala Lumpur"

$ ~/go/bin/msgpack-cli decode msgpacker.mp --pp
"Kuala Lumpur"2025/03/13 16:48:48 msgpack decode error [pos 14]: invalid byte descriptor for decoding bytes, got: 0x82

Given that the test clears internally when you parse back in, I have a feeling that the library maybe doesn't implement the spec correctly. This is backed up by rmp and rmp_serde parsing cleanly with the tools.

$ ~/go/bin/msgpack-cli decode rmp-serde.mp --pp
["Kuala Lumpur",{"Street 1":10,"Street 2":20},["Zone 1","Zone 2"]]

$ ~/go/bin/msgpack-cli decode msgpacker.mp --pp
"Kuala Lumpur"2025/03/13 17:06:38 msgpack decode error [pos 14]: invalid byte descriptor for decoding bytes, got: 0x82

$ mpk rmp-serde.mp --to-json
["Kuala Lumpur",{"Street 2":20,"Street 1":10},["Zone 1","Zone 2"]]

$ mpk msgpacker.mp --to-json
"Kuala Lumpur"

Here's the difference in the hex:

$ xxd msgpacker.mp 
00000000: ac4b 7561 6c61 204c 756d 7075 7282 a853  .Kuala Lumpur..S
00000010: 7472 6565 7420 3214 a853 7472 6565 7420  treet 2..Street 
00000020: 310a 92a6 5a6f 6e65 2031 a65a 6f6e 6520  1...Zone 1.Zone 
00000030: 32                                       2

$ xxd rmp-serde.mp 
00000000: 93ac 4b75 616c 6120 4c75 6d70 7572 82a8  ..Kuala Lumpur..
00000010: 5374 7265 6574 2032 14a8 5374 7265 6574  Street 2..Street
00000020: 2031 0a92 a65a 6f6e 6520 31a6 5a6f 6e65   1...Zone 1.Zone
00000030: 2032                                      2

The rmp-serde.mp file starts with a byte 0x93. According to the spec, this is a fixed array delimiter with 3 elements (name, inhabitants_per_street, and zones). It seems these other tools expect the payload to start with either an array, map, or otherwise exclusively contain a single messagepack type -- opposed to this library's list of types with no outer container at the top level

@computermouth
Copy link
Author

The following hack fixed the parsing with other tools:

    let field_count = f.named.len();
    let mut field_access_exprs: Vec<syn::Expr> = Vec::new();
    let block_packable: Block = parse_quote! {
        {
            let mut n = 0;
            n += match #field_count {
                0..=15 => {
                    buf.extend(::core::iter::once((#field_count as u8) | 0x90));
                    1
                }
                16..=65535 => {
                    buf.extend(::core::iter::once(0xdc).chain((#field_count as u16).to_be_bytes()));
                    3
                }
                _ => {
                    buf.extend(::core::iter::once(0xdd).chain((#field_count as u32).to_be_bytes()));
                    5
                }
            };
        }
    };

Its turning out to be much more complicated to fix than I originally imagined, and I'm not handy with Rust macros

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant