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

Implement deserialization textual content into externally tagged enum variants #541

Merged
merged 6 commits into from
Jan 8, 2023

Conversation

Mingun
Copy link
Collaborator

@Mingun Mingun commented Jan 7, 2023

This is partially address #257. This PR adds an ability to use specially named enum variant $text, which would get a textual content:

enum Choice {
  One,
  Two,
  #[serde(rename = "$text")]
  Text(String),
}

is able to deserialize

<one>...</one>
text
<two>...</two>

into Vec<Choice>:

vec![
  Choice::One,
  Choice::Text("text"),
  Choice::Two,
]

…_enum` test

test_parse_enum test is covered by serde-de::enum_::internally_tagged tests.
Removed enum Animal should be defined as

    #[derive(PartialEq, Debug, Serialize, Deserialize)]
    #[serde(tag = "@type")] // << Added line
    enum Animal {
        Dog,
        Frog(String),
        Ant(Simple),
        Cat { age: usize, name: String },
    }

because enum type is encoded in the "xsi:type" attribute,
which means that enum is internally tagged
failures (9):
    enum_::externally_tagged::text::newtype::cdata
    enum_::externally_tagged::text::newtype::text
    enum_::externally_tagged::text::struct_::cdata
    enum_::externally_tagged::text::struct_::mixed
    enum_::externally_tagged::text::struct_::text
    enum_::externally_tagged::text::tuple::cdata
    enum_::externally_tagged::text::tuple::text
    enum_::externally_tagged::text::unit::cdata
    enum_::externally_tagged::text::unit::text
@Mingun Mingun added enhancement serde Issues related to mapping from Rust types to XML labels Jan 7, 2023
@Mingun Mingun requested a review from dralley January 7, 2023 21:40
@codecov-commenter
Copy link

Codecov Report

Merging #541 (ac3c3d5) into master (967d60d) will increase coverage by 0.36%.
The diff coverage is 48.52%.

@@            Coverage Diff             @@
##           master     #541      +/-   ##
==========================================
+ Coverage   60.91%   61.27%   +0.36%     
==========================================
  Files          33       32       -1     
  Lines       15722    15639      -83     
==========================================
+ Hits         9577     9583       +6     
+ Misses       6145     6056      -89     
Flag Coverage Δ
unittests 61.27% <48.52%> (+0.36%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/de/mod.rs 56.86% <3.33%> (-0.53%) ⬇️
src/de/var.rs 91.54% <84.21%> (+6.44%) ⬆️
src/events/mod.rs 71.64% <0.00%> (-0.75%) ⬇️
src/name.rs 87.82% <0.00%> (-0.72%) ⬇️
src/escapei.rs 12.41% <0.00%> (-0.18%) ⬇️
src/de/simple_type.rs 93.65% <0.00%> (+0.77%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@dralley dralley merged commit 662fc24 into tafia:master Jan 8, 2023
@Mingun Mingun deleted the de-enums branch January 8, 2023 06:51
@Mingun Mingun mentioned this pull request Jan 30, 2023
13 tasks
Mingun added a commit to Mingun/quick-xml that referenced this pull request Mar 6, 2023
crapStone added a commit to Calciumdibromid/CaBr2 that referenced this pull request Mar 16, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [quick-xml](https://github.com/tafia/quick-xml) | dependencies | minor | `0.27.1` -> `0.28.0` |

---

### Release Notes

<details>
<summary>tafia/quick-xml</summary>

### [`v0.28.0`](https://github.com/tafia/quick-xml/blob/HEAD/Changelog.md#&#8203;0280----2023-03-13)

[Compare Source](tafia/quick-xml@v0.27.1...v0.28.0)

##### New Features

-   [#&#8203;541]: (De)serialize specially named `$text` enum variant in [externally tagged]
    enums to / from textual content
-   [#&#8203;556]: `to_writer` and `to_string` now accept `?Sized` types
-   [#&#8203;556]: Add new `to_writer_with_root` and `to_string_with_root` helper functions
-   [#&#8203;520]: Add methods `BytesText::inplace_trim_start` and `BytesText::inplace_trim_end`
    to trim leading and trailing spaces from text events
-   [#&#8203;565]: Allow deserialize special field names `$value` and `$text` into borrowed
    fields when use serde deserializer
-   [#&#8203;568]: Rename `Writter::inner` into `Writter::get_mut`
-   [#&#8203;568]: Add method `Writter::get_ref`
-   [#&#8203;569]: Rewrite the `Reader::read_event_into_async` as an async fn, making the future `Send` if possible.
-   [#&#8203;571]: Borrow element names (`<element>`) when deserialize with serde.
    This change allow to deserialize into `HashMap<&str, T>`, for example
-   [#&#8203;573]: Add basic support for async byte writers via tokio's `AsyncWrite`.

##### Bug Fixes

-   [#&#8203;537]: Restore ability to deserialize attributes that represents XML namespace
    mappings (`xmlns:xxx`) that was broken since [#&#8203;490]
-   [#&#8203;510]: Fix an error of deserialization of `Option<T>` fields where `T` is some
    sequence type (for example, `Vec` or tuple)
-   [#&#8203;540]: Fix a compilation error (probably a rustc bug) in some circumstances.
    `Serializer::new` and `Serializer::with_root` now accepts only references to `Write`r.
-   [#&#8203;520]: Merge consequent (delimited only by comments and processing instructions)
    texts and CDATA when deserialize using serde deserializer. `DeEvent::Text` and
    `DeEvent::CData` events was replaced by `DeEvent::Text` with merged content.
    The same behavior for the `Reader` does not implemented (yet?) and should be
    implemented manually
-   [#&#8203;562]: Correctly set minimum required version of memchr dependency to 2.1
-   [#&#8203;565]: Correctly set minimum required version of tokio dependency to 1.10
-   [#&#8203;565]: Fix compilation error when build with serde <1.0.139

[externally tagged]: https://serde.rs/enum-representations.html#externally-tagged

[#&#8203;490]: tafia/quick-xml#490

[#&#8203;510]: tafia/quick-xml#510

[#&#8203;520]: tafia/quick-xml#520

[#&#8203;537]: tafia/quick-xml#537

[#&#8203;540]: tafia/quick-xml#540

[#&#8203;541]: tafia/quick-xml#541

[#&#8203;556]: tafia/quick-xml#556

[#&#8203;562]: tafia/quick-xml#562

[#&#8203;565]: tafia/quick-xml#565

[#&#8203;568]: tafia/quick-xml#568

[#&#8203;569]: tafia/quick-xml#569

[#&#8203;571]: tafia/quick-xml#571

[#&#8203;573]: tafia/quick-xml#573

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42LjAiLCJ1cGRhdGVkSW5WZXIiOiIzNS42LjAifQ==-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1818
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants