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

Use $value and $unflatten together #430

Closed
tyilo opened this issue Jul 19, 2022 · 1 comment · Fixed by #490
Closed

Use $value and $unflatten together #430

tyilo opened this issue Jul 19, 2022 · 1 comment · Fixed by #490
Assignees
Labels
bug help wanted serde Issues related to mapping from Rust types to XML

Comments

@tyilo
Copy link

tyilo commented Jul 19, 2022

I have some XML that looks like:

<root>
  <state>B</state>
</root>

which I would like to represent in rust as:

enum State {
  A,
  B,
  C,
}

struct Root {
  state: State,
}

I have tried combining $unflatten and $value to make this happen, but it doesn't seem to work:

Cargo.toml:

[package]
name = "quick-xml-test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.139", features = ["derive"] }
quick-xml = { version = "0.23.0", features = ["serialize" ] }

src/main.rs:

mod working {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Deserialize, Serialize)]
    enum State {
        A,
        B,
        C,
    }

    #[derive(Debug, Deserialize, Serialize)]
    struct StateOuter {
        #[serde(rename = "$value")]
        state: State,
    }

    #[derive(Debug, Deserialize, Serialize)]
    pub struct Root {
        state: StateOuter,
    }
}

mod not_working {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Deserialize, Serialize)]
    #[serde(rename = "$value")]
    enum State {
        A,
        B,
        C,
    }

    #[derive(Debug, Deserialize, Serialize)]
    pub struct Root {
        #[serde(rename = "$unflatten=state")]
        state: State,
    }
}

fn main() {
    let xml = "<root><state>B</state></root>";
    println!("Deserialized (working): {:?}", quick_xml::de::from_str::<working::Root>(&xml));
    println!("Deserialized (not working): {:?}", quick_xml::de::from_str::<not_working::Root>(&xml));
}

Output:

Deserialized (working): Ok(Root { state: StateOuter { state: B } })
Deserialized (not working): Err(Custom("unknown variant `state`, expected one of `A`, `B`, `C`"))

Is it possible to do without introducing the extra StateOuter struct?

@Mingun
Copy link
Collaborator

Mingun commented Jul 19, 2022

$unflatten was a wrong feature, that is needed only for serializing and means forcely serialize field as an element (instead of attribute). I'll plan to remove it in next releases and made serialization-as-element to be a default and turning on serialization-as-attribute by a special mark.

As I said in #429, enums support leaves much to be desired. I wrote there how you can help.

@Mingun Mingun added bug help wanted serde Issues related to mapping from Rust types to XML labels Jul 19, 2022
@Mingun Mingun self-assigned this Sep 7, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 1, 2022
This prefix just does nothing. Deserialzier already works as with them, out-of-box.
Support for namespaces will be done in another PR

This commit reverts 8cd971e

Removed tests covered by following existing tests:
- test_parse_unflatten_field        - serde-de::struct_::elements
- test_issue305_unflatten_namespace - serde-de::struct_::namespaces
- test_issue305_unflatten_nesting   - serde-de::nested_struct

Fixes tafia#430
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 1, 2022
Fixes tafia#252 - ok
Fixes tafia#280 - ok
Fixes tafia#287 - ok
Fixes tafia#343 - ok
Fixes tafia#346 - not ok
Fixes tafia#361 - ok
Partially addresses tafia#368
Fixes tafia#429 - ok
Fixes tafia#430 - ok
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 1, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 24, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 25, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 26, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests

Co-authored-by: Daniel Alley <dalley@redhat.com>
@Mingun Mingun closed this as completed in 459b9bf Oct 26, 2022
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
This prefix just does nothing. Deserialzier already works as with them, out-of-box.
Support for namespaces will be done in another PR

This commit reverts 8cd971e

Removed tests covered by following existing tests:
- test_parse_unflatten_field        - serde-de::struct_::elements
- test_issue305_unflatten_namespace - serde-de::struct_::namespaces
- test_issue305_unflatten_nesting   - serde-de::nested_struct

Fixes tafia#430
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests

Co-authored-by: Daniel Alley <dalley@redhat.com>
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
This prefix just does nothing. Deserialzier already works as with them, out-of-box.
Support for namespaces will be done in another PR

This commit reverts 8cd971e

Removed tests covered by following existing tests:
- test_parse_unflatten_field        - serde-de::struct_::elements
- test_issue305_unflatten_namespace - serde-de::struct_::namespaces
- test_issue305_unflatten_nesting   - serde-de::nested_struct

Fixes tafia#430
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests

Co-authored-by: Daniel Alley <dalley@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants