deser-xml 0.10.1

XML support for deser
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
use std::borrow::Cow;

use deser_core::de::{
    self, ContentKey, Deserialize, DeserializeDriver, LexicalRules, deserialize_value,
};
use deser_core::{
    Atom, ContainerShape, Error, ErrorKind, Event, Order, Source, Text, TrackLocations,
};
use quick_xml::XmlVersion;
use quick_xml::events::{BytesRef, BytesStart, Event as XmlEvent};
use quick_xml::name::{QName, ResolveResult};
use quick_xml::reader::NsReader;

use crate::Names;
use crate::mixed::WhitespaceDepths;
use crate::root::{Declarations, RootData};

/// Configures how XML documents are deserialized.
///
/// See the [crate documentation](crate) for how XML maps onto the data
/// model of deser.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeserializerConfig {
    pub(crate) names: Names,
    resolve_namespaces: bool,

    context: deser_core::Context,
}

impl Default for DeserializerConfig {
    fn default() -> DeserializerConfig {
        DeserializerConfig::new()
    }
}

impl DeserializerConfig {
    /// Creates the default configuration.
    pub const fn new() -> DeserializerConfig {
        DeserializerConfig {
            names: Names::new(),
            resolve_namespaces: false,

            context: deser_core::Context::new(),
        }
    }

    /// Returns a builder for the configuration (see [`DeserializerConfigBuilder`]).
    pub const fn builder() -> DeserializerConfigBuilder {
        DeserializerConfigBuilder::new()
    }

    /// Returns a builder that starts with this configuration.
    pub const fn into_builder(self) -> DeserializerConfigBuilder {
        DeserializerConfigBuilder { value: self }
    }

    /// Sets the context the values are deserialized in.
    ///
    /// The values of the context are the defaults of the extension values
    /// of the state (see [`Context`](deser_core::Context)), for instance
    /// the variants of open enums.  The deserializers and readers created
    /// with the configuration use this context.  A context set
    /// on the driver takes precedence.
    pub fn set_context(&mut self, context: deser_core::Context) {
        self.context = context;
    }

    /// Returns the configuration without its context (for the frames of
    /// streams, which get the context of the stream).
    pub(crate) fn without_context(&self) -> DeserializerConfig {
        let mut config = self.clone();
        config.context = deser_core::Context::default();
        config
    }

    /// Returns the context the values are deserialized in.
    pub fn context(&self) -> &deser_core::Context {
        &self.context
    }

    /// Sets the prefix of the keys of attributes.
    ///
    /// The default is `@`: `<a href="x"/>` is `{"@href": "x"}`.
    pub const fn set_attribute_prefix(&mut self, prefix: &'static str) {
        self.names.attribute_prefix = prefix;
    }

    /// Sets the key of the text of elements that are maps.
    ///
    /// The default is `$text`: `<a href="x">y</a>` is
    /// `{"@href": "x", "$text": "y"}`.
    pub const fn set_text_key(&mut self, key: &'static str) {
        self.names.text_key = key;
    }

    /// Sets the prefixes of namespaces.
    ///
    /// Names are passed on as written in the document (`atom:link`),
    /// unless their namespace has a prefix here: then they are written
    /// with this prefix, whichever prefix the document uses.  The empty
    /// prefix leaves only the local name.  Names in other namespaces are
    /// passed on as written or, if namespaces are
    /// [resolved](Self::set_resolve_namespaces), as `{uri}local`.  The table
    /// can be written with [`prefixes!`](crate::prefixes).
    ///
    /// ```
    /// use deser_xml::DeserializerConfig;
    ///
    /// #[derive(deser::Deserialize)]
    /// struct Feed {
    ///     title: String,
    ///     #[deser(rename = "dc:creator")]
    ///     creator: String,
    /// }
    ///
    /// const CONFIG: DeserializerConfig = DeserializerConfig::new().namespaces(&[
    ///     ("", "http://www.w3.org/2005/Atom"),
    ///     ("dc", "http://purl.org/dc/elements/1.1/"),
    /// ]);
    /// let feed: Feed = CONFIG.from_str(r#"
    ///     <a:feed xmlns:a="http://www.w3.org/2005/Atom"
    ///             xmlns:x="http://purl.org/dc/elements/1.1/">
    ///       <a:title>Example</a:title>
    ///       <x:creator>Jane</x:creator>
    ///     </a:feed>
    /// "#).unwrap();
    /// assert_eq!(feed.title, "Example");
    /// assert_eq!(feed.creator, "Jane");
    /// ```
    pub const fn namespaces(
        mut self,
        namespaces: &'static [(&'static str, &'static str)],
    ) -> DeserializerConfig {
        self.names.namespaces = namespaces;
        self
    }

