Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Can't deserialize into tuple struct #334

Closed
PSeitz opened this issue Sep 11, 2019 · 3 comments
Closed

Can't deserialize into tuple struct #334

PSeitz opened this issue Sep 11, 2019 · 3 comments
Labels
A-parsing Deserialization

Comments

@PSeitz
Copy link

PSeitz commented Sep 11, 2019

#[derive(Serialize, Deserialize, Debug, Clone)]
struct MyData {
    is_nice: bool
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WrappedMap(HashMap<String, MyData>);

#[test]
fn name() {

    let indices = r#"
        ["commonness"]
            is_nice = true
    "#;

    let _conf:HashMap<String, MyData> = toml::from_str(indices).unwrap();
    let _conf:WrappedMap = toml::from_str(indices).unwrap();


}

It can't deserialize into the tuple struct:
message: "invalid type: map, expected tuple struct WrappedMap", key: []

These wrappers are quite common in rust and are supported by serde_json.

@ehuss
Copy link
Collaborator

ehuss commented Sep 12, 2019

I think you need #[serde(transparent)] on WrappedMap.

It looks like serde_json treats newtype_structs as "transparent" by default. toml should probably do the same. It looks to be specific to not working as a top-level table.

@ehuss ehuss added the A-parsing Deserialization label Sep 12, 2019
luckysori added a commit to comit-network/comit-rs that referenced this issue Nov 15, 2019
The solution could be simpler if
toml-rs/toml-rs#334 were fixed. It
would be even simpler if `serde` supported flattening enum variants,
but it doesn't: serde-rs/serde#1402.

Also:

- Rename config file field from `allowed_foreign_origins` to
  `allowed_origins` since being under the CORS section already
  indicates that it refers to foreign origins.
luckysori added a commit to comit-network/comit-rs that referenced this issue Nov 15, 2019
The solution could be simpler if
toml-rs/toml-rs#334 were fixed. It
would be even simpler if `serde` supported flattening enum variants,
but it doesn't: serde-rs/serde#1402.

Also:

- Rename config file field from `allowed_foreign_origins` to
  `allowed_origins` since being under the CORS section already
  indicates that it refers to foreign origins.
@TonalidadeHidrica
Copy link

A similar issue happened for a HashMap with tuple-struct keys. Adding #[serde(transparent)] resolved the issue. Is the reason same?

use std::collections::HashMap;

use serde::Deserialize;

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Deserialize)]
// #[serde(transparent)]
struct S(String);
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Deserialize)]
struct T {
    a: u64
}

fn main() {
    let map: HashMap<S, T> = serde_json::from_str(r#"
        {
            "abc": {"a": 123},
            "def": {"a": 456}
        }
    "#).unwrap();
    println!("{:?}", map);
    let map: HashMap<S, T> = toml::from_str(r#"
        [abc]
        a = 123
        [def]
        a = 456
    "#).unwrap();
    println!("{:?}", map);
}
{S("abc"): T { a: 123 }, S("def"): T { a: 456 }}
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { inner: ErrorInner { kind: Custom, line: Some(3), col: 8, at: Some(39), message: "invalid type: string \"abc\", expected tuple struct S", key: [] } }', src/main.rs:25:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@epage
Copy link
Member

epage commented Sep 23, 2022

Maintenance of this crate has moved to the https://github.com/toml-rs/toml repo. As a heads up, we plan to move toml to be on top of toml_edit, see toml-rs/toml#340.

Closing this out. If this is still a problem, feel free to recreate this issue in the new repo.

@epage epage closed this as completed Sep 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-parsing Deserialization
Projects
None yet
Development

No branches or pull requests

4 participants