Skip to main content

Module emit

Module emit 

Source
Expand description

Code emission: IR TypeGraph → Rust source string.

The main entry point is emit, which takes a fully-lowered TypeGraph and returns a formatted Rust source file as a String.

§Pipeline

  1. Each TypeDef variant is dispatched to the appropriate submodule.
  2. Each submodule returns a proc_macro2::TokenStream.
  3. All token streams are collected and formatted with prettyplease.

§Example

use mx20022_codegen::{xsd, ir, emit};

let schema = xsd::parse_str(r#"<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="urn:example">
  <xs:element name="Root" type="RootType"/>
  <xs:complexType name="RootType">
    <xs:sequence>
      <xs:element name="Id" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>"#).unwrap();

let graph = ir::lower(&schema).unwrap();
let code = emit::emit(&graph);
assert!(code.contains("pub struct RootType"));

Functions§

emit
Emit a complete Rust source file from the given TypeGraph.