Problem deserializing a struct that can be serialized
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 38/100
Research direction
Start with the issue’s minimal Rust example and its failing deserialization tests, especially the Source enum and the source field’s serde(rename = "Source") attribute. Check quick-xml’s deserialization behavior for repeated XML elements and enum values; done means the shown XML deserializes to the expected Sources values while the serialization test still passes.
Written by the indexing model from the issue text.
Description
I have a Problem de serializing the following xml
<Sources>
<Source>A</Source>
<Source>B</Source>
</Sources>
This is my current attempt
/*
[dependencies]
serde = { version = "1.0", features = ["derive"] }
quick-xml = {version="0.31", features = ["serialize"]}
*/
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Sources {
#[serde(rename = "Source")]
source: Vec<Source>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum Source {
A,
B,
}
#[cfg(test)]
mod tests {
use quick_xml::de::from_str;
use super::*;
#[test]
fn test_serialize_sources() {
let sources = Sources {
source: vec![Source::A, Source::B],
};
let xml = r#"<Sources><Source>A</Source><Source>B</Source></Sources>"#;
let serialized = to_string(&sources).expect("Failed to serialize sources"); // Use the to_string function from quick_xml
assert_eq!(serialized, xml);
}
#[test]
fn test_deserialize_sources(){
let xml = r#"<Sources><Source>A</Source><Source>B</Source></Sources>"#;
let sources = Sources{ source: vec![Source::A, Source::B]};
let deserialize = from_str::<Sources>(&xml).expect("Failed to deserialize sources");
assert_eq!(deserialize, sources);
}
#[test]
fn test_deserialize_source(){
let xml = r#"<Sources><Source>B</Source></Sources>"#;
let sources = Sources{ source: vec![Source::B]};
let deserialize = from_str::<Sources>(&xml).expect("Failed to deserialize sources");
assert_eq!(deserialize, sources);
}
}
which fails with the following error
running 3 tests
test tests::test_deserialize_sources ... FAILED
test tests::test_serialize_sources ... ok
test tests::test_deserialize_source ... FAILED
failures:
---- tests::test_deserialize_sources stdout ----
thread 'tests::test_deserialize_sources' panicked at src/main.rs:45:53:
Failed to deserialize sources: Custom("unknown variant `Source`, expected `A` or `B`")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
---- tests::test_deserialize_source stdout ----
thread 'tests::test_deserialize_source' panicked at src/main.rs:54:53:
Failed to deserialize sources: Custom("unknown variant `Source`, expected `A` or `B`")
failures:
tests::test_deserialize_source
tests::test_deserialize_sources
test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--bin example_issue_quick_xml`
❯
- Dominant language
- Rust
- Stars
- 1.6k
- Forks
- 299
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 9
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tafia/quick-xml
-
Parser accepts an empty element or attribute namePossibly taken @ychampion claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
DecodingReader::set_encoding panics when the XML declaration is longer than 64 bytesPossibly taken @emschwartz claimed this 4 days ago. Openbug encoding
Difficulty 4/5 3-5 days Newbie friendliness 30/100
tafia/quick-xml#1040 · 1 comment ·
Maintainers usually reply within 1 day
-
encoding optimization
Difficulty 4/5 3-5 days Newbie friendliness 55/100
tafia/quick-xml#1039 · 1 comment ·
Maintainers usually reply within 1 day
-
optimization question
Difficulty 4/5 3-5 days Newbie friendliness 52/100
Maintainers usually reply within 1 day
-
enhancement
Difficulty 4/5 1-2 days Newbie friendliness 53/100
tafia/quick-xml#1035 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
storytold/photocraft#14822 ·
Maintainers usually reply within 1 day
-
bug css
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
dginev/latexml-oxide#853 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 2 days
-
[Bug] export_build_environment admits its output directory only after the export ran in the build computerPossibly taken @viniciusdsandrade claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day