Whitespaces around character entities

Đang mở
#900 3 bình luận 2 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
backend

Hướng nghiên cứu

Start by reading the Config::trim_text behavior and the deserializer path that handles text and decoded character entities; the report includes a reproducer using NsReader and quick_xml::de::Deserializer::borrowing. Check existing tests for trimming and entity decoding, then determine whether a configuration option can preserve spaces around entities while trimming only leading and trailing text. Done means the reproducer retains those internal spaces without losing global edge trimming.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

help wanted serde

I maintain a large codebase that used quick-xml for years across hundreds of structs.

After switching from 0.37.5 to 0.38, I hit the already well-known whitespace issues. 0.38.1 introduced Config::trim_text which saved me from touching thousands of fields, but now I found the next caveat: XML character entities make trim_text(true) trim more than needed:

#!/usr/bin/env cargo
---
[dependencies]
serde = { version = "1", features = ["derive"] }
quick-xml = { version = "0.38", features = ["serialize"] }
quick-xml-old = { package = "quick-xml", version = "0.37", features = ["serialize"] }
---

use quick_xml::reader::NsReader;
use serde::Deserialize;
use std::result::Result;

#[derive(Debug, Deserialize)]
struct Root {
    bar: String,
}

pub fn from_str_trim_text<'de, T>(s: &'de str) -> Result<T, Box<dyn std::error::Error>>
where
    T: Deserialize<'de>,
{
    let mut reader = NsReader::from_str(s);
    let config = reader.config_mut();

    config.trim_text(true); // Trims also spaces around decoded &amp;
    config.expand_empty_elements = true;

    let mut de = quick_xml::de::Deserializer::borrowing(reader);
    Ok(T::deserialize(&mut de)?)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let xml = r#"
    <root>
        <bar>
            Foo &amp; Bar  &amp;  Baz Qux   Quux
        </bar>
    </root>"#;

    let old: Root = quick_xml_old::de::from_str(xml)?;
    let new: Root = quick_xml::de::from_str(xml)?;
    let new_trimming: Root = from_str_trim_text(xml)?;

    println!("Old: {}", old.bar); // "Foo & Bar  &  Baz Qux   Quux"
    println!("New: {}", new.bar); // "\n            Foo & Bar  &  Baz Qux   Quux\n        "
    println!("New trimming: {}", new_trimming.bar); // "Foo&Bar&Baz Qux   Quux"

    Ok(())
}

(cargo +nightly -Zscript example.rs)

I know this is documented behavior (thank you), so I'm not calling it a bug. I’m looking for a solution to preserve "internal" spaces (e.g, around the decoded entities) while still trimming leading/trailing whitespace globally.

The only workaround that comes to my mind atm is either introducing a newtype or using deserialize_with, which are both rather error-prone in a mature codebase like mine and require tons of work.

If there is no existing solution right now, I could try to implement a feature (e.g., a new config option?) if the maintainers think it is a good idea.

Ngôn ngữ chính
Rust
Star
1.6k
Fork
299
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
9

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của tafia/quick-xml

Tất cả issue của tafia/quick-xml

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.