ferroxl 0.2.0

ferroxl: read and write Excel 2007 xlsx/xlsm files with an openpyxl-compatible API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! Shared XML tools (`openpyxl/xml/functions.py`, `namespace.py`).
//!
//! The Python original leans on ElementTree both for building output and for parsing
//! input, exposing a handful of helpers (`get_document_content`, `pretty_indent`,
//! `start_tag`, `tag`, `safe_iterator`, `ConditionalElement`). This module provides the
//! same surface: a small DOM for building parts, a compact streaming writer, and a
//! namespace-aware parser that maps to ElementTree-style `{ns}tag` selectors.

use std::collections::BTreeMap;
use std::io::Read;

use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::{Reader, Writer as XmlWriterInner};

use crate::exceptions::{Error, Result};
use crate::xml::constants::*;

/// Prefixes registered by `openpyxl.xml.functions.register_namespace`.
///
/// ElementTree looks these up when serialising, so a `{SHEET_MAIN_NS}workbook` tag
/// becomes `s:workbook`.
pub const REGISTERED_PREFIXES: [(&str, &str); 10] = [
    (DCTERMS_PREFIX, DCTERMS_NS),
    ("dcmitype", "http://purl.org/dc/dcmitype/"),
    ("cp", COREPROPS_NS),
    ("c", CHART_NS),
    ("a", DRAWING_NS),
    ("s", SHEET_MAIN_NS),
    ("r", REL_NS),
    ("vt", VTYPES_NS),
    ("xdr", SHEET_DRAWING_NS),
    ("cdr", CHART_DRAWING_NS),
];

/// A namespace-qualified name, stored ElementTree-style as `{ns}local`.
///
/// Names without a namespace are stored bare (`"dimension"`).
pub type QName = String;

/// An in-memory XML element, mirroring `xml.etree.ElementTree.Element`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Element {
    /// Element tag, either `{ns}local` or bare.
    pub tag: QName,
    /// Attributes in insertion order; keys may be `{ns}local` or bare.
    pub attributes: Vec<(String, String)>,
    /// Text content directly under this element.
    pub text: Option<String>,
    /// Text following this element (used for pretty-printing).
    pub tail: Option<String>,
    /// Child elements.
    pub children: Vec<Element>,
}

impl Element {
    /// Create an element with the given qualified or bare tag.
    pub fn new(tag: impl Into<QName>) -> Self {
        Element {
            tag: tag.into(),
            ..Default::default()
        }
    }

    /// Create an element with attributes, preserving their order.
    ///
    /// The tag and both attribute fields are anything that renders to a string, so callers
    /// can pass computed values without converting them by hand.
    pub fn with_attributes<T, I, K, V>(tag: T, attrs: I) -> Self
    where
        T: std::fmt::Display,
        I: IntoIterator<Item = (K, V)>,
        K: std::fmt::Display,
        V: std::fmt::Display,
    {
        let mut el = Element::new(tag.to_string());
        for (k, v) in attrs {
            el.set(k.to_string(), v.to_string());
        }
        el
    }

