Skip to content

Improve XmlVersion enumeration: explicit and implicit 1.0 variants#955

Open
Mingun wants to merge 4 commits intotafia:masterfrom
Mingun:better-version
Open

Improve XmlVersion enumeration: explicit and implicit 1.0 variants#955
Mingun wants to merge 4 commits intotafia:masterfrom
Mingun:better-version

Conversation

@Mingun
Copy link
Copy Markdown
Collaborator

@Mingun Mingun commented May 3, 2026

This PR changes XmlVersion from

pub enum XmlVersion {
  V1_0,
  V1_1,
}

to

pub enum XmlVersion {
  Implicit1_0,
  Explicit1_0,
  Explicit1_1,
}

The motivation behind this change is simplification with processing versions. XML specification explicitly allows at most one XML declaration (the <?xml version=...?> part). At the same time the version information in the declaration is mandatory. This leads us to a simple cycle that allows, using one variable, to track the uniqueness of the declaration and obtain values according to the version

let mut reader = Reader::from_str(...);
let mut version = XmlVersion::Implicit1_0;
loop {
  match reader.read_event() {
    // Return error in case of duplicated declaration
    Event::XmlDecl(e) if version == XmlVersion::Implicit1_0 => return Err(...),
    // Return error in case of missing "version"
    Event::XmlDecl(e) => { version = e.xml_version()?; }

    Event::Text(e) => {
      let text = e.xml_content(version)?;
      // ...
    }
    ...
  }
}

Without thus changes you may use Option<XmlVersion>, but then you will forced to use e.xml_content(version.unwrap_or(XmlVersion::V1_0)), which will add check for None for every usage of version in the loop. Usually XML documents a well-formed, so this is just wasting of CPU cycles.

XmlVersion is not a part of any release yet, so it can be changed freely.

@Mingun Mingun requested a review from dralley May 3, 2026 18:22
@codecov-commenter
Copy link
Copy Markdown

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

Codecov Report

❌ Patch coverage is 72.07207% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.47%. Comparing base (a759d65) to head (c0f371d).
⚠️ Report is 18 commits behind head on master.

Files with missing lines Patch % Lines
src/events/attributes.rs 84.48% 9 Missing ⚠️
examples/custom_entities.rs 0.00% 6 Missing ⚠️
examples/read_nodes.rs 0.00% 5 Missing ⚠️
benches/macrobenches.rs 0.00% 4 Missing ⚠️
src/events/mod.rs 62.50% 3 Missing ⚠️
src/lib.rs 85.00% 3 Missing ⚠️
src/de/mod.rs 83.33% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #955      +/-   ##
==========================================
+ Coverage   55.08%   56.47%   +1.38%     
==========================================
  Files          44       44              
  Lines       16911    17648     +737     
==========================================
+ Hits         9316     9966     +650     
- Misses       7595     7682      +87     
Flag Coverage Δ
unittests 56.47% <72.07%> (+1.38%) ⬆️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants