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
//! A serde-like library for rigorous XML (de)serialization.
//!
//! instant-xml provides traits and derive macros for mapping XML to Rust types,
//! with full support for XML namespaces and zero-copy deserialization.
//!
//! # Quick Start
//!
//! ```
//! # use instant_xml::{FromXml, ToXml, from_str, to_string};
//! #[derive(Debug, PartialEq, FromXml, ToXml)]
//! struct Person {
//! name: String,
//! #[xml(attribute)]
//! age: u32,
//! }
//!
//! let person = Person {
//! name: "Alice".to_string(),
//! age: 30,
//! };
//!
//! let xml = to_string(&person).unwrap();
//! assert_eq!(xml, r#"<Person age="30"><name>Alice</name></Person>"#);
//!
//! let deserialized: Person = from_str(&xml).unwrap();
//! assert_eq!(person, deserialized);
//! ```
//!
//! # `#[xml(...)]` attribute reference
//!
//! The `#[xml(...)]` attribute configures serialization and deserialization behavior
//! for the [`ToXml`] and [`FromXml`] derive macros.
//!
//! ## Container attributes
//!
//! Applied to structs and enums using `#[xml(...)]`:
//!
//! - **`rename = "name"`** - renames the root element
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//! #[derive(ToXml)]
//! #[xml(rename = "custom-name")]
//! struct MyStruct { }
//!
//! assert_eq!(to_string(&MyStruct {}).unwrap(), "<custom-name />");
//! ```
//!
//! - **`rename_all = "case"`** - transforms all field/variant names.
//!
//! Supported cases: `"lowercase"`, `"UPPERCASE"`, `"PascalCase"`, `"camelCase"`,
//! `"snake_case"`, `"SCREAMING_SNAKE_CASE"`, `"kebab-case"`, `"SCREAMING-KEBAB-CASE"`.
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//! #[derive(ToXml)]
//! #[xml(rename_all = "camelCase")]
//! struct MyStruct {
//! field_one: String,
//! }
//!
//! let s = MyStruct { field_one: "value".to_string() };
//! assert_eq!(to_string(&s).unwrap(), "<MyStruct><fieldOne>value</fieldOne></MyStruct>");
//! ```
//!
//! - **`ns("uri")` or `ns("uri", prefix = "namespace")`** - configures XML namespaces
//!
//! The first positional argument sets the namespace for the element. If the parent's namespace
//! differs and no ancestors set a prefix for this namespace, a `xmlns="uri"` declaration is
//! emitted on the element. If a prefix is declared for the namespace, it is used. Fields
//! without their own `ns(...)` inherit the type's namespace.
//!
//! Additional `prefix = "uri"` entries declare prefix mappings that are emitted as
//! `xmlns:prefix="uri"` on the element. These prefixes can then be referenced by
//! fields and child elements. Namespace URIs can be string literals or paths to constants.
//! Prefix names may contain dashes and dots: `#[xml(ns(my-ns.v1 = "uri"))]`.
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//! #[derive(ToXml)]
//! #[xml(ns("http://example.com"))]
//! struct Root { }
//!
//! assert_eq!(to_string(&Root {}).unwrap(), r#"<Root xmlns="http://example.com" />"#);
//!
//! #[derive(ToXml)]
//! #[xml(ns("http://example.com", xsi = XSI))]
//! struct WithPrefix { }
//!
//! assert_eq!(
//! to_string(&WithPrefix {}).unwrap(),
//! r#"<WithPrefix xmlns="http://example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />"#
//! );
//!
//! const XSI: &'static str = "http://www.w3.org/2001/XMLSchema-instance";
//! ```
//!
//! When a child struct has a different default namespace than its parent, a new `xmlns="..."` is
//! emitted on the child element. Prefix declarations from the parent are inherited and not
//! redeclared.
//!
//! - **`transparent`** *(structs only)* - inlines fields without wrapper element
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//! #[derive(ToXml)]
//! #[xml(transparent)]
//! struct Inline {
//! foo: Foo,
//! bar: Bar,
//! }
//!
//! #[derive(ToXml)]
//! struct Foo { }
//!
//! #[derive(ToXml)]
//! struct Bar { }
//!
//! let inline = Inline { foo: Foo {}, bar: Bar {} };
//! assert_eq!(to_string(&inline).unwrap(), "<Foo /><Bar />");
//! ```
//!
//! - **`scalar`** *(enums only)* - serializes variants as text content.
//!
//! The enum must only have unit variants.
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//!
//! #[derive(ToXml)]
//! struct Container {
//! status: Status,
//! }
//!
//! #[derive(ToXml)]
//! #[xml(scalar)]
//! enum Status {
//! Active,
//! Inactive,
//! }
//!
//! let c = Container { status: Status::Active };
//! assert_eq!(to_string(&c).unwrap(), "<Container><status>Active</status></Container>");
//! ```
//!
//! Variants can use `#[xml(rename = "...")]` or string/integer discriminants.
//!
//! - **`forward`** *(enums only)* - forwards to inner type's element name.
//!
//! Each variant must contain exactly one unnamed field.
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//!
//! #[derive(ToXml)]
//! #[xml(forward)]
//! enum Message {
//! Request(Request),
//! Response(Response),
//! }
//!
//! #[derive(ToXml)]
//! struct Request { }
//!
//! #[derive(ToXml)]
//! struct Response { }
//!
//! let msg = Message::Request(Request {});
//! assert_eq!(to_string(&msg).unwrap(), "<Request />");
//! ```
//!
//! -**`force_prefix`** *(structs only)* - Always serialize a namespace prefix if one is set for this element's namespace.
//! Does not affect deserialization.
//!
//! ## Field attributes
//!
//! Applied to struct fields using `#[xml(...)]`:
//!
//! - **`attribute`** - (de)serializes as XML attribute instead of child element
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//! #[derive(ToXml)]
//! struct Element {
//! #[xml(attribute)]
//! id: String,
//! }
//!
//! let elem = Element { id: "abc123".to_string() };
//! assert_eq!(to_string(&elem).unwrap(), r#"<Element id="abc123" />"#);
//! ```
//!
//! - **`direct`** - field contains element's direct text content
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//! #[derive(ToXml)]
//! struct Paragraph {
//! #[xml(attribute)]
//! lang: String,
//! #[xml(direct)]
//! text: String,
//! }
//!
//! let p = Paragraph { lang: "en".to_string(), text: "Hello".to_string() };
//! assert_eq!(to_string(&p).unwrap(), r#"<Paragraph lang="en">Hello</Paragraph>"#);
//! ```
//!
//! - **`rename = "name"`** - renames the field's element or attribute name
//!
//! - **`ns("uri")`** - sets namespace for this specific field
//!
//! Like the container-level attribute, this supports both string literals and constant
//! paths. How the namespace is serialized depends on whether it matches a prefix
//! declared on the container:
//!
//! - If the URI matches a declared prefix, the field is serialized with that prefix
//! (`<bar:field>`).
//! - If the URI does not match any declared prefix, a `xmlns="uri"` is emitted
//! directly on the field's element.
//! - Fields without `ns(...)` inherit the container's namespace.
//!
//! For `attribute` fields, the namespace must reference a URI that has a declared
//! prefix (XML attributes cannot use unprefixed default namespaces).
//!
//! ```
//! # use instant_xml::{ToXml, to_string};
//! const BAZ: &str = "http://baz.example.com";
//!
//! #[derive(ToXml)]
//! #[xml(ns("http://example.com", bar = BAZ))]
//! struct Example {
//! plain: bool, // inherits default ns "http://example.com"
//! #[xml(ns(BAZ))]
//! prefixed: String, // matches prefix "bar", serialized as <bar:prefixed>
//! #[xml(ns("http://other"))]
//! direct: i32, // no matching prefix, emits xmlns="http://other"
//! }
//!
//! assert_eq!(
//! to_string(&Example { plain: true, prefixed: "val".into(), direct: 1 }).unwrap(),
//! concat!(
//! r#"<Example xmlns="http://example.com" xmlns:bar="http://baz.example.com">"#,
//! "<plain>true</plain>",
//! "<bar:prefixed>val</bar:prefixed>",
//! r#"<direct xmlns="http://other">1</direct>"#,
//! "</Example>",
//! ),
//! );
//! ```
//!
//! - **`serialize_with = "path"`** - custom serialization function with signature:
//!
//! ```
//! # use instant_xml::{Error, Serializer, ToXml, to_string};
//! # use std::fmt;
//! #[derive(ToXml)]
//! struct Config {
//! #[xml(serialize_with = "serialize_custom")]
//! count: u32,
//! }
//!
//! fn serialize_custom<W: fmt::Write + ?Sized>(
//! value: &u32,
//! serializer: &mut Serializer<'_, W>,
//! ) -> Result<(), Error> {
//! serializer.write_str(&format!("value: {}", value))?;
//! Ok(())
//! }
//!
//! let config = Config { count: 42 };
//! assert_eq!(to_string(&config).unwrap(), "<Config>value: 42</Config>");
//! ```
//!
//! - **`deserialize_with = "path"`** - custom deserialization function with signature:
//!
//! ```
//! # use instant_xml::{Deserializer, Error, FromXml, from_str};
//! #[derive(FromXml, PartialEq, Debug)]
//! struct Config {
//! #[xml(deserialize_with = "deserialize_bool")]
//! enabled: bool,
//! }
//!
//! fn deserialize_bool<'xml>(
//! accumulator: &mut <bool as FromXml<'xml>>::Accumulator,
//! field: &'static str,
//! deserializer: &mut Deserializer<'_, 'xml>,
//! ) -> Result<(), Error> {
//! if accumulator.is_some() {
//! return Err(Error::DuplicateValue(field));
//! }
//!
//! let Some(s) = deserializer.take_str()? else {
//! return Ok(());
//! };
//!
//! *accumulator = Some(match s.as_ref() {
//! "yes" => true,
//! "no" => false,
//! other => return Err(Error::UnexpectedValue(
//! format!("expected 'yes' or 'no', got '{}'", other)
//! )),
//! });
//!
//! deserializer.ignore()?;
//! Ok(())
//! }
//!
//! let xml = "<Config><enabled>yes</enabled></Config>";
//! let config = from_str::<Config>(xml).unwrap();
//! assert_eq!(config.enabled, true);
//! ```
//!
//! - **`borrow`** - Borrows from input during deserialization. Automatically applies to
//! top-level `&str` and `&[u8]` fields. Useful for `Cow<str>` and similar types.
//!
//! ```
//! # use instant_xml::{FromXml, from_str};
//! # use std::borrow::Cow;
//! #[derive(FromXml, PartialEq, Debug)]
//! struct Borrowed<'a> {
//! #[xml(borrow)]
//! text: Cow<'a, str>,
//! }
//!
//! let xml = "<Borrowed><text>Hello</text></Borrowed>";
//! let parsed = from_str::<Borrowed>(xml).unwrap();
//! assert_eq!(parsed.text, "Hello");
//! ```
use ;
use Error;
pub use ;
use Context;
pub use Deserializer;
pub use ;
pub use Serializer;
pub use ;
/// Serialize a type to XML
/// Deserialize a type from XML
/// Accumulate values during deserialization
///
/// A type implementing `Accumulate<T>` is used to accumulate a value of type `T`.
/// Deserialize a type from an XML string
/// Serialize a value to an XML string
/// Serialize a value to an XML writer
/// Marker trait for types that can be deserialized with any lifetime
/// Errors that can occur during XML serialization and deserialization
/// The kind of XML node a type represents
/// Identifier for an XML element or attribute with namespace