    /// Enables or disables resolving namespaces.
    ///
    /// By default names are passed on as written in the document.  If
    /// namespaces are resolved, names in a namespace are passed on as
    /// `{uri}local` (the notation of James Clark, attributes are
    /// `@{uri}local`) unless the namespace has a
    /// [prefix](Self::namespaces), so the prefixes of the document do not
    /// matter.  Names without namespace are their local name, the `xml`
    /// prefix is kept (`@xml:lang`).  Prefixes that are not declared are
    /// an error.  The default is `false`.
    ///
    /// The names can be written with [`qname!`](crate::qname) and
    /// [`namespace!`](crate::namespace):
    ///
    /// ```
    /// use deser_xml::DeserializerConfig;
    ///
    /// deser_xml::namespace!(atom = "http://www.w3.org/2005/Atom");
    ///
    /// #[derive(deser::Deserialize)]
    /// struct Link {
    ///     #[deser(rename = "@href")]
    ///     href: String,
    /// }
    ///
    /// #[derive(deser::Deserialize)]
    /// struct Feed {
    ///     #[deser(rename = atom!("title"))]
    ///     title: String,
    ///     #[deser(rename = atom!("link"))]
    ///     link: Link,
    /// }
    ///
    /// const CONFIG: DeserializerConfig =
    ///     DeserializerConfig::builder().resolve_namespaces(true).build();
    /// let xml = r#"
    ///     <feed xmlns="http://www.w3.org/2005/Atom">
    ///       <title>Example</title>
    ///       <link href="/a"/>
    ///     </feed>
    /// "#;
    /// let feed: Feed = CONFIG.from_str(xml).unwrap();
    /// assert_eq!(feed.title, "Example");
    /// assert_eq!(feed.link.href, "/a");
    /// ```
    pub const fn set_resolve_namespaces(&mut self, yes: bool) {
        self.resolve_namespaces = yes;
    }

    /// Deserializes a value from a string with this configuration.
    pub fn from_str<'de, T: Deserialize<'de>>(&self, s: &'de str) -> Result<T, Error> {
        deserialize_value(|driver| self.drive_str(s, driver))
    }

    /// The part of [`from_str`](Self::from_str) that does not depend on the type
    /// of the value, it exists once.
    fn drive_str<'de>(
        &self,
        s: &'de str,
        driver: &mut DeserializeDriver<'_, 'de>,
    ) -> Result<(), Error> {
        de::Deserializer::drive(
            &mut Deserializer::from_str_with_config(s, self.clone()),
            driver,
        )
    }

    /// Deserializes a value from bytes with this configuration.
    ///
    /// The input must be UTF-8.
    pub fn from_slice<'de, T: Deserialize<'de>>(&self, bytes: &'de [u8]) -> Result<T, Error> {
        deserialize_value(|driver| self.drive_slice(bytes, driver))
    }

    /// The part of [`from_slice`](Self::from_slice) that does not depend on the type
    /// of the value, it exists once.
    fn drive_slice<'de>(
        &self,
        bytes: &'de [u8],
        driver: &mut DeserializeDriver<'_, 'de>,
    ) -> Result<(), Error> {
        de::Deserializer::drive(
            &mut Deserializer::from_slice_with_config(bytes, self.clone()),
            driver,
        )
    }
}