    /// Set an attribute, replacing any existing value for that key.
    pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
        let key = key.into();
        let value = value.into();
        match self.attributes.iter_mut().find(|(k, _)| *k == key) {
            Some(slot) => slot.1 = value,
            None => self.attributes.push((key, value)),
        }
        self
    }

    /// Get an attribute value by qualified or bare key.
    pub fn get(&self, key: impl AsRef<str>) -> Option<&str> {
        let key = key.as_ref();
        self.attributes
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v.as_str())
    }

    /// Set the text content.
    pub fn set_text(&mut self, text: impl Into<String>) -> &mut Self {
        self.text = Some(text.into());
        self
    }

    /// Append a child element.
    pub fn append(&mut self, child: Element) -> &mut Self {
        self.children.push(child);
        self
    }

    /// Insert a child at `index`.
    pub fn insert(&mut self, index: usize, child: Element) -> &mut Self {
        self.children.insert(index, child);
        self
    }

    /// First direct child matching `path`, where `path` may be a bare tag name or an
    /// ElementTree path such as `*/{ns}workbookView`.
    pub fn find(&self, path: impl AsRef<str>) -> Option<&Element> {
        let path = path.as_ref();
        let (ancestor, tag) = match split_path(path) {
            Some((a, t)) => (Some(a), t),
            None => (None, path),
        };
        if let Some(ancestor) = ancestor {
            if !self.matches_step(ancestor) {
                return None;
            }
        }
        self.children.iter().find(|child| child.matches_tag(tag))
    }

    /// Mutable variant of [`Element::find`].
    pub fn find_mut(&mut self, path: impl AsRef<str>) -> Option<&mut Element> {
        let path = path.as_ref();
        let (ancestor, tag) = match split_path(path) {
            Some((a, t)) => (Some(a), t),
            None => (None, path),
        };
        if let Some(ancestor) = ancestor {
            if !self.matches_step(ancestor) {
                return None;
            }
        }
        self.children
            .iter_mut()
            .find(|child| child.matches_tag(tag))
    }

    fn matches_step(&self, step: &str) -> bool {
        step == "*" || self.matches_tag(step)
    }

    /// Whether this element's tag answers to `tag`.
    ///
    /// An unqualified query matches by local name. ElementTree would demand the `{ns}`
    /// form, but the writer's parts are all in one namespace and its own tests read far
    /// more clearly without the prefix repeated on every lookup.
    fn matches_tag(&self, tag: &str) -> bool {
        self.tag == tag
            || (super::constants::namespace_of(tag).is_none()
                && super::constants::local_name(&self.tag) == tag)
    }

    /// All direct children matching `tag`.
    pub fn find_all(&self, tag: impl AsRef<str>) -> Vec<&Element> {
        let tag = tag.as_ref();
        self.children
            .iter()
            .filter(|c| c.matches_tag(tag))
            .collect()
    }

    /// Mutable variant of [`Element::find_all`].
    pub fn find_all_mut(&mut self, tag: impl AsRef<str>) -> Vec<&mut Element> {
        let tag = tag.as_ref();
        self.children
            .iter_mut()
            .filter(|c| c.matches_tag(tag))
            .collect()
    }

    /// All direct children, in document order.
    pub fn children(&self) -> Vec<&Element> {
        self.children.iter().collect()
    }

    /// Mutable variant of [`Element::children`].
    pub fn children_mut(&mut self) -> Vec<&mut Element> {
        self.children.iter_mut().collect()
    }

    /// Text of the first descendant matching `path`, or `default` when it has none.
    pub fn find_text(&self, path: impl AsRef<str>, default: &str) -> String {
        match self.find(path.as_ref()) {
            Some(node) => node.text.clone().unwrap_or_else(|| default.to_string()),
            None => default.to_string(),
        }
    }

    /// Text of the first descendant matching `path`, if both exist.
    pub fn find_text_opt(&self, path: impl AsRef<str>) -> Option<String> {
        self.find(path.as_ref()).and_then(|node| node.text.clone())
    }

    /// Alias of [`Element::find_all`], named for parity with the Python helper.
    pub fn iter_tag(&self, tag: impl AsRef<str>) -> Vec<&Element> {
        self.find_all(tag)
    }

    /// Serialise the tree to compact XML bytes.
    pub fn to_string_bytes(&self) -> Vec<u8> {
        serialize(self)
    }

    /// Serialise the tree to indented XML.
    ///
    /// The indentation mirrors `xml.etree.ElementTree.indent`, which openpyxl applies to
    /// the parts it builds with an ElementTree rather than the streaming writer.
    pub fn to_pretty_string(&self) -> String {
        let mut namespaces: Vec<String> = Vec::new();
        collect_namespaces(self, &mut namespaces);
        let mut prefixes: BTreeMap<String, String> = BTreeMap::new();
        let mut declarations: Vec<(String, String)> = Vec::new();
        for ns in &namespaces {
            if ns == super::constants::XML_NS {
                // `xml:` is bound by the XML specification, so it is not declared.
                prefixes.insert(ns.clone(), "xml".to_string());
                continue;
            }
            let prefix = REGISTERED_PREFIXES
                .iter()
                .find(|(_, uri)| *uri == ns)
                .map(|(prefix, _)| (*prefix).to_string())
                .unwrap_or_else(|| format!("ns{}", prefixes.len()));
            declarations.push((prefix.clone(), ns.clone()));
            prefixes.insert(ns.clone(), prefix);
        }
        let mut out = String::new();
        write_pretty(&mut out, self, &prefixes, &declarations, 0, &mut false);
        out
    }
}

