-
Notifications
You must be signed in to change notification settings - Fork 0
Language Rust
The Rust generator emits a stand-alone .rs file.
swift run lexicon-generate commerce.lexicon --type rust -o src/lexiconThis 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 Rust defines:
-
I, the shared trait withid()andlocalized(). -
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.
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.
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.
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()).
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 testIf normal Rust builds should not require Swift, commit src/lexicon.rs.
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.