/// Builds a [`DeserializerConfig`].
///
/// The methods have the names of the setters of [`DeserializerConfig`] (without `set_`).
#[derive(Debug, Clone)]
#[must_use]
pub struct DeserializerConfigBuilder {
    value: DeserializerConfig,
}

impl DeserializerConfigBuilder {
    /// Creates a builder that starts with the default.
    pub const fn new() -> DeserializerConfigBuilder {
        DeserializerConfigBuilder {
            value: DeserializerConfig::new(),
        }
    }

    /// Sets the prefix of the keys of attributes.
    ///
    /// See [`DeserializerConfig::set_attribute_prefix`].
    pub const fn attribute_prefix(mut self, prefix: &'static str) -> DeserializerConfigBuilder {
        self.value.set_attribute_prefix(prefix);
        self
    }

    /// Sets the key of the text of elements that are maps.
    ///
    /// See [`DeserializerConfig::set_text_key`].
    pub const fn text_key(mut self, key: &'static str) -> DeserializerConfigBuilder {
        self.value.set_text_key(key);
        self
    }

    /// Enables or disables resolving namespaces.
    ///
    /// See [`DeserializerConfig::set_resolve_namespaces`].
    pub const fn resolve_namespaces(mut self, yes: bool) -> DeserializerConfigBuilder {
        self.value.set_resolve_namespaces(yes);
        self
    }

    /// Sets the context the values are deserialized in.
    ///
    /// See [`DeserializerConfig::set_context`].
    pub fn context(mut self, context: deser_core::Context) -> DeserializerConfigBuilder {
        self.value.set_context(context);
        self
    }

    /// Returns the built [`DeserializerConfig`].
    pub const fn build(self) -> DeserializerConfig {
        // the value cannot be moved out of the builder in a const fn as the
        // builder needs dropping (the context has a destructor)
        // SAFETY: the value is read once and the builder is forgotten
        let value = unsafe { core::ptr::read(&self.value) };
        core::mem::forget(self);
        value
    }
}

impl Default for DeserializerConfigBuilder {
    fn default() -> DeserializerConfigBuilder {
        DeserializerConfigBuilder::new()
    }
}

/// Deserializes a value from an XML string.
///
/// ```
/// #[derive(deser::Deserialize)]
/// struct Link {
///     #[deser(rename = "@href")]
///     href: String,
/// }
///
/// let link: Link = deser_xml::from_str(r#"<a href="/x"/>"#).unwrap();
/// assert_eq!(link.href, "/x");
/// ```
pub fn from_str<'de, T: Deserialize<'de>>(s: &'de str) -> Result<T, Error> {
    DeserializerConfig::new().from_str(s)
}

/// Deserializes a value from UTF-8 encoded XML.
pub fn from_slice<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, Error> {
    DeserializerConfig::new().from_slice(bytes)
}

/// Deserializes XML documents.
pub struct Deserializer<'a> {
    input: &'a str,
    error: Option<Error>,
    config: DeserializerConfig,
}