/// Collect every namespace URI used by the tree, in first-seen order.
fn collect_namespaces(el: &Element, out: &mut Vec<String>) {
    if let Some(ns) = namespace_of(&el.tag) {
        if !out.iter().any(|x| x == ns) {
            out.push(ns.to_string());
        }
    }
    for (key, _) in &el.attributes {
        if let Some(ns) = namespace_of(key) {
            if !out.iter().any(|x| x == ns) {
                out.push(ns.to_string());
            }
        }
    }
    for child in &el.children {
        collect_namespaces(child, out);
    }
}

/// Write one element and its subtree with indentation.
///
/// `first_child_of_line` tracks whether the next write needs to open a new line, which is
/// how ElementTree decides between `<a><b/></a>` and `<a>\n  <b/>\n</a>`.
fn write_pretty(
    out: &mut String,
    el: &Element,
    prefixes: &BTreeMap<String, String>,
    declarations: &[(String, String)],
    depth: usize,
    _is_root: &mut bool,
) {
    const INDENT: &str = "    ";
    for _ in 0..depth {
        out.push_str(INDENT);
    }
    out.push('<');
    out.push_str(&render_qname(&el.tag, prefixes, true));
    if depth == 0 {
        for (prefix, uri) in declarations {
            out.push_str(&format!(" xmlns:{prefix}=\"{}\"", escape_attribute(uri)));
        }
    }
    for (key, value) in &el.attributes {
        out.push_str(&format!(
            " {}=\"{}\"",
            render_qname(key, prefixes, false),
            escape_attribute(value)
        ));
    }
    let has_children = !el.children.is_empty();
    let text = el.text.as_deref().filter(|text| !text.is_empty());
    match (has_children, text) {
        (false, None) => out.push_str("/>"),
        (false, Some(text)) => {
            out.push('>');
            out.push_str(&escape_text(text));
            out.push_str("</");
            out.push_str(&render_qname(&el.tag, prefixes, true));
            out.push('>');
        }
        (true, text) => {
            out.push('>');
            if let Some(text) = text {
                out.push_str(&escape_text(text));
            }
            for child in &el.children {
                out.push('\n');
                write_pretty(out, child, prefixes, declarations, depth + 1, _is_root);
            }
            out.push('\n');
            for _ in 0..depth {
                out.push_str(INDENT);
            }
            out.push_str("</");
            out.push_str(&render_qname(&el.tag, prefixes, true));
            out.push('>');
        }
    }
}

fn render_qname(name: &str, prefixes: &BTreeMap<String, String>, element: bool) -> String {
    match namespace_of(name) {
        Some(ns) => {
            let local = super::constants::local_name(name);
            match prefixes.get(ns) {
                Some(prefix) => {
                    if element {
                        format!("{prefix}:{local}")
                    } else {
                        // Unprefixed attributes are not in the default namespace; an
                        // OOXML `r:id` must carry its prefix explicitly.
                        format!("{prefix}:{local}")
                    }
                }
                None => local.to_string(),
            }
        }
        None => name.to_string(),
    }
}

/// Escape text content the way `xml.sax.saxutils.escape` does.
pub fn escape_text(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            _ => out.push(ch),
        }
    }
    out
}

/// Escape an attribute value the way `quoteattr` does.
pub fn escape_attribute(value: &str) -> String {
    let mut out = String::with_capacity(value.len());
    for ch in value.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\n' => out.push_str("&#10;"),
            '\r' => out.push_str("&#13;"),
            '\t' => out.push_str("&#9;"),
            _ => out.push(ch),
        }
    }
    out
}

/// A compact streaming XML writer, equivalent to `xml.sax.saxutils.XMLGenerator`.
///
/// openpyxl uses this for worksheets and the shared string table, where the output is
/// deliberately unindented. The API mirrors the Python `start_tag`/`end_tag`/`tag`
/// wrappers.
#[derive(Debug, Default)]
pub struct XmlWriter {
    out: String,
}

impl XmlWriter {
    /// Create an empty writer.
    pub fn new() -> Self {
        XmlWriter { out: String::new() }
    }

    /// Consume the writer and return the accumulated XML.
    pub fn into_string(self) -> String {
        self.out
    }

