Education / learning project: Rust + AUTOSAR. The goal is to learn how to turn an official AUTOSAR XSD into usable Rust tooling — a generated model, a schema independent reader/writer/validator and a generic editing layer — and to document the pitfalls along the way (huge schemas, identifier collisions, namespace handling, …).
Tooling to read, write and validate AUTOSAR ARXML documents, driven by the official AUTOSAR XSD schemas.
The Rust code for the schema is generated with the
xsd-parser library from the XSD file.
The generated model is plain Rust (no xsd-parser dependency at runtime), so it
can be vendored and used as a standalone base library.
The generated model (
engineering/crates/arxml-model-r4-0/src/generated.rs, ~130 MB) is not committed — it is a build artifact. Run./generate.ps1once to create it before building the model crates.
AUTOSAR_00047.xsd ──► engineering/tools/xsdgen ──► engineering/crates/arxml-model-r4-0/src/generated.rs
│
engineering/crates/arxml-core ◄─────────┤ (runtime: read / write / validate)
├─ dom: generic DOM, path access, generic validation
└─ typed helpers over the generated model
│
engineering/crates/arxml (facade) ◄────┘ (version detection + dispatch)
engineering/crates/arxml-model-r4-0/src/generated.rs ──► engineering/tools/arxml-index ──► model-index.json
any .arxml ──► engineering/tools/arxml-cli (get / set / validate, schema independent)
| Path | Purpose | Depends on xsd-parser? |
|---|---|---|
engineering/tools/xsdgen |
XSD → Rust code generator (CLI) | yes (build time only) |
engineering/crates/arxml-core |
Runtime helpers: read / write / validate, namespace detection | no |
engineering/crates/arxml-model-r4-0 |
Generated model for AUTOSAR R4.0 (AUTOSAR_00047.xsd) + version API (generated.rs is produced by the generator, not committed) |
no |
engineering/crates/arxml |
Version aware facade (ArxmlVersion, detect_version, Model) |
no |
engineering/tools/arxml-index |
Scans the generated code → JSON index of types/fields | no |
engineering/tools/arxml-cli |
Schema independent CLI: version/get/set/validate |
no |
engineering/schemas/ |
XSD sources (AUTOSAR_00047.xsd, xml.xsd) |
– |
generate.ps1 |
One command regeneration of a model crate | – |
Only xsd-parser-types, quick-xml and regex are needed at runtime — the
generator itself is never linked into the shipped library.
./generate.ps1
# equivalent to:
cargo run -p xsdgen --release -- `
engineering/schemas/AUTOSAR_00047.xsd `
engineering/crates/arxml-model-r4-0/src/generated.rs `
--namespace http://autosar.org/schema/r4.0Options of engineering/tools/xsdgen:
xsdgen <schema.xsd> <generated.rs>
[--namespace <ns>] namespace of the root element (default: AUTOSAR R4.0)
[--namespace-modules] one Rust module per XML namespace (default)
[--no-namespace-modules]
[--schema-modules] one Rust module per schema
[--no-schema-modules]
[--debug] write parser/interpreter/optimizer logs to target/
The generated file is huge (≈ 130 MB of Rust, ~14 600 structs) because the AUTOSAR schema contains several thousand types.
use arxml_model_r4_0::api;
use std::io::BufReader;
let file = std::fs::File::open("model.arxml")?;
let model = api::read(BufReader::new(file))?; // streaming, event based
api::validate(&model)?; // strict schema validation
let xml = api::to_string(&model)?; // serialize
api::write(&model, std::fs::File::create("out.arxml")?)?;use arxml::{detect_version, Model};
let data = std::fs::read("model.arxml")?;
assert_eq!(detect_version(&data).map(|v| v.label()), Some("R4.0"));
let model = Model::read(&data)?;
model.validate()?;
let xml = model.to_string()?;cargo run -p arxml --example read_write_validateThe example (engineering/crates/arxml/examples/read_write_validate.rs) reads
engineering/crates/arxml/examples/data/sample.arxml, validates it, renames the package,
writes it back, re-reads it and validates it again.
The generated model repeats the same field groups (short_name_fragments,
long_name, desc, category, admin_data, introduction, annotations, …)
in thousands of structs, so no hand written code should touch those files.
Two independent tools solve that:
engineering/crates/arxml-core/src/dom.rs implements a small XML tree on top of
quick-xml only. It works for any AUTOSAR release and needs neither the
generated model nor xsd-parser:
use arxml_core::dom::Document;
use std::io::BufReader;
let file = std::fs::File::open("model.arxml")?;
let mut doc = Document::parse(BufReader::new(file))?; // streaming parse
// query by path: NAME, NAME[n] (1-based), * (wildcard)
let names = doc.texts_at("AR-PACKAGES/AR-PACKAGE/SHORT-NAME");
// update by path
doc.set_text_at("AR-PACKAGES/AR-PACKAGE/SHORT-NAME", "NewName");
// generic validation (root element, namespace, non empty SHORT-NAME,
// unique UUIDs, no mixed content)
for issue in doc.validate() {
eprintln!("{:?} at {} – {}", issue.kind, issue.path, issue.message);
}
std::fs::write("out.arxml", doc.to_xml_string()?)?;Runnable example (works without the 130 MB model crate):
cargo run -p arxml-core --example generic_editcargo run -p arxml-cli -- version model.arxml
cargo run -p arxml-cli -- get model.arxml "AR-PACKAGES/AR-PACKAGE/SHORT-NAME"
cargo run -p arxml-cli -- set model.arxml "AR-PACKAGES/AR-PACKAGE/SHORT-NAME" NewName -o out.arxml
cargo run -p arxml-cli -- validate model.arxmlThe tool scans the generated Rust and writes a JSON index, so field/type look-ups never require reading the generated file:
cargo run -p arxml-index --release -- `
engineering/crates/arxml-model-r4-0/src/generated.rs -o target/model-index.json{
"types": { "ar::ArPackageTypeContent": { "short_name": "IdentifierType", "...": "..." } },
"field_usage": { "short_name_fragments": ["ar::Type1", "ar::Type2"] }
}For the AUTOSAR R4.0 model this yields 14 640 types and 3 011 distinct fields;
short_name_fragments alone appears in 779 types.
arxml_core::validate_arxml serializes the model and parses the result back
with the strict deserializer that xsd-parser generates. The generated
deserializer enforces
- required elements (
minOccurs = 1) and cardinality (maxOccurs), - enumerations and simple type facets (patterns, lengths, ranges),
- duplicate elements/attributes and unexpected elements/attributes,
- the namespaces of the document.
Errors are reported with the position and the element path of the offending node. This is the practical equivalent of an XSD validation without shipping a separate XSD validator.
- Put the new XSD (and the W3C
xml.xsdit imports) intoengineering/schemas/. - Create a model crate, e.g.
engineering/crates/arxml-model-r4-1witharxml-core+xsd-parser-types+regexas dependencies and agenerated.rsproduced bycargo run -p xsdgen --release -- engineering/schemas/AUTOSAR_00047_4-1.xsd engineering/crates/arxml-model-r4-1/src/generated.rs --namespace <ns>. - Add the release to
ArxmlVersioninengineering/crates/arxml/src/lib.rs, add the featurer4-1 = ["dep:arxml-model-r4-1"]and extend theModelenum. - Add the crate to
[workspace] members.
Every release keeps its own generated types, so documents of different releases
can be read, written and validated side by side; detect_version dispatches a
document to the right model.
- XSD → Rust is a pipeline, not a one-liner.
xsd-parsersplits into parser → interpreter → optimizer → generator → renderer. Each stage has flags that decide whether simple-type restrictions survive, whether types are flattened, and whether namespaces become separate Rust modules. - Big schemas break naive assumptions. AUTOSAR R4.0 yields 330 complex types, 742 simple types and ~14 600 generated structs (~130 MB of Rust). The generated code compiles, but it is unreadable by hand — which is exactly why the generic DOM layer and the index tool exist.
- Name collisions are the real work. XSD names like
MEMORY-SECTIONandMEMORY-SECTION-TYPEmap to the same Rust identifier, and the XMLSchema built-ins (BooleanType,StringType, …) clash with AUTOSAR types. The fix belongs in the naming layer (engineering/tools/xsdgen/src/naming.rs), not in post-processing the generated file: theNamed/Generateddistinction ofxsd-parsermust be preserved, otherwise the generator produces code that does not compile (thousands of type-inference errors). - Two access strategies are needed. A typed model for strict validation and code generation, and a generic DOM for editing files whose release you do not have a model for.
- Validation without a separate XSD validator. Serializing the model and parsing it back with the generated strict deserializer checks cardinality, enumerations, facets, duplicates and namespaces.
- Multiple releases = multiple model crates + one facade. Each release gets
its own generated crate;
ArxmlVersion/detect_versiondispatch documents to the right model.
- The generated model is very large; compiling it takes minutes. The model
crate has a
generatedfeature (enabled by default) that can be switched off when only the facade is needed. xsd-parsermaps some AUTOSAR types to the same Rust identifier (e.g.MEMORY-SECTIONandMEMORY-SECTION-TYPE).engineering/tools/xsdgen/src/naming.rsinstalls a small naming layer that makes every type and enum variant identifier unique (MemorySectionType,MemorySectionType2, …) and splits the XML namespaces into separate Rust modules, so the built-in XMLSchema types do not collide with the AUTOSAR types.- Simple type restrictions are preserved on purpose
(
OptimizerFlags::USE_UNRESTRICTED_BASE_TYPE_SIMPLEstays disabled) so that the deserializer can validate patterns and lengths.