Skip to content

Language Rust

Oliver Atkinson edited this page May 31, 2026 · 1 revision

Rust

The Rust generator emits a stand-alone .rs file.

Generate Rust

swift run lexicon-generate commerce.lexicon --type rust -o src/lexicon

This writes:

src/lexicon.rs

Declare the module from your crate:

mod lexicon;

use lexicon::I;

fn main() {
    let submit = lexicon::l().commerce().api().order().submit();
    println!("{}", submit.id());
}

Generated Shape

Generated Rust defines:

  • I, the shared trait with id() and localized().
  • L, the base exact-ID holder.
  • Lexicon, a root holder with methods for root lemmas.
  • l(), which creates a fresh root holder.
  • one struct per concrete lemma.
  • methods for own and inherited children.
  • l!(...), an exact-path macro.

Typed API

let submit = lexicon::l().commerce().api().order().submit();
let button = lexicon::l().commerce().ui().checkout().button().primary();

assert_eq!(submit.id(), "commerce.api.order.submit");
assert_eq!(button.id(), "commerce.ui.checkout.button.primary");

The typed API is the best default inside application code because invalid paths fail at compile time.

Exact-Path Macro

Generated Rust also supports:

let submit = lexicon::l!(commerce.api.order.submit);
assert_eq!(submit.id(), "commerce.api.order.submit");

The macro walks the generated typed API instead of generating one macro arm per lemma. Large lexicons therefore do not create huge macro tables. Invalid macro paths fail through normal Rust type checking.

Reserved Words

Rust keywords become raw identifiers:

let type_node = lexicon::l().test().r#type();
let bad = lexicon::l!(test.type.even.bad);

The macro accepts the lexical path spelling (type) and expands to the raw-identifier method (r#type()).

Cargo Integration

If generation should happen during local development:

[package.metadata.lexicon]
source = "lexicons/commerce.lexicon"
output = "src/lexicon.rs"

Then wire a project script:

swift run lexicon-generate lexicons/commerce.lexicon --type rust -o src/lexicon
cargo fmt
cargo test

If normal Rust builds should not require Swift, commit src/lexicon.rs.

Editor Support

lexicon-lsp recognizes exact-path macro calls:

l!(commerce.api.order.submit)

With editor integration, completions and unknown-path diagnostics work inside the macro path. Normal Rust tooling handles generated methods and type checking.

See Editor Support.

Clone this wiki locally