    /// Borrow the accumulated XML.
    pub fn as_str(&self) -> &str {
        &self.out
    }

    /// Append raw XML text.
    pub fn raw(&mut self, text: &str) -> &mut Self {
        self.out.push_str(text);
        self
    }

    /// Open an element.
    ///
    /// Attribute names and values are anything that renders to a string, so a caller can
    /// mix literals with computed values in one array.
    pub fn start_tag<I, K, V>(&mut self, name: &str, attrs: I) -> &mut Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: std::fmt::Display,
        V: std::fmt::Display,
    {
        self.out.push('<');
        self.out.push_str(name);
        for (k, v) in attrs {
            self.out.push(' ');
            self.out.push_str(&k.to_string());
            self.out.push_str("=\"");
            self.out.push_str(&escape_attribute(&v.to_string()));
            self.out.push('"');
        }
        self.out.push('>');
        self
    }

    /// Close an element.
    pub fn end_tag(&mut self, name: &str) -> &mut Self {
        self.out.push_str("</");
        self.out.push_str(name);
        self.out.push('>');
        self
    }

    /// Write an element containing only text (`tag(doc, name, attr, body)`).
    pub fn tag<I, K, V>(&mut self, name: &str, attrs: I, body: Option<&str>) -> &mut Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: std::fmt::Display,
        V: std::fmt::Display,
    {
        self.start_tag(name, attrs);
        if let Some(body) = body {
            if !body.is_empty() {
                self.out.push_str(&escape_text(body));
            }
        }
        self.end_tag(name)
    }

    /// Close the document.
    pub fn end_document(&mut self) -> &mut Self {
        self
    }
}

/// `start_tag(doc, name, attr, body)` as a free function, kept for parity with the
/// Python module API.
pub fn start_tag<I, K, V>(doc: &mut XmlWriter, name: &str, attrs: I, body: Option<&str>)
where
    I: IntoIterator<Item = (K, V)>,
    K: std::fmt::Display,
    V: std::fmt::Display,
{
    doc.start_tag(name, attrs);
    if let Some(body) = body {
        if !body.is_empty() {
            doc.raw(&escape_text(body));
        }
    }
}

/// `end_tag(doc, name)` as a free function.
pub fn end_tag(doc: &mut XmlWriter, name: &str) {
    doc.end_tag(name);
}

/// `tag(doc, name, attr, body)` as a free function.
pub fn tag<I, K, V>(doc: &mut XmlWriter, name: &str, attrs: I, body: Option<&str>)
where
    I: IntoIterator<Item = (K, V)>,
    K: std::fmt::Display,
    V: std::fmt::Display,
{
    doc.tag(name, attrs, body);
}

/// `ConditionalElement`: append a child only when `condition` holds.
///
/// When `attr` is a string, it becomes a `name="1"` attribute; otherwise no attributes
/// are emitted.
pub fn conditional_element<'a>(
    parent: &'a mut Element,
    tag: &str,
    condition: bool,
    attr: Option<&str>,
) -> Option<&'a mut Element> {
    if !condition {
        return None;
    }
    let mut child = Element::new(tag);
    if let Some(name) = attr {
        child.set(name, "1");
    }
    parent.append(child);
    parent.children.last_mut()
}

/// Parse an XML document into an [`Element`] tree (`fromstring`).
///
/// Element and attribute names are normalised to ElementTree's `{ns}local` form so the
/// same lookup helpers work for both.
/// Append a chunk of text to the element being parsed.
///
/// The reader delivers one element's content as a sequence of events -- a run of text, then an
/// entity reference, then more text -- so the text is accumulated here rather than assigned.
fn append_text(top: Option<&mut Element>, chunk: &str) {
    let Some(top) = top else {
        return;
    };
    match top.text.as_mut() {
        Some(existing) => existing.push_str(chunk),
        None => top.text = Some(chunk.to_string()),
    }
}

/// Resolve `&#NN;` or `&#xNN;` to the character it names.
///
/// Returns `None` for anything else, which covers both a malformed reference and a plain name:
/// the caller decides what to do, because a declared entity and a character reference are the two
/// cases XML allows and the rest are not valid content.
fn decode_character_reference(text: &str) -> Option<String> {
    let digits = text.strip_prefix('#')?;
    let code = match digits.strip_prefix(['x', 'X']) {
        Some(hex) => u32::from_str_radix(hex, 16).ok()?,
        None => digits.parse::<u32>().ok()?,
    };
    char::from_u32(code).map(String::from)
}

