instant-xml
A serde-like Rust library that rigorously maps XML to typed structs with full namespace support.
Repository Health
Technical Analysis
instant-xml is a Rust library providing traits and procedural derive macros to map the XML data model onto Rust types. Where serde’s structure is a poor fit for XML, instant-xml more rigorously models XML including namespaces while keeping the familiar, ergonomic serde-like interface developers already know.
It favors zero-copy deserialization where possible, borrowing directly from the input rather than allocating, and drives serialization and deserialization from derive annotations. It is used in production at Instant Domain Search.
What You Get
- ToXml and FromXml derive macros that generate serialization and deserialization code from struct and enum definitions
- to_string and from_str entry points for round-tripping XML to typed values
- First-class XML namespace handling, including prefix declarations, inheritance, and renaming via xml() attributes
- Zero-copy deserialization that borrows from the input buffer where lifetimes permit
- Optional chrono integration for date and time types
Common Use Cases
- Parsing namespaced XML API responses (SOAP, Atom, domain registry protocols) into typed Rust structs
- Serializing Rust data models into well-formed XML with controlled element and attribute layout
- Round-tripping XML configuration or document formats where namespaces must be preserved exactly
- Replacing serde plus quick-xml pipelines that struggle with the XML namespace data model
Under The Hood
Architecture - The project is a two-crate Cargo workspace: instant-xml holds the runtime traits and (de)serializers (ser.rs, de.rs, impls.rs, any_element.rs) while instant-xml-macros is a proc-macro crate that generates ToXml/FromXml implementations from derive and xml() attributes. At runtime, serialization walks the derived tree emitting elements and attributes with namespace resolution, and deserialization is driven by xmlparser events fed into the derived FromXml state machines, borrowing from the source buffer for zero-copy where lifetimes allow.
Tech Stack - Pure Rust (edition 2021, MSRV 1.71). The runtime depends on xmlparser for tokenizing, thiserror for error types, and the internal instant-xml-macros crate; the macro crate uses syn, quote, proc-macro2, and heck for case conversion. chrono is an optional feature for date/time support.
Code Quality - The repo enforces a broad set of workspace clippy and rustc lints (use_self, redundant_clone, unreachable_pub, and more) and carries an extensive integration test suite of roughly two dozen test files covering attributes, namespaces, nested and forwarded enums, generics, lifetimes, escaping, scalars, options, and vecs, plus a decode benchmark. Doc comments on the public API include compile-tested examples.
API Design - The public surface is deliberately serde-shaped: derive ToXml/FromXml, then call to_string/from_str, with behavior tuned through a well-documented xml() attribute vocabulary (rename, rename_all, ns/prefix, attribute). This keeps boilerplate minimal for anyone familiar with serde, though the namespace attribute grammar and the library’s early-lifecycle, still-evolving trait APIs add some learning curve.