What XML to Rust does
XML to Rust turns a sample XML document into Rust code: one struct with Serialize/Deserialize derives per object, with a type for every field inferred from the value in the sample.
XML to Rust examples
The built-in sample
Input
<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>Output
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Root {
pub _id: String,
pub name: String,
}This is the input that the Load sample button fills in.
Attributes, repeated elements and empty elements
Input
<order id="7" paid="false"><total>19.5</total><item sku="A1"><qty>2</qty></item><item sku="B2"><qty>1</qty><gift/></item><note/></order>Output
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Item {
pub _sku: String,
pub qty: String,
pub gift: String,
}
#[derive(Serialize, Deserialize)]
pub struct Root {
pub _id: String,
pub _paid: String,
pub total: String,
pub item: Vec<Item>,
pub note: String,
}In the sample, id, paid and sku are attributes, item repeats, and gift and note are empty elements. Every value is typed as a string, because XML does not carry types.
Each output was produced by running this tool on the input above it.
How it works
The sample is walked recursively and each value is mapped to a Rust type:
| Sample value | Rust type |
|---|---|
| String | String |
| Whole number | i64 |
| Decimal number | f64 |
| true / false | bool |
| null, or an empty array’s items | serde_json::Value |
| Array of T | Vec<T> |
| Object | a new named type |
XML has no data types, so every leaf value is generated as the Rust string type. Attributes become fields too: the @ prefix becomes an underscore (_id). Repeated elements become lists, and the root element itself is always called Root.
Reading XML into the generated types
With the serde support of the quick-xml crate, a field that holds an attribute is renamed to the attribute name with an @ prefix, for example #[serde(rename = "@id")].
Details and limits
- A key that is not a valid identifier is renamed with underscores. Add
#[serde(rename = "order-id")]above the field so that it still matches the data. - Whole numbers are
i64and decimalsf64. - A field that can be missing or null must be wrapped in
Option<T>by hand; the generator makes every field required. - A key that is a Rust keyword, such as
type, is emitted unchanged. Write it in the raw formr#type. - A type is named after the key that holds the object:
itemsgives a type calledItems, with no attempt to make it singular. The top-level type is alwaysRoot. - If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
- Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
- Input that is not valid XML stops the tool, and the status line shows where the problem is. The XML Validator page lists the usual causes.
Questions
Is my data uploaded when I use XML to Rust?
No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.
What does XML to Rust do?
XML to Rust turns a sample XML document into Rust code: one struct with Serialize/Deserialize derives per object, with a type for every field inferred from the value in the sample.
Are there any limits I should know about?
A key that is not a valid identifier is renamed with underscores. Add #[serde(rename = "order-id")] above the field so that it still matches the data. Whole numbers are i64 and decimals f64.
Is there a size limit?
There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.
Does it work on phones, and is it free?
Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.