/// Split an ElementTree path into its last ancestor step and its final tag.
///
/// A namespace URI inside a `{ns}tag` step contains slashes, so only a slash outside
/// braces separates two steps.
fn split_path(path: &str) -> Option<(&str, &str)> {
    let mut depth = 0i32;
    for (index, ch) in path.char_indices() {
        match ch {
            '{' => depth += 1,
            '}' => depth -= 1,
            '/' if depth == 0 => return Some((&path[..index], &path[index + 1..])),
            _ => {}
        }
    }
    None
}
/// Parse an XML document into an [`Element`] tree (`fromstring`).
///
/// Element and attribute names are normalised to ElementTree's `{ns}local` form so the
/// same lookup helpers work for both. Namespace declarations are resolved as the tree
/// is built, because `quick_xml::Reader` does not apply them itself.
pub fn fromstring(data: &[u8]) -> Result<Element> {
    let text = std::str::from_utf8(data)
        .map_err(|e| Error::Xml(format!("input is not valid UTF-8: {e}")))?;
    let mut reader = Reader::from_str(text);
    reader.config_mut().trim_text(false);
    reader.config_mut().check_end_names = true;
    let mut stack: Vec<Element> = vec![Element::new("__document__")];
    // `quick_xml::Reader` does not apply namespace declarations itself, so the bindings are
    // tracked here: `bindings` holds every declaration in scope, and `scope_marks` records
    // where each open element's own declarations begin so they can be dropped on close.
    let mut bindings: Vec<(String, String)> = Vec::new();
    let mut scope_marks: Vec<usize> = vec![0];
    let mut buffer = Vec::new();
    loop {
        match reader.read_event_into(&mut buffer) {
            Ok(Event::Start(start)) => {
                scope_marks.push(bindings.len());
                let el = start_element(start, &mut bindings)?;
                stack.push(el);
            }
            Ok(Event::Empty(start)) => {
                let el = start_element(start, &mut bindings)?;
                let parent = stack.last_mut().expect("document root");
                parent.children.push(el);
            }
            Ok(Event::End(BytesEnd { .. })) => {
                if stack.len() > 1 {
                    if let Some(mark) = scope_marks.pop() {
                        bindings.truncate(mark);
                    }
                    let el = stack.pop().expect("element underflow");
                    let parent = stack.last_mut().expect("document root");
                    parent.children.push(el);
                }
            }
            Ok(Event::Text(text)) => {
                // `&amp;` and friends arrive as their own event rather than inline, so this
                // text has no escapes left in it. Anything still encoded is an escape the
                // reader did not report separately, and resolving it is a no-op otherwise.
                let decoded =
                    quick_xml::escape::unescape(&text).map_err(|e| Error::Xml(e.to_string()))?;
                append_text(stack.last_mut(), &decoded);
            }
            Ok(Event::GeneralRef(reference)) => {
                // An entity reference in element content, which the reader hands over separately
                // from the text around it. The five XML entities are resolved here and a numeric
                // character reference is read as one; anything else is an undeclared entity,
                // which XML forbids in content, so it is an error rather than a silent omission.
                let name = reference.as_ref();
                let resolved = match name {
                    "amp" => "&".to_string(),
                    "lt" => "<".to_string(),
                    "gt" => ">".to_string(),
                    "quot" => "\"".to_string(),
                    "apos" => "'".to_string(),
                    _ => match decode_character_reference(name) {
                        Some(character) => character,
                        None => {
                            return Err(Error::Xml(format!(
                                "undefined entity reference &{name}; in content this is not \
                                 valid XML"
                            )))
                        }
                    },
                };
                append_text(stack.last_mut(), &resolved);
            }
            Ok(Event::CData(cdata)) => {
                // CDATA content is literal, so it is copied verbatim.
                let decoded = cdata
                    .escape()
                    .map_err(|e| Error::Xml(e.to_string()))?
                    .to_string();
                append_text(stack.last_mut(), &decoded);
            }
            Ok(Event::Eof) => break,
            Ok(_) => {}
            Err(e) => return Err(Error::Xml(e.to_string())),
        }
        buffer.clear();
    }
    // The stack holds a sentinel plus the document element; the sentinel is dropped and
    // the real root returned. Input with no element at all is malformed, not empty.
    if stack.len() == 1 {
        let mut document = stack.pop().expect("document sentinel");
        return match document.children.pop() {
            Some(root) => Ok(root),
            None => Err(Error::Xml("document has no root element".into())),
        };
    }
    Err(Error::Xml("no root element found".into()))
}