impl<'a> Deserializer<'a> {
    /// Creates a deserializer for a string.
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(input: &'a str) -> Deserializer<'a> {
        Deserializer::from_str_with_config(input, DeserializerConfig::new())
    }

    /// Creates a deserializer for a string with the given configuration.
    pub fn from_str_with_config(input: &'a str, config: DeserializerConfig) -> Deserializer<'a> {
        Deserializer {
            input,
            error: None,
            config,
        }
    }

    /// Creates a deserializer for UTF-8 encoded bytes.
    pub fn from_slice(input: &'a [u8]) -> Deserializer<'a> {
        Deserializer::from_slice_with_config(input, DeserializerConfig::new())
    }

    /// Creates a deserializer for UTF-8 encoded bytes with the given
    /// configuration.
    pub fn from_slice_with_config(input: &'a [u8], config: DeserializerConfig) -> Deserializer<'a> {
        // a byte order mark is not part of the document
        let input = input.strip_prefix(b"\xef\xbb\xbf").unwrap_or(input);
        match std::str::from_utf8(input) {
            Ok(input) => Deserializer::from_str_with_config(input, config),
            Err(err) => Deserializer {
                input: "",
                error: Some(Error::with_offset(
                    ErrorKind::Syntax,
                    "input is not valid UTF-8",
                    err.valid_up_to(),
                )),
                config,
            },
        }
    }

    /// Returns the configuration.
    pub fn config(&self) -> &DeserializerConfig {
        &self.config
    }

    /// Deserializes the document.
    pub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error> {
        de::Deserializer::deserialize(self)
    }

    /// Deserializes the document with a configured driver.
    ///
    /// The callback is invoked with the driver before the value is
    /// deserialized, for instance to add [`Layer`](deser_core::de::Layer)s.
    pub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
    where
        T: Deserialize<'a>,
        F: FnOnce(&mut DeserializeDriver<'_, 'a>),
    {
        de::Deserializer::deserialize_with(self, setup)
    }

    /// Parses the document and feeds the events into the driver.
    ///
    /// Events are emitted while the document is parsed.  Text is passed on
    /// borrowed from the input unless it has references or line breaks
    /// that are normalized.
    ///
    /// The context of the configuration is given to the driver (values that
    /// the context of the driver has take precedence, see
    /// [`DeserializeDriver::set_default_context`]).
    pub fn drive(&mut self, driver: &mut DeserializeDriver<'_, 'a>) -> Result<(), Error> {
        if !self.config.context.is_empty() {
            driver.set_default_context(self.config.context.clone());
        }
        if let Some(err) = self.error.take() {
            return Err(err);
        }
        let state = driver.state_mut();
        if TrackLocations::of(state) {
            Source(self.input.into()).set(state);
        }
        TEXT_RULES.set_default(state);
        // elements with attributes are text for types that expect text,
        // text is an element for types that expect maps
        ContentKey(self.config.names.text_key).set(state);
        *state.get_mut::<Names>() = self.config.names.clone();
        Parser {
            input: self.input,
            config: &self.config,
            reader: NsReader::from_str(self.input),
            stack: Vec::new(),
            root_done: false,
            root: None,
            declarations: None,
        }
        .run(driver)
        .map_err(|mut err| {
            err.resolve_position(self.input.as_bytes());
            err
        })
    }
}

impl<'a> de::Deserializer<'a> for Deserializer<'a> {
    fn drive(&mut self, driver: &mut DeserializeDriver<'_, 'a>) -> Result<(), Error> {
        Deserializer::drive(self, driver)
    }
}

/// How the text of XML is interpreted.
///
/// Booleans are `true` and `false` (XML Schema also allows `1` and `0`,
/// which are integers here), an empty element is a missing value for types
/// that do not accept it (`<age/>` is `None` for an `Option<u32>`).
const TEXT_RULES: LexicalRules = {
    let mut rules = LexicalRules::STRICT;
    rules.set_empty_is_null(true);
    rules
};

/// A byte range in the input.
type Range = (usize, usize);

/// The text of an element that was not passed on yet.
enum PendingText<'a> {
    None,
    Borrowed(&'a str, Range),
    Owned(String, Range),
}

impl<'a> PendingText<'a> {
    fn push(&mut self, text: Cow<'a, str>, range: Range) {
        *self = match (std::mem::replace(self, PendingText::None), text) {
            (PendingText::None, Cow::Borrowed(text)) => PendingText::Borrowed(text, range),
            (PendingText::None, Cow::Owned(text)) => PendingText::Owned(text, range),
            (PendingText::Borrowed(prev, (start, _)), text) => {
                PendingText::Owned(prev.to_string() + &text, (start, range.1))
            }
            (PendingText::Owned(mut prev, (start, _)), text) => {
                prev.push_str(&text);
                PendingText::Owned(prev, (start, range.1))
            }
        };
    }

    fn is_blank(&self) -> bool {
        match self {
            PendingText::None => true,
            PendingText::Borrowed(text, _) => is_blank(text),
            PendingText::Owned(text, _) => is_blank(text),
        }
    }

    /// Emits the text as lexical atom.
    fn emit(self, driver: &mut DeserializeDriver<'_, 'a>, fallback: Range) -> Result<(), Error> {
        match self {
            PendingText::None => emit_at(driver, Atom::Lexical(Text::borrowed("")), fallback),
            PendingText::Borrowed(text, range) => {
                driver.state_mut().set_input_range(range.0, range.1);
                driver.emit_borrowed(Atom::Lexical(Text::borrowed(text)))
            }
            PendingText::Owned(text, range) => {
                emit_at(driver, Atom::Lexical(Text::owned(text)), range)
            }
        }
    }
}

fn is_blank(text: &str) -> bool {
    text.bytes()
        .all(|b| matches!(b, b' ' | b'\t' | b'\n' | b'\r'))
}

/// An open element.
struct Element<'a> {
    /// `true` once the element was passed on as map.
    is_map: bool,
    text: PendingText<'a>,
    /// The range of the start tag.
    start: Range,
}

struct Parser<'a, 'c> {
    input: &'a str,
    config: &'c DeserializerConfig,
    reader: NsReader<&'a [u8]>,
    stack: Vec<Element<'a>>,
    root_done: bool,
    /// The name and the namespaces of the root element until they are
    /// attached to its first event.
    root: Option<RootData>,
    /// The namespaces declared on the last element that was started until
    /// they are attached to its first event.
    declarations: Option<Vec<(String, String)>>,
}

impl<'a> Parser<'a, '_> {
    fn run(mut self, driver: &mut DeserializeDriver<'_, 'a>) -> Result<(), Error> {
        loop {
            let start = self.position();
            let event = self
                .reader
                .read_event()
                .map_err(|err| xml_error(err, self.reader.error_position() as usize))?;
            let range = (start, self.position());
            match event {
                XmlEvent::Start(ref tag) => self.start(driver, tag, range)?,
                XmlEvent::Empty(ref tag) => {
                    self.start(driver, tag, range)?;
                    self.end(driver, range)?;
                }
                XmlEvent::End(_) => self.end(driver, range)?,
                XmlEvent::Text(text) => {
                    self.text(text.xml_content(XmlVersion::Implicit1_0), range)?
                }
                XmlEvent::CData(text) => {
                    self.text(text.xml_content(XmlVersion::Implicit1_0), range)?
                }
                XmlEvent::GeneralRef(reference) => {
                    let text = resolve_reference(&reference, start)?;
                    self.text(Cow::Owned(text.to_string()), range)?
                }
                XmlEvent::Decl(_)
                | XmlEvent::PI(_)
                | XmlEvent::Comment(_)
                | XmlEvent::DocType(_) => {}
                XmlEvent::Eof => {
                    if !self.stack.is_empty() {
                        return Err(Error::with_offset(
                            ErrorKind::EndOfFile,
                            "unexpected end of input, an element is not closed",
                            start,
                        ));
                    }
                    if !self.root_done {
                        return Err(Error::with_offset(
                            ErrorKind::EndOfFile,
                            "no root element",
                            start,
                        ));
                    }
                    return Ok(());
                }
            }
        }
    }

    fn position(&self) -> usize {
        self.reader.buffer_position() as usize
    }

    /// Passes on the element on top of the stack as map if it's not yet.
    fn make_map(&mut self, driver: &mut DeserializeDriver<'_, 'a>) -> Result<(), Error> {
        if !self.stack.last().unwrap().is_map {
            self.attach_element(driver);
            let element = self.stack.last_mut().unwrap();
            element.is_map = true;
            emit_at(
                driver,
                Event::MapStart({
                    let mut shape = ContainerShape::with_order(Order::Significant);
                    shape.set_multimap(true);
                    shape
                }),
                element.start,
            )?;
        }
        self.flush_text(driver)
    }

    /// Passes on the text of the element on top of the stack as entry.
    fn flush_text(&mut self, driver: &mut DeserializeDriver<'_, 'a>) -> Result<(), Error> {
        let element = self.stack.last_mut().unwrap();
        // whitespace between elements is not text, unless the content is
        // mixed
        if matches!(element.text, PendingText::None)
            || (element.text.is_blank() && !WhitespaceDepths::applies(driver.state()))
        {
            element.text = PendingText::None;
            return Ok(());
        }
        let text = std::mem::replace(&mut element.text, PendingText::None);
        let range = match text {
            PendingText::Borrowed(_, range) | PendingText::Owned(_, range) => range,
            PendingText::None => unreachable!(),
        };
        emit_at(
            driver,
            Atom::Lexical(Text::borrowed(self.config.names.text_key)),
            range,
        )?;
        text.emit(driver, range)
    }

    fn start(
        &mut self,
        driver: &mut DeserializeDriver<'_, 'a>,
        tag: &BytesStart<'a>,
        range: Range,
    ) -> Result<(), Error> {
        if self.stack.is_empty() {
            if self.root_done {
                return Err(Error::with_offset(
                    ErrorKind::Syntax,
                    "more than one root element",
                    range.0,
                ));
            }
            self.root = Some(self.root_data(tag, range.0)?);
        } else {
            self.make_map(driver)?;
            let name = self.name(tag.name(), false, range.0)?;
            emit_key(driver, name, range)?;
            // most elements declare nothing, the attributes are only
            // looked at if they might
            if tag.attributes_raw().contains("xmlns") {
                let declarations = self.declarations(tag, false, range.0)?;
                if !declarations.is_empty() {
                    self.declarations = Some(declarations);
                }
            }
        }
        self.stack.push(Element {
            is_map: false,
            text: PendingText::None,
            start: range,
        });

        for attr in tag.attributes() {
            let attr = attr.map_err(|err| {
                Error::with_offset(
                    ErrorKind::Syntax,
                    format!("invalid attribute: {err}"),
                    range.0,
                )
            })?;
            let raw = attr.key.as_ref();
            // namespace declarations are not data
            if raw == "xmlns" || raw.starts_with("xmlns:") {
                continue;
            }
            self.make_map(driver)?;
            let name = self.name(attr.key, true, range.0)?;
            emit_key(driver, name, range)?;
            let value = attr
                .normalized_value(XmlVersion::Implicit1_0)
                .map_err(|err| xml_error(err, range.0))?;
            match reborrow(self.input, &value) {
                Some(value) => {
                    driver.state_mut().set_input_range(range.0, range.1);
                    driver.emit_borrowed(Atom::Lexical(Text::borrowed(value)))?;
                }
                None => emit_at(driver, Atom::Lexical(Text::borrowed(&value)), range)?,
            }
        }
        Ok(())
    }

    /// Returns the name of the root element and the namespaces declared
    /// on it.
    ///
    /// Namespaces with a configured prefix are declared with it as that's
    /// how names in them are passed on.
    fn root_data(&self, tag: &BytesStart<'a>, offset: usize) -> Result<RootData, Error> {
        Ok(RootData {
            name: Some(self.name(tag.name(), false, offset)?.into_owned()),
            namespaces: self.declarations(tag, true, offset)?,
        })
    }

    /// Returns the namespaces declared on an element.
    ///
    /// Namespaces with a configured prefix are declared with it as that's
    /// how names in them are passed on.  Undeclaring the default namespace
    /// (`xmlns=""`) is a declaration with an empty URI, except on the root
    /// where there is nothing to undeclare.
    fn declarations(
        &self,
        tag: &BytesStart<'a>,
        is_root: bool,
        offset: usize,
    ) -> Result<Vec<(String, String)>, Error> {
        let mut namespaces: Vec<(String, String)> = Vec::new();
        // invalid attributes are reported when the attributes are passed on
        for attr in tag.attributes().flatten() {
            let raw: &str = attr.key.as_ref();
            let prefix = match raw.strip_prefix("xmlns") {
                Some("") => "",
                Some(rest) => match rest.strip_prefix(':') {
                    Some(prefix) => prefix,
                    None => continue,
                },
                None => continue,
            };
            let uri = attr
                .normalized_value(XmlVersion::Implicit1_0)
                .map_err(|err| xml_error(err, offset))?;
            if uri.is_empty() && (is_root || !prefix.is_empty()) {
                continue;
            }
            let prefix = match self.config.names.namespaces.iter().find(|(_, x)| *x == uri) {
                Some((alias, _)) => alias,
                None => prefix,
            };
            if !namespaces.iter().any(|(x, _)| x == prefix) {
                namespaces.push((prefix.to_string(), uri.into_owned()));
            }
        }
        Ok(namespaces)
    }

    /// Attaches the name and the namespaces of the root element or the
    /// namespaces declared on another element to its first event.
    ///
    /// The first event of an element comes before the events of the
    /// elements in it, so the element they are for is the last one that was
    /// started.
    fn attach_element(&mut self, driver: &mut DeserializeDriver<'_, 'a>) {
        if let Some(root) = self.root.take() {
            *driver.state_mut().event_mut::<RootData>() = root;
        }
        if let Some(declarations) = self.declarations.take() {
            driver.state_mut().event_mut::<Declarations>().0 = declarations;
        }
    }

    fn end(&mut self, driver: &mut DeserializeDriver<'_, 'a>, range: Range) -> Result<(), Error> {
        if self.stack.last().unwrap().is_map {
            self.flush_text(driver)?;
            self.stack.pop();
            emit_at(driver, Event::MapEnd, range)?;
            WhitespaceDepths::prune(driver.state_mut());
        } else {
            self.attach_element(driver);
            let element = self.stack.pop().unwrap();
            element.text.emit(driver, element.start)?;
        }
        if self.stack.is_empty() {
            self.root_done = true;
        }
        Ok(())
    }

    fn text(&mut self, text: Cow<'a, str>, range: Range) -> Result<(), Error> {
        match self.stack.last_mut() {
            Some(element) => {
                element.text.push(text, range);
                Ok(())
            }
            None if is_blank(&text) => Ok(()),
            None => Err(Error::with_offset(
                ErrorKind::Syntax,
                "text outside of the root element",
                range.0,
            )),
        }
    }

    /// Returns the key of an element or attribute.
    fn name(
        &self,
        name: QName<'_>,
        is_attribute: bool,
        offset: usize,
    ) -> Result<Cow<'a, str>, Error> {
        let names = &self.config.names;
        let written: &str = name.as_ref();
        let local_name = name.local_name();
        let local: &str = local_name.as_ref();
        let mut key = match self.namespace(name, is_attribute) {
            Namespace::Alias("") => Cow::Owned(local.to_string()),
            Namespace::Alias(alias) => Cow::Owned(format!("{alias}:{local}")),
            Namespace::Uri(uri) => Cow::Owned(format!("{{{uri}}}{local}")),
            Namespace::Unknown if self.config.resolve_namespaces => {
                return Err(Error::with_offset(
                    ErrorKind::Syntax,
                    format!("the prefix of `{written}` is not declared"),
                    offset,
                ));
            }
            Namespace::Written | Namespace::Unknown => match reborrow(self.input, written) {
                Some(written) => Cow::Borrowed(written),
                None => Cow::Owned(written.to_string()),
            },
        };
        if is_attribute && !names.attribute_prefix.is_empty() {
            key = Cow::Owned(format!("{}{}", names.attribute_prefix, key));
        }
        Ok(key)
    }

