Code works on rust playground but not in test code

PressRex profile image
by PressRex
Code works on rust playground but not in test code

Here is the code in playground: Rust Playground

Here is the code in test code:

#[cfg(test)]
mod serde_tests {
    // use super::*;
    use serde::{Deserialize, Serialize};
    use serde_json;

    #[derive(Debug, Serialize, Deserialize)]
    struct CurrencyRates {
        date: String,
        #[serde(flatten)]
        rates: Rates,
    }

    #[derive(Debug, Serialize, Deserialize)]
    #[serde(untagged)]
    enum Rates {
        Idr {
            #[serde(rename = "idr")]
            idr: CurrencyMap,
        },
        Usd {
            #[serde(rename = "usd")]
            usd: CurrencyMap,
        },
    }

    #[derive(Debug, Serialize, Deserialize)]
    #[serde(untagged)]
    enum CurrencyMap {
        Specific {
            #[serde(rename = "1inch")]
            one_inch: f64,
            aave: f64,
            ada: f64,
        },
        Partial {
            ada: f64,
        },
        Other {
            env: f64,
        },
    }

    #[test]
    fn test_serde() {
        let idr_json = r#"{
       "date": "2025-01-26",
       "idr": {
           "ada": 0.000062981114
       }
   }"#;

        let usd_json = r#"{
       "date": "2025-01-26",
       "usd": {
           "env": 0.22
       }
   }"#;

        let full_json = r#"{
       "date": "2025-01-26",
       "usd": {
           "1inch": 0.00019578941,
           "aave": 1.8799505e-7,
           "ada": 0.000062981114
       }
   }"#;

        let idr_rates: CurrencyRates = serde_json::from_str(idr_json).unwrap();
        let usd_rates: CurrencyRates = serde_json::from_str(usd_json).unwrap();
        let full_rates: CurrencyRates = serde_json::from_str(full_json).unwrap();

        println!("IDR Rates: {:?}", idr_rates);
        println!("USD Rates: {:?}", usd_rates);
        println!("Full Rates: {:?}", full_rates);

        // Serialize back to JSON
        let idr_serialized = serde_json::to_string_pretty(&idr_rates).unwrap();
        let usd_serialized = serde_json::to_string_pretty(&usd_rates).unwrap();

        println!("Serialized IDR Rates:\n{}", idr_serialized);
        println!("Serialized USD Rates:\n{}", usd_serialized);
    }
}

With error:

thread 'forex_impl::exchange_api::serde_tests::test_serde' panicked at src/forex_impl/exchange_api.rs:110:71:
called `Result::unwrap()` on an `Err` value: Error("data did not match any variant of untagged enum Rates", line: 6, column: 4)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test forex_impl::exchange_api::serde_tests::test_serde ... FAILED

failures:

failures:
    forex_impl::exchange_api::serde_tests::test_serde

Here is the toml:

[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
serde_json = "1"
serde = { version = "1", features = ["derive"] }

Cargo version:

$ cargo --version
cargo 1.83.0 (5ffbef321 2024-10-29)

Why does this erring for the exact same code?

2 posts - 2 participants

Read full topic

Source: View source

PressRex profile image
by PressRex

Subscribe to New Posts

Lorem ultrices malesuada sapien amet pulvinar quis. Feugiat etiam ullamcorper pharetra vitae nibh enim vel.

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

Read More