/// Build an element from a start tag, extending `bindings` with its declarations.
fn start_element(start: BytesStart<'_>, bindings: &mut Vec<(String, String)>) -> Result<Element> {
    // The raw attributes are read first because the element's own declarations must be in
    // scope before its name can be resolved.
    let mut raw: Vec<(String, String)> = Vec::new();
    for attr in start.attributes().with_checks(false) {
        let attr = attr.map_err(|e| Error::Xml(e.to_string()))?;
        let value = attr
            .normalized_value(quick_xml::XmlVersion::Implicit1_0)?
            .into_owned();
        raw.push((attr.key.as_ref().to_string(), value));
    }
    for (key, value) in &raw {
        if key == "xmlns" {
            bindings.push((String::new(), value.clone()));
        } else if let Some(prefix) = key.strip_prefix("xmlns:") {
            bindings.push((prefix.to_string(), value.clone()));
        }
    }

    let name = start.name();
    let tag = qualify(name.as_ref(), bindings, false);
    let mut el = Element::new(tag);
    for (key, value) in raw {
        if key == "xmlns" || key.starts_with("xmlns:") {
            continue;
        }
        el.attributes.push((qualify(&key, bindings, true), value));
    }
    Ok(el)
}

/// Resolve a qualified name against the bindings in scope.
///
/// An unprefixed name takes the default namespace, which is what lets a SpreadsheetML
/// document with `xmlns=".../main"` produce `{.../main}sheet` tags. An unprefixed
/// *attribute*, however, is never in the default namespace, so `is_attribute` suppresses
/// that lookup.
fn qualify(name: &str, bindings: &[(String, String)], is_attribute: bool) -> String {
    let (prefix, local) = match name.split_once(':') {
        Some((prefix, local)) => (prefix, local),
        None => ("", name),
    };
    let resolved = if is_attribute && prefix.is_empty() {
        None
    } else {
        lookup(bindings, prefix)
    };
    match resolved {
        Some(uri) => format!("{{{uri}}}{local}"),
        None => local.to_string(),
    }
}

/// Find the most recent binding for `prefix`.
fn lookup<'a>(bindings: &'a [(String, String)], prefix: &str) -> Option<&'a str> {
    if prefix == "xml" {
        return Some(super::constants::XML_NS);
    }
    bindings
        .iter()
        .rev()
        .find(|(declared, _)| declared == prefix)
        .map(|(_, uri)| uri.as_str())
}

/// Serialise an element tree to bytes without pretty-printing.
///
/// Namespace prefixes are declared on the root, matching the ElementTree behaviour the
/// reader's helpers expect.
pub fn serialize(root: &Element) -> Vec<u8> {
    let mut writer = XmlWriterInner::new(Vec::new());
    write_element_with(&mut writer, root);
    writer.into_inner()
}

fn write_element_with<W: std::io::Write>(writer: &mut XmlWriterInner<W>, el: &Element) {
    let mut start = BytesStart::new(super::constants::local_name(&el.tag).to_string());
    if let Some(ns) = namespace_of(&el.tag) {
        start = start.with_attributes([("xmlns", ns)]);
    }
    for (key, value) in &el.attributes {
        let attr_name = if let Some(ns) = namespace_of(key) {
            BytesStart::new(format!(
                "xmlns:{}",
                REGISTERED_PREFIXES
                    .iter()
                    .find(|(_, u)| *u == ns)
                    .map(|(p, _)| *p)
                    .unwrap_or("ns")
            ))
        } else {
            BytesStart::new(key.clone())
        };
        let _ = attr_name;
        start = start.with_attributes([(key.as_str(), value.as_str())]);
    }
    if el.children.is_empty() && el.text.is_none() {
        let _ = writer.write_event(Event::Empty(start));
    } else {
        let _ = writer.write_event(Event::Start(start));
        if let Some(text) = &el.text {
            // Escaping happens in `serialize`; writing the raw text avoids double-encoding.
            let _ = writer.write_event(Event::Text(BytesText::new(text)));
        }
        for child in &el.children {
            write_element_with(writer, child);
        }
        let _ = writer.write_event(Event::End(BytesEnd::new(
            super::constants::local_name(&el.tag).to_string(),
        )));
    }
}