    /// Returns how the namespace of a name is written.
    fn namespace(&self, name: QName<'_>, is_attribute: bool) -> Namespace {
        let namespaces = self.config.names.namespaces;
        let resolve = self.config.resolve_namespaces;
        if namespaces.is_empty() && !resolve {
            return Namespace::Written;
        }
        let resolver = self.reader.resolver();
        let (ns, _) = if is_attribute {
            resolver.resolve_attribute(name)
        } else {
            resolver.resolve_element(name)
        };
        match ns {
            ResolveResult::Bound(ns) => {
                let uri: &str = ns.as_ref();
                if let Some((alias, _)) = namespaces.iter().find(|(_, x)| *x == uri) {
                    Namespace::Alias(alias)
                } else if !resolve {
                    Namespace::Written
                } else if uri == XML_NAMESPACE {
                    Namespace::Alias("xml")
                } else {
                    Namespace::Uri(uri.to_string())
                }
            }
            ResolveResult::Unbound => Namespace::Written,
            ResolveResult::Unknown(_) => Namespace::Unknown,
        }
    }
}

/// The namespace of the `xml` prefix.
pub(crate) const XML_NAMESPACE: &str = "http://www.w3.org/XML/1998/namespace";

/// How the namespace of a name is written.
enum Namespace {
    /// The name is passed on as written.
    Written,
    /// The name has the configured prefix.
    Alias(&'static str),
    /// The name is `{uri}local`.
    Uri(String),
    /// The prefix is not declared.
    Unknown,
}

/// Returns the text as a slice of the input if it is one.
fn reborrow<'a>(input: &'a str, text: &str) -> Option<&'a str> {
    let start = (text.as_ptr() as usize).checked_sub(input.as_ptr() as usize)?;
    let end = start.checked_add(text.len())?;
    input
        .get(start..end)
        .filter(|x| x.as_ptr() == text.as_ptr())
}

