Skip to content
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

Fix u64 deserialization #705

Open
litchipi opened this issue Mar 21, 2024 · 2 comments
Open

Fix u64 deserialization #705

litchipi opened this issue Mar 21, 2024 · 2 comments
Labels
A-parse Area: Parsing TOML A-serde Area: Serde integration C-bug Category: Things not working as expected

Comments

@litchipi
Copy link

Past issue: toml-rs/toml-rs#256

u64 cannot be deserialized as it's backed by a i64 format

This causes this to fail:

let data = format!("id = {}", u64::MAX);
let val : toml::Value = toml::from_str(&data).unwrap();
println!("{:?}", val);

And any number > i64::MAX will also fail.

@epage epage added C-bug Category: Things not working as expected A-parse Area: Parsing TOML A-serde Area: Serde integration labels Mar 21, 2024
@epage
Copy link
Member

epage commented Mar 21, 2024

This deals with problems similar to #540

@litchipi
Copy link
Author

Indeed, I hacked a bit around, and it just makes sense to have a Number(i64) variant to cover the everyday usage.
We could imagine some kind of variants for larger numbers, but the toml::Value enum would be much more of a burden (many variants to take care of).

I guess the best way would be to have:

enum Value {
   // other variants ...
   Number(NumberValue),
}

enum NumberValue {
  Unsigned(u64),
  Signed(i64),
  BigUnsigned(u128),
  BigSigned(i128),
  Float(f64),
}

impl TryInto<u64> for NumberValue {
  // If the variant matches, get the value, else raise an error
}

It forces an extra step to get a usable number from the toml::Value, but it covers all kind of numeric types we could want.
Seams to me like a good middle ground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parse Area: Parsing TOML A-serde Area: Serde integration C-bug Category: Things not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants