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

Fix boolean parsing and make SimpleTypeSerializer and SimpleTypeDeserializer public #826

Merged
merged 8 commits into from
Oct 20, 2024

Conversation

Mingun
Copy link
Collaborator

@Mingun Mingun commented Oct 19, 2024

This PR makes SimpleTypeSerializer and SimpleTypeDeserializer public, as requested in #824. It also includes two other changes.

When checking documentation for the SimpleTypeDeserializer I noticed, that we parse more boolean representations than described in the XML Schema specification. I do not see much reasons to keep that additional variants anymore, also because of the next implemented change. If you need more representations, you can use the wrapper type or #[serde(deserialize_with)] attribute.

The last change that this PR contains is final transfer of the responsibility of generating errors about mismatched types to the type being deserialized. That means that we no more need DeError::InvalidInt, DeError::InvalidFloat and DeError::InvalidBoolean errors, so their are removed. The changes can be notified in this code snippet:

#[test]
fn integer() {
    quick_xml::de::from_str::<usize>("string").unwrap();
}

#[test]
fn float() {
    quick_xml::de::from_str::<f64>("string").unwrap();
}

#[test]
fn boolean() {
    quick_xml::de::from_str::<bool>("trues").unwrap();
}

Before:

---- temp::integer stdout ----
thread 'temp::integer' panicked at tests/serde-issues.rs:560:48:
called `Result::unwrap()` on an `Err` value: InvalidInt(ParseIntError { kind: InvalidDigit })

---- temp::float stdout ----
thread 'temp::float' panicked at tests/serde-issues.rs:565:46:
called `Result::unwrap()` on an `Err` value: InvalidFloat(ParseFloatError { kind: Invalid })

---- temp::boolean stdout ----
thread 'temp::boolean' panicked at tests/serde-issues.rs:570:46:
called `Result::unwrap()` on an `Err` value: InvalidBoolean("trues")

After:

---- temp::integer stdout ----
thread 'temp::integer' panicked at tests/serde-issues.rs:560:48:
called `Result::unwrap()` on an `Err` value: Custom("invalid type: string \"string\", expected usize")

---- temp::float stdout ----
thread 'temp::float' panicked at tests/serde-issues.rs:565:46:
called `Result::unwrap()` on an `Err` value: Custom("invalid type: string \"string\", expected f64")

---- temp::boolean stdout ----
thread 'temp::boolean' panicked at tests/serde-issues.rs:570:46:
called `Result::unwrap()` on an `Err` value: Custom("invalid type: string \"trues\", expected a boolean")

Closes #824

@Mingun Mingun added enhancement serde Issues related to mapping from Rust types to XML labels Oct 19, 2024
@Mingun Mingun requested a review from dralley October 19, 2024 13:29
@codecov-commenter
Copy link

codecov-commenter commented Oct 19, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 69.09091% with 34 lines in your changes missing coverage. Please review.

Project coverage is 60.05%. Comparing base (a9391f3) to head (83d2957).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
src/de/simple_type.rs 71.18% 17 Missing ⚠️
src/de/mod.rs 44.44% 15 Missing ⚠️
src/de/key.rs 75.00% 1 Missing ⚠️
src/utils.rs 95.00% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #826      +/-   ##
==========================================
- Coverage   60.21%   60.05%   -0.16%     
==========================================
  Files          41       41              
  Lines       16021    16031      +10     
==========================================
- Hits         9647     9628      -19     
- Misses       6374     6403      +29     
Flag Coverage Δ
unittests 60.05% <69.09%> (-0.16%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

…omicDeserializer

Content::deserialize_all can be replaced by Content::deserialize_item helper, because
when it is used, the self.offset is 0
Helper in CowRef will be reused in next commit
…lean or number

Call visit_borrowed_str / visit_str / visit_string instead. It is responsibility of
the type to return an error if it is not able to process such data; the deserializer
does the conversion only because technically all things in XML stored as strings
@Mingun Mingun merged commit a6c5fc5 into tafia:master Oct 20, 2024
7 checks passed
@Mingun Mingun deleted the more-deserializers branch October 20, 2024 19:35
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.

How to deserialize a Struct with only Attributes from BytesStart?
3 participants