/// Resolves a character or entity reference.
///
/// Only the predefined entities are supported, the entities of document
/// types are never expanded.
fn resolve_reference(reference: &BytesRef<'_>, offset: usize) -> Result<char, Error> {
    if let Some(c) = reference
        .resolve_char_ref()
        .map_err(|err| xml_error(err, offset))?
    {
        return Ok(c);
    }
    match &*reference.xml_content(XmlVersion::Implicit1_0) {
        "lt" => Ok('<'),
        "gt" => Ok('>'),
        "amp" => Ok('&'),
        "apos" => Ok('\''),
        "quot" => Ok('"'),
        name => Err(Error::with_offset(
            ErrorKind::Syntax,
            format!("unknown entity `&{name};`"),
            offset,
        )),
    }
}

fn xml_error(err: quick_xml::Error, offset: usize) -> Error {
    Error::with_offset(ErrorKind::Syntax, format!("invalid XML: {err}"), offset)
}

fn emit_key<'a>(
    driver: &mut DeserializeDriver<'_, 'a>,
    key: Cow<'a, str>,
    range: Range,
) -> Result<(), Error> {
    driver.state_mut().set_input_range(range.0, range.1);
    match key {
        Cow::Borrowed(key) => driver.emit_borrowed(Atom::Lexical(Text::borrowed(key))),
        Cow::Owned(key) => driver.emit(Atom::Lexical(Text::owned(key))),
    }
}

fn emit_at<'e, E: Into<Event<'e>>>(
    driver: &mut DeserializeDriver<'_, '_>,
    event: E,
    range: Range,
) -> Result<(), Error> {
    driver.state_mut().set_input_range(range.0, range.1);
    driver.emit(event)
}