dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
964 stars 164 forks source link

Fail to deserialize i128 in internally tagged enum #391

Open gpollo opened 1 year ago

gpollo commented 1 year ago

Hi, I'm having trouble de-serializing i128 when it is in a tagged enum. Here's a MWE of the bug. Any ideas why this might happen? Am I doing something wrong? It works when I replace the i128 with a i64.

$ cargo run
Error: i128 is not supported
main.rs ```rust use indoc::indoc; use anyhow::Result; use serde::Deserialize; #[derive(Clone, Debug, Deserialize)] pub struct IntegerType { pub value: T, } #[derive(Clone, Debug, Deserialize)] #[serde(tag = "type")] pub enum Type { // replacing i128/u128 by i64/u64 works #[serde(rename(deserialize = "signed"))] Signed(IntegerType), #[serde(rename(deserialize = "unsigned"))] Unsigned(IntegerType), } fn main() -> Result<()> { let yaml = indoc! {" type: signed value: 123 "}; let yaml: Type = serde_yaml::from_str(yaml)?; println!("{:#?}", yaml); Ok(()) } ```
Cargo.lock ```toml [package] name = "test-serde-yaml" version = "0.1.0" edition = "2021" [dependencies] anyhow = "1" indoc = "2" serde_yaml = "0.9" serde = { version = "1", features = ["derive"] } ```
gpollo commented 1 year ago

Did the same test with serde_json, seems like it's not working there either. Might be a problem with serde itself.

main.rs ```rust use indoc::indoc; use anyhow::Result; use serde::Deserialize; #[derive(Clone, Debug, Deserialize)] pub struct IntegerType { pub value: T, } #[derive(Clone, Debug, Deserialize)] #[serde(tag = "type")] pub enum Type { // replacing i128/u128 by i64/u64 works #[serde(rename(deserialize = "signed"))] Signed(IntegerType), #[serde(rename(deserialize = "unsigned"))] Unsigned(IntegerType), } fn main() -> Result<()> { let yaml: Result = serde_yaml::from_str(indoc! {" type: signed value: 123 "}); let json: Result = serde_json::from_str(indoc! {" { \"type\": \"signed\", \"value\": 123 } "}); println!("{:#?}", yaml); println!("{:#?}", json); Ok(()) } ```
Cargo.lock ```toml [package] name = "test-serde-yaml" version = "0.1.0" edition = "2021" [dependencies] anyhow = "1" indoc = "2" serde_json = "1.0" serde_yaml = "0.9" serde = { version = "1", features = ["derive"] } ```

EDIT: seems related to https://github.com/serde-rs/serde/issues/2576