xsd_parser/quick_xml/
attributes.rs

1use quick_xml::events::{
2    attributes::{AttrError, Attribute},
3    BytesStart,
4};
5
6use super::{Error, SerializeBytes};
7
8/// Write the passed `attrib` to the passed `bytes` object.
9///
10/// # Errors
11/// An error is returned of the attribute could not be serialized.
12pub fn write_attrib<T>(bytes: &mut BytesStart<'_>, name: &str, attrib: &T) -> Result<(), Error>
13where
14    T: SerializeBytes,
15{
16    if let Some(attrib) = SerializeBytes::serialize_bytes(attrib)? {
17        bytes.push_attribute((name, attrib));
18    }
19
20    Ok(())
21}
22
23/// Write the passed `attrib`  to the passed `bytes` object.
24///
25/// # Errors
26/// An error is returned of the attribute could not be serialized.
27pub fn write_attrib_opt<T>(
28    bytes: &mut BytesStart<'_>,
29    name: &str,
30    attrib: &Option<T>,
31) -> Result<(), Error>
32where
33    T: SerializeBytes,
34{
35    let Some(attrib) = attrib else {
36        return Ok(());
37    };
38
39    write_attrib(bytes, name, attrib)
40}
41
42/// Returns an iterator that yields all attributes of the passed `bytes_start`
43/// object, except the `xmlns` attributes.
44pub fn filter_xmlns_attributes<'a>(
45    bytes_start: &'a BytesStart<'_>,
46) -> impl Iterator<Item = Result<Attribute<'a>, AttrError>> {
47    bytes_start.attributes().filter(|attrib| {
48        let Ok(attrib) = attrib else {
49            return true;
50        };
51
52        attrib.key.0 != b"xmlns" && !attrib.key.0.starts_with(b"xmlns:")
53    })
54}