/// Read all bytes from a reader (helper for file-like inputs).
pub fn read_all<R: Read>(mut reader: R) -> Result<Vec<u8>> {
    let mut buf = Vec::new();
    reader.read_to_end(&mut buf)?;
    Ok(buf)
}

/// Format a float the way Python's `repr()` does for the values written to XML.
///
/// Python writes the shortest representation that round-trips; Rust's default `{}` for
/// `f64` does the same but always keeps a decimal point for integral values, so the
/// integral case is special-cased to emit an integer-looking literal like `1` rather
/// than `1.0` — matching openpyxl's `repr`/`str` output for floats.
pub fn repr_float(value: f64) -> String {
    if value == value.trunc() && value.abs() < 1e16 {
        format!("{}", value as i64)
    } else {
        let mut s = format!("{value}");
        if s.contains('e') || s.contains("inf") || s.contains("NaN") {
            s = format!("{value:?}");
        }
        s
    }
}

/// Python's `"%.15g" % value`, used by `safe_string` for numeric XML payloads.
pub fn safe_string(value: f64) -> String {
    if value == 0.0 {
        return "0".to_string();
    }
    let exp = value.abs().log10().floor() as i32;
    if !(-4..15).contains(&exp) {
        let mut s = format!("{:.14e}", value);
        // Python renders exponents with at least two digits and no zero padding on
        // the mantissa beyond what is needed.
        if let Some(pos) = s.find('e') {
            let (mantissa, exponent) = s.split_at(pos);
            let mut mantissa = mantissa.trim_end_matches('0').to_string();
            if mantissa.ends_with('.') {
                mantissa.pop();
            }
            let exponent = &exponent[1..];
            let (sign, digits) = match exponent.strip_prefix('-') {
                Some(d) => ('-', d),
                None => ('+', exponent),
            };
            s = format!("{mantissa}e{sign}{:0>2}", digits);
        }
        s
    } else {
        let decimals = (14 - exp).max(0) as usize;
        let mut s = format!("{:.*}", decimals, value);
        if s.contains('.') {
            s = s.trim_end_matches('0').trim_end_matches('.').to_string();
        }
        s
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_and_find() {
        let xml =
            br#"<?xml version="1.0"?><root xmlns="urn:x"><child a="1">hi</child><child/></root>"#;
        let root = fromstring(xml).unwrap();
        assert_eq!(root.tag, "{urn:x}root");
        assert_eq!(root.find_text("{urn:x}child", ""), "hi");
        assert_eq!(root.find("{urn:x}child").unwrap().get("a"), Some("1"));
        assert_eq!(root.find_all("{urn:x}child").len(), 2);
        assert!(root.find("{urn:x}missing").is_none());
    }

    #[test]
    fn parse_namespaced_attributes() {
        let xml =
            br#"<w:workbook xmlns:w="urn:w" xmlns:r="urn:r"><w:sheet r:id="rId1"/></w:workbook>"#;
        let root = fromstring(xml).unwrap();
        let sheet = root.find("{urn:w}sheet").unwrap();
        assert_eq!(sheet.get("{urn:r}id"), Some("rId1"));
    }

    #[test]
    fn serialise_declares_namespaces() {
        let mut el = Element::new(format!("{{{SHEET_MAIN_NS}}}workbook"));
        el.append(Element::new(format!("{{{SHEET_MAIN_NS}}}sheets")));
        let out = el.to_pretty_string();
        // Every namespace in the tree is declared on the root with a generated prefix.
        assert!(out.starts_with("<s:workbook xmlns:s="));
        assert!(out.contains(SHEET_MAIN_NS));
        assert!(out.contains("<s:sheets/>"));
        assert!(out.trim_end().ends_with("</s:workbook>"));
    }

    #[test]
    fn streaming_writer_matches_python_shape() {
        let mut doc = XmlWriter::new();
        doc.start_tag("worksheet", [("xmlns", SHEET_MAIN_NS)]);
        doc.tag("dimension", [("ref", "A1:B2")], None);
        doc.start_tag("row", [("r", "1")]);
        doc.tag("c", [("r", "A1"), ("t", "s")], Some("0"));
        doc.end_tag("row");
        doc.end_tag("worksheet");
        // Python's `XMLGenerator` never self-closes an element, so `tag` writes an explicit
        // close tag rather than `/>`.
        assert_eq!(
            doc.as_str(),
            "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><dimension ref=\"A1:B2\"></dimension><row r=\"1\"><c r=\"A1\" t=\"s\">0</c></row></worksheet>"
        );
    }

    #[test]
    fn escaping_is_applied() {
        let mut doc = XmlWriter::new();
        doc.tag("t", [("a", "x\"&<>")], Some("a<b&c>d"));
        // Python's `quoteattr` switches to single quotes when a value contains a double
        // quote; ferroxl always uses double quotes and escapes the quote itself, which is
        // equivalent XML.
        assert_eq!(
            doc.as_str(),
            "<t a=\"x&quot;&amp;&lt;&gt;\">a&lt;b&amp;c&gt;d</t>"
        );
    }

    #[test]
    fn an_inline_string_is_read_from_is_not_from_v() {
        // openpyxl writes a string inline -- `<is><t>` -- when the workbook has no shared
        // string table, which is the default for a file it has just created. Nothing else
        // supplies the value, so a cell in this form used to read back empty.
        let cell = fromstring(br#"<c r="A1" t="inlineStr"><is><t>plain &amp; simple</t></is></c>"#)
            .expect("parses");
        let text = cell
            .find("is")
            .and_then(|is| is.find("t"))
            .and_then(|t| t.text.clone());
        assert_eq!(
            text.as_deref(),
            Some("plain & simple"),
            "an inline string carries its text in <is><t>, not in <v>"
        );
    }

    #[test]
    fn an_entity_reference_in_text_is_resolved_rather_than_dropped() {
        // The reader reports `&amp;` as its own `GeneralRef` event, so a parser that handles only
        // `Text` loses it. That is not a header/footer problem: it silently deleted every `&`, `<`,
        // `>`, `"` and `'` from every element's text, in every part of every workbook.
        for (xml, expected) in [
            ("<t>a &amp; b</t>", "a & b"),
            ("<t>Page &amp;P of &amp;N</t>", "Page &P of &N"),
            ("<t>&lt;tag&gt;</t>", "<tag>"),
            ("<t>&quot;quoted&quot;</t>", "\"quoted\""),
            ("<t>it&apos;s</t>", "it's"),
            ("<t>&#65;&#66;</t>", "AB"),
            ("<t>&#x41;&#x42;</t>", "AB"),
            // An escape in the middle of a longer run, where the chunks have to be joined.
            ("<t>Ben &amp; Jerry&apos;s</t>", "Ben & Jerry's"),
        ] {
            let element = fromstring(xml.as_bytes()).expect("parses");
            assert_eq!(
                element.text.as_deref(),
                Some(expected),
                "{xml} should read back as {expected:?}"
            );
        }
    }

    #[test]
    fn an_entity_reference_in_an_attribute_is_kept() {
        // Attributes are read by their own parser, so this pins the half that already worked --
        // the bug was in element content.
        let element = fromstring(br#"<t name="a &amp; b"/>"#).expect("parses");
        assert_eq!(element.get("name"), Some("a & b"));
    }

    #[test]
    fn an_undeclared_entity_is_an_error_rather_than_a_silent_omission() {
        // XML allows only the five entities and character references in content. A name it does
        // not know is not something to skip quietly: that is what turned this into silent data
        // loss in the first place.
        assert!(fromstring(b"<t>&nbsp;</t>").is_err());
    }

    #[test]
    fn safe_string_matches_python_g_format() {
        assert_eq!(safe_string(0.0), "0");
        assert_eq!(safe_string(1.0), "1");
        assert_eq!(safe_string(1.5), "1.5");
        assert_eq!(safe_string(1234567890123456.0), "1.23456789012346e+15");
        assert_eq!(safe_string(0.0001), "0.0001");
    }

    #[test]
    fn repr_float_drops_trailing_dot() {
        assert_eq!(repr_float(1.0), "1");
        assert_eq!(repr_float(1.5), "1.5");
    }
}