Skip to content

Latest commit

 

History

History
827 lines (690 loc) · 62.2 KB

rust.md

File metadata and controls

827 lines (690 loc) · 62.2 KB

Rust port

The Rust port is a full language of 1.3.0 (ADR-0065). It reads a project, validates it, serializes it and generates code from it, and it is held to the same shared conformance corpora as the other five ports. Its command, cargo meta, runs the whole codegen loop on a JSON or YAML model: init, gen, verify --codegen, verify --templates and eject. Eleven generators ship, from the model crate through the store, the rules, the service and its axum routes to a typed client and its commands, and a project chooses which of them run in metaobjects.config.json. A generated service passes 77 scenarios of the shared API-contract corpus over HTTP (see Conformance); examples/rust-service/ is a whole app built that way. The four crates are ready to publish but not published: they package and build from their packages (a cargo publish --dry-run runs in the rust lane), and there is no release on crates.io yet, so there is nothing to cargo add or cargo install from a registry. You work from a checkout of this repository until the first release.

This page says what runs today, what does not exist yet, and how to build and test it. For the decisions behind the port (why Rust, the layout, the conformance rules), read the ADR.

What works today

The loader is the metaobjects crate, and the workspace at server/rust/ adds the codegen crate (metaobjects-codegen, the run engine and generator), the CLI crate (metaobjects-cli, the cargo-meta binary) and the prompt crate (metaobjects-render, render and the reply parser). What each area does:

Area What it does
Loading Reads canonical JSON and sigil-free authoring YAML (ADR-0006): packages, children, subtypes, attributes, extends and references, with a line and column on every error from a YAML file.
Project resolution Reads .metaobjects/config.json and resolves sources the way every port does: the four source kinds, directory discovery (symlinks followed, _pending skipped), and the iam and ai library sources, which are embedded in the crate so a library resolves with no file on disk. See Metadata sources.
Naming The naming strategies, the pluralizers and the collection-name collision check (ADR-0058).
Registry The type registry and the built-in providers. It is strict and sealed after bootstrap, with the same ERR_* codes as the other ports.
Validation All 41 validation passes the TypeScript loader runs, in the same order, the business-rules pass among them (operations, permission names and lifecycles, ADR-0066). The ordered list is PASSES in server/rust/metaobjects/src/validate/mod.rs.
Serializer The canonical serializer. A tree it loads serializes to the same bytes the other ports write.
Prompt rendering metaobjects_render::render renders a template.* body over a JSON payload as every port does: partials expanded ahead of time, each {{value}} escaped for the template's format (text, markdown, html, xml, csv, spreadsheet, json), derived has<Field> and -last accessors, and a fail-closed maxChars budget. The Mustache engine is the crate's own, following the reference renderer's name lookup, truthiness and whitespace rules.
Reply parser metaobjects_render::extract::extract reads a typed answer out of a model's reply (FR-010, FR-011): fences, prose, comments, trailing commas and cut-off values tolerated, every declared field classified (EXTRACTED, DEFAULTED, LOST_OPTIONAL, LOST_REQUIRED, MALFORMED) and coerced to its kind, under a STRICT, NORMAL or LOOSE tolerance. metaobjects_render::prompt::render_output_format writes the output-format fragment that asks for that answer, in the guide, inline and exampleOnly styles.
Frozen tree The loader freezes the tree when it finishes. A frozen tree memoizes its resolved reads (effective children, attributes and array-ness), so many readers can share one.
Timings Every load records how long each phase and each validation pass took (LoadResult::timings).
Report shapes metaobjects::report_shape::report_shape derives an object.report's fields (FR-044 Table B, with Table C's required): each dimension and measure, its subtype, and the @of field it takes its type from. It byte-matches fixtures/persistence-conformance/report-shapes.json, as every port does.
Docs view metaobjects::docs_view::build builds the port-neutral docs view (docs/features/docs-view.md) from the loaded model, with the requirement ledger, the generated files and the routes when it is given them. It byte-matches fixtures/explorer-conformance/, as every port does.
Metadata routes metaobjects::meta_endpoint holds the bodies of GET /_meta (meta_json) and GET /_meta/docs (docs, with its ETag and the If-None-Match check), framework-free, for the host to mount (Metadata API).
Code generation (cargo meta) The run engine, sixteen generators (entity, rules-names, contract-types, repository, service, service-routes, client, commands, render-helper, output-parser, output-prompt, the opt-in shared-model publisher, and four opt-in docs generators: metamodel-docs, the metamodel reference pages rendered from the shared Mustache set, requirements and agent-docs, the requirement and agent pages read off the docs view, and docs-site, the model explorer), the codegen and template drift gates and eject. See The command and What gen writes below.

The command: cargo meta

cargo meta is a Cargo subcommand (a cargo-meta binary), the way the other build-tool-hosted ports spell theirs (dotnet meta, mvn metaobjects:...). A bare meta stays the Node schema CLI. Install it from the checkout:

cargo install --path <repo-root>/server/rust/metaobjects-cli

To use a candidate or any other commit without a checkout, pin it from git (cargo install --git https://github.com/metaobjectsdev/metaobjects --rev <commit> metaobjects-cli, and the same git + rev on a metaobjects dependency). The candidate log (tools/prerelease/candidates.md) names the commit each staged candidate was built from; see Pre-releases, "Rust".

Command What it does
cargo meta init [--project <dir>] [--transport] Scaffolds .metaobjects/config.json, .metaobjects/.gitignore (which keeps .gen-state/.hashes/ committed), a first metaobjects/meta.app.yaml and metaobjects.config.json, the list of generators to run (see Choosing generators). --transport adds the opt-in service-routes generator to that list. It writes only what is missing, and never adds an empty metadata file to a project that already has metadata.
cargo meta gen [ENTITY...] Generates the crates. It skips the whole run when the model, the project config and the generators are unchanged (ADR-0064), writes only files whose bytes changed, removes a generated file nothing emits any more, and refuses to overwrite or delete a generated file that was edited by hand. It prints what it created, updated and removed, and the crates to re-check.
cargo meta verify [--codegen] [--templates] [--deps] [--full] The codegen drift gate: renders in memory, compares with disk, writes nothing, and exits 1 on drift, naming each file. Locally it may answer from the stored skip key; --full, or a set CI variable, always renders. --codegen runs that gate alone. --templates is the template drift gate: each template.* body (under prompts/, or --prompts <dir>) against its payload value object, and each prompt's @responseRef reply; a bare verify runs it too, and a Jinja body is refused (ERR_TEMPLATE_ENGINE_UNSUPPORTED). A bare verify also runs the business-rules gate (ADR-0066) on the loaded model, and prints nothing about it when the model declares no operation and no transition; an error-severity finding (a rules.permissions.names of require, or verify.lifecycle.unreachable of error) exits 1. --deps is the dependency check (FR-023), opt-in and never part of a bare verify: each declared dependency's publisher artifact (its metaobjects.pkg.json and the artifact beside it) is hashed and compared with .metaobjects/deps.lock.json, and a drifted or unresolved one exits 1 with ERR_DEPENDENCY_UPSTREAM_DRIFT (an unreadable config or lock, or a --project that is not a project, exits 2); it resolves the path transport only, and reports any other dependency unresolved, naming the Node meta deps check. --db exits 2 with the reason and where that check lives.
cargo meta eject <generator>... Copies a shipped generator into the project to own. An existing copy is refused; --force replaces it with the reference, and leaves a copy that already matches untouched. --undo takes back copies still identical to the shipped source; --list shows what can be ejected and how each owned copy compares. See Owning a generator.
cargo meta agent-docs [--server <lang>]... [--client <fw>]... [--out <dir>] [--no-wire-root] Writes the static agent files, .metaobjects/AGENTS.md, the metaobjects-* skills under .claude/skills/ and the .metaobjects/.agent-context.json manifest, byte for byte what every other port's agent-docs writes (fixtures/agent-context-conformance/), with no Node: the files are embedded in the crate. With no --server and no --client it reuses the stack the last run recorded, else rust. A re-run writes nothing for an unchanged file and puts fresh text beside a hand-edited one as <file>.new; it also adds the @.metaobjects/AGENTS.md import to the root AGENTS.md / CLAUDE.md unless --no-wire-root. An unknown stack exits 2.

gen and verify share --project <dir>, --out <dir> (default generated), --generators <a,b>, --strict and --timings. gen adds --force and --list. cargo meta --help is the reference.

Choosing generators

The generators a project runs are the list in metaobjects.config.json at the project root, the way the other ports choose theirs (metaobjects.config.ts, codegen/Generators.cs, the pom, metaobjects.config.yaml). cargo meta init writes every shipped generator but the opt-in ones: the transport, service-routes, which cargo meta init --transport adds, the shared-model publisher, and the four docs generators, metamodel-docs, requirements, agent-docs and docs-site, which you add by name. Delete a line to stop running one, and no eject is needed:

{ "generators": ["entity", "client", "commands", "render-helper", "output-parser", "output-prompt"] }

The file takes that one key: a layer is only a generator, so there are no layer settings. An entry is a name, or {"name": "<generator>", "options": {...}} for a generator that takes options; the generator's configure reads them and refuses a key it does not know (ERR_UNKNOWN_GENERATOR_OPTION, naming the options it takes). Of the shipped generators only shared-model, metamodel-docs, agent-docs and docs-site take options (metamodel-docs and agent-docs take subDir; docs-site takes the six in its section; shared-model the six in its section), so any other option fails the run, also a run --generators narrows to other generators. A project with no such file runs every generator but service-routes, shared-model and the four docs generators, which run only when they are listed: an HTTP server is a choice the project makes, not a default, and so are the metamodel reference pages and the model explorer. A generator that leaves the list has its files removed on the next gen (a file somebody edited is refused by name instead), and the run fails, naming the file, for a name nothing offers, a duplicate, an unknown key, or a generator listed without one it builds on (client, render-helper and output-parser need entity; commands needs entity and client; each layer needs the one beneath it, up to service-routes, which needs service). cargo meta gen --list shows each generator as [configured] or [not configured], marks the opt-in ones [opt-in] and the one that cannot be ejected [package-only], and --generators narrows one run to some of the list.

Exit codes are the ones every port's tool uses: 0 success, 1 the run failed (a model that does not load, a refused file, drift), 2 a usage error. An unknown flag names the flags the command takes. An owned generator crate that fails to build, or a generator in it that panics, is a failed run: 1, with the compiler's or the panic's own message.

There is no migrate, no verify --db and no fmt in cargo meta. Schema migrations run in your own migration tool (sqlx migrations, say), handed the schema meta schema prints; the Node meta migrate is the reference migration helper, one option, and the database check is the Node meta verify --db (ADR-0015).

What gen writes

One crate per metadata package (ADR-0065), per generator, plus one shared crate per generator. For a package shop:

generated/
  shop/           entity     the model: structs, enums, create and patch bodies, validate()
  shop_client/    client     a typed HTTP client and list query per served entity
  shop_cli/       commands   clap commands over the client
  shop_prompts/   render-helper   a typed render function per template (when the package has templates)
  shop_parsers/   output-parser   a strict parse and a tolerant extract per responding prompt
  shop_formats/   output-prompt   the output-format fragment per responding prompt
  shop_rules/     rules-names     lifecycles, commands, operations, permissions, business errors (when the package declares rules)
  shop_contract/  contract-types  each operation's input, output and errors (when the package declares operations)
  shop_store/     repository list, count, get, create, update, delete and the guarded update per stored entity, on sqlx
  shop_service/   service    each operation in one transaction, with the rules checks, over the store
  shop_http/      service-routes  the axum routes over the service (when listed)
  meta_support/   entity     exact decimals, durations, patch helpers, the validation checks
  meta_client/    client     the HTTP transport (reqwest), errors, the query builder
  meta_cli/       commands   what a command uses to build a body and a query
  meta_store/     repository the table descriptor, filters, and the Postgres and SQLite statements
  meta_service/   service    the error codes and statuses, the transaction and the guarded move
  meta_http/      service-routes  the list query parser, the body checks and the error responses

The generated crates depend on each other by path and on ordinary crates.io crates (serde, serde_json, reqwest, clap, sqlx, axum). The model, client and command crates import nothing from MetaObjects: the shared code is written into the meta_* crates as source you can read and change. The three prompt-tier crates import metaobjects-render, an ordinary Apache-2.0 library (the render engine and the reply parser), as every port's prompt tier imports its render package. It is not on crates.io yet, so until it is, point a [patch.crates-io] entry at a checkout's server/rust/metaobjects-render. Add the crates you want to your workspace as path dependencies:

[dependencies]
shop = { path = "generated/shop" }
shop_client = { path = "generated/shop_client" }
meta_client = { path = "generated/meta_client" }

Every file is written already formatted, and each crate carries the rustfmt.toml it was written under, so cargo fmt over the output changes nothing.

entity: the model crate

In the model In Rust
a concrete object.entity or object.value a pub struct with every effective field (inherited ones included), serde derives
a served read: a concrete object.projection or object.report whose read source is a view a read-only pub struct, with no create or patch body. A report's members are its dimensions, then its measures, each typed as FR-044 Table B derives it (a dimension and a min/max as their @of field, a count as i64, a ratio as Decimal, a sum of an int as i64) and an Option unless Table C says the column is never null
an entity also <Name>Create (what a caller sends: no generated key, no read-only or derived member) and <Name>Patch (every member optional; a nullable member tells "set to null" from "leave alone")
a field.enum a pub enum that serializes as the member symbols
a field that is not @required Option<T>; an array is Vec<T>; a field.map is BTreeMap<String, V>
field.object the struct its @objectRef names, Boxed when it closes a cycle; a reference into another package is a path dependency on that package's crate
field.decimal meta_support::wire::Decimal, which holds the value as exact decimal text. It writes the portable decimal string and reads a string or a JSON number (a string stays exact; a fractional JSON number arrives as a float). Rust has no standard decimal type, so wire.decimals has nothing native to select
field.duration meta_support::wire::Duration, signed ISO-8601
field.currency i64 minor units
field.inet std::net::IpAddr (text when the field is @lenient)
date, time, timestamp, uuid, uri String, as on the wire; validate() checks the form

A value object, a create body and a patch body each have validate(), which returns every rule of the model the value breaks: lengths, bounds, item counts, patterns, text forms, the rules of a nested value object and, on a create body, the model's rules across members (validator.comparison, requiredWhen, presentIff, atLeastOne, exactlyOne). A validator.regex pattern is matched by a small matcher the support crate carries, so no regex crate is needed; a pattern outside what that matcher supports is refused when you generate, not at run time. The matcher keeps its choice points on the heap, so a text of any length matches a repeated group such as ^(\w+\s?)*$ without overflowing the stack. Each match has a step budget that grows with the text, and a pathological pattern that spends it reports its own violation, 'field' pattern could not be evaluated within limits, never the ordinary mismatch.

The project's wire.nullMembers setting is honored: omit leaves a None member out of the JSON, and the default writes null, which is what serde does on its own (ADR-0019 Amendment 2). Request bodies (<Name>Create, <Name>Patch) always leave an unset member out.

Navigation across relationships is not generated: a relationship is not a field, and the foreign-key fields it is built on are.

client: a typed client for the REST API the model implies

Every port's server generators serve the same routes for the same model (the API contract), so a client for them is derivable from the model alone. For each served entity (concrete, backed by a source.rdb):

let api = meta_client::Client::new("https://example.test/api");
let orders = shop_client::OrderClient::new(&api);

let query = shop_client::OrderQuery::new()
    .status_eq(shop::OrderStatus::InFlight)
    .total_gte(1250)
    .sort(shop_client::OrderSort::Total, meta_client::Direction::Descending)
    .limit(20);
let rows: Vec<shop::Order> = orders.list(&query).await?;
let page = orders.page(&query).await?;          // rows and the total the filters match
let one = orders.get(7).await?;
  • <Name>Query has one method per field and operator the model's @filterable allows, taking a value of the field's own type, and sort takes an enum of the @sortable fields. A request the server would refuse for its filter or its sort does not compile. raw is the escape hatch.
  • get, update and delete exist when the entity has a single key of text or a whole number. An entity with a composite key is listed and created only.
  • A view-backed entity gets reads only. A subtype of a single-table hierarchy is reached under its base, at its discriminator value (auths/bridge). A many-to-many relationship gets <relation>_of(id).
  • An error is meta_client::Error: Transport, Api (the status and the contract's {error, message} body) or Decode.

The transport is reqwest with its default features plus json and query, written out as the meta_client crate. A project that wants another HTTP stack or TLS backend changes that one crate, which it owns.

Not generated yet: clients (and commands) for object.projection and object.report routes. The Rust transport serves neither yet (see What does not exist yet), so they wait for it.

commands: command-line commands over the client

For each served entity the generator writes a clap command group: list, get, create, update, delete and one command per many-to-many relationship. A <package>_cli crate exports a clap::Subcommand and an asynchronous run, and no main: the binary, its name, where the API's address comes from and how an answer is printed are your application's.

#[derive(clap::Parser)]
struct App {
    #[command(subcommand)]
    command: shop_cli::Command,
}

let app = <App as clap::Parser>::parse();
let api = meta_client::Client::new(&std::env::var("SHOP_API")?);
let answer = shop_cli::run(app.command, &api).await?;   // a serde_json::Value to print
app order list --filter status=IN_FLIGHT --filter total:gte=1250 --sort total:desc --limit 20 --count
app order create --json "$(cat order.json)" --total 1250
app order update 7 --status done --total 1500

list checks --filter and --sort against the same allowlists the typed client is built from. create and update take the body as --json (or - for standard input) and one flag per scalar member, a flag winning over the body. The finished body is read as the model's create or patch type and held to its validate() before anything is sent, and a member the model does not declare is refused, not dropped.

commands is Rust-only, by decision (ADR-0065 Amendment 1). No other port ships it. The shared generator registry records client and commands with rust as their only port, so the difference is stated in the one place every port checks itself against, not left to be discovered.

render-helper: a render function per template

For each template.output and template.prompt the generator writes a typed function in a <package>_prompts crate. A document template (the default @kind, and every prompt) gets render_<name>(&payload, &provider) -> Result<String, RenderError>, escaped for its @format and held to its @maxChars. An email template gets one returning an EmailDocument: the subject and the text body rendered as text, the HTML body as HTML. The payload is the value object's own struct from the entity crate (ADR-0056).

use metaobjects_render::FilesystemProvider;

let provider = FilesystemProvider::new("prompts");
let text = shop_prompts::render_order_summary(&summary, &provider)?;

The drift gate runs when you generate. Each body is read from the project's template directory (prompts/ when it exists, else templates/) and checked against the payload's fields, nested value objects included. A variable the payload does not have fails cargo meta gen with ERR_VAR_NOT_ON_PAYLOAD, naming the template, the reference and the variable. The field tree is also written into the function, and every render checks the body again, so a body edited after generation, or served by another provider, fails the render instead of rendering a blank. A body is an input of the generator: editing one regenerates the crate even when the model did not change.

output-parser and output-prompt: the reply to a prompt

A template.prompt with @responseRef names the value object its answer is read into (ADR-0052). output-parser writes, in a <package>_parsers crate:

  • parse_<name>(text) -> Result<T, serde_json::Error>, the strict parse: the reply must be exactly the JSON of a T. A prompt whose @responseFormat is xml has none (ADR-0053).
  • extract_<name>(text, &opts) -> Extracted<T>, the tolerant read of a raw model reply. It never fails: value is the answer when the extracted fields make a complete T, and outcome.report classifies every field either way.
  • <name>_schema(), the extract descriptor both read with, derived from the response object as every port derives it: nested value objects, numeric ranges and element counts from the field's validators, @enumAlias, @coerceDefault, @default, @normalize and @xmlText.

output-prompt writes, in a <package>_formats crate, render_<name>_format(&overrides) -> String: the part of the prompt that tells the model how to shape its answer, in the reply's syntax and the prompt's @promptStyle (guide, inline or exampleOnly), with each field's @example, @instruction and @enumDoc. Its root is the response object's name, the root the parser looks for.

let fragment = shop_formats::render_judge_format(&PromptOverrides::default());
let reply = call_the_model(&format!("{prompt}\n\n{fragment}"))?;
let got = shop_parsers::extract_judge(&reply, &ExtractOptions::default());
match got.value {
    Some(verdict) => use_it(verdict),
    None => log_the_report(&got.outcome.report),
}

rules-names and contract-types: the rules layers

The business-rules vocabulary (ADR-0066) gets the first two layers of ADR-0067. Both write nothing for a package that declares no lifecycle and no operation, so turning them on changes nothing for a model without rules.

rules-names (layer 0) writes a <package>_rules crate of names hand-written code can match on:

  • a module per entity with a lifecycle, and in it a module per lifecycle field: State (the model crate's own enum, so a value read off a record compares directly), FIELD, STATES, INITIAL, TERMINAL and Transition (name(), from(), to()), plus Command, the commands derived from the transitions no operation claims, with the permission each asks about;
  • Operation (kind(), permission(), transitions(), errors()), Permission (every permission the package asks about, declared or derived: <package>.<operation> and <package>.<Entity>.<transition>), BusinessError and ShellError (the five codes the enforcing shell answers with). Each is a closed enum with ALL.

contract-types (layer 1) writes a <package>_contract crate. Its operations module has, per operation, <Op>Input and <Op>Output (the model's own types, a Vec for @many), <Op>BusinessError (its declared @errors, or NoBusinessError) and <Op>Error, the error body whose code is a fixed one or one of its own. A projection an operation returns gets its read shape in that crate, unless it is view-backed: then it is the model crate's, and the operation names that. The shared meta_contract crate holds the wire error contract: ErrorCode with each code's HTTP status, ErrorBody, ValidationErrorBody and ConstraintViolationBody. An entity's read, create and patch shapes, with validate(), are the model crate's; it carries no persistence mapping, so it already is the contract.

let body: DecideError = serde_json::from_slice(&response)?;
match body.error {
    OperationCode::Business(DecideBusinessError::OverBudget) => ask_for_less(),
    OperationCode::Fixed(code) => fail(code.status()),
    OperationCode::Business(DecideBusinessError::AlreadyDecided) => refresh(),
}

repository: data access on sqlx

repository (ADR-0067 layer 3) writes a <package>_store crate with a module per entity that has a source, and per served read (a view-backed projection or report). Each module has TABLE, the physical mapping (table, schema, key, and each field's column and storage kind, under the project's naming strategies and every @table/@column), and typed functions over it:

  • list(c, &Query) (a Filter of the nine operators, sort and paging), count(c, Option<&Filter>) and get(c, id), which a report (it has no key) does not get;
  • create(c, &XCreate), update(c, id, &XPatch) (None when there was no row) and delete(c, id) (false when there was none), for an entity whose source is writable. A projection and a report are read only;
  • per lifecycle (ADR-0066), the guarded update transition_<field>(c, id, from, to): one UPDATE ... WHERE key = id AND state IN (from), false when no row matched because it is gone or another writer moved it first. It decides nothing: which move is legal, and who may make it, is the service's (layer 4). Underneath it is meta_store::transition, which also takes a transition's @condition (a meta_store::Condition, the CHECK subset: the six comparisons, the null tests, and, or, not and coalesce) and ANDs it into the same statement; a literal compared with a field is written in that field's stored form, so an int-backed enum's member is its integer.

c is a sqlx::PgConnection or a sqlx::SqliteConnection (a transaction derefs to one), through meta_store::Conn; the shared crate's postgres and sqlite features pick the engines. Rows come back as the model crate's own types, which stay free of sqlx. A subtype in a single-table hierarchy writes its discriminator itself and reads only its own rows. Field names in a filter or sort are the model's, checked at run time; which fields a caller may filter on is the transport's choice, not the store's.

use meta_store::{Filter, Query};
use shop_store::order;

let mut c = sqlx::PgConnection::connect(&url).await?;
let page = order::list(&mut c, &Query {
    filter: Some(Filter::from_json(&json!({ "status": { "eq": "NEW" } }))?),
    limit: Some(50),
    ..Query::default()
}).await?;
// When `status` has transitions, it is a lifecycle and gets its guarded update:
if !order::transition_status(&mut c, id, &[OrderStatus::New], OrderStatus::InFlight).await? {
    // someone else moved it first
}

A view decides its own column types (AVG of an integer is NUMERIC on Postgres), so a served read's numbers are read through a cast to their field's type, and a report's column is its derived field's name under the project's column strategy.

A many-to-many relationship of a keyed entity gets <relation>_of(c, id): the related rows, read through the junction in one statement, in no order. The junction and its two keys are the ones the loader's pairing rule derives (metaobjects::validate::relationships::derive_m2m_fields), so a relationship that loads is one the store can follow. A @symmetric self-join answers from both sides of a row, and a row relating a row to itself answers that row.

Not stored yet: a @storage: flattened field is refused at generation (ERR_UNSUPPORTED).

The store is held to the shared persistence corpus (fixtures/persistence-conformance): tests/persistence_conformance.rs generates the corpus model, starts a throwaway Postgres (docker run --rm on a tmpfs, removed when the test ends; without docker the test says so and passes), and runs every scenario through the generated functions, normalized as normalization.md says. All 56 run, guarded-update among them; a scenario this port could not run would go on the ledger in that file, which is empty and only shrinks. tests/repository.rs runs the same rows on SQLite in memory, and the guarded update on the rules model.

service: the operations, with the rules

service (ADR-0067 layer 4) writes a <package>_service crate per package with a stored entity. Its <Pkg>Service<D, A, P, B, H, E> is generic over the pool (D, a sqlx::PgPool or SqlitePool, through meta_service::Db) and the actor type (A, whatever the transport resolves a caller to), and is built from what the app writes:

  • P: <Pkg>Policy<A, C>: allows(permission, actor, row, input, conn) answers whether the actor may run a command, a transition or an operation; the permission is the rules crate's enum;
  • B: <Pkg>Bodies<A, C>: a method per declared operation. A command's body answers the move to make (<Op>Move, one variant per @transitions entry, or None) and its output; BodyError::Business answers one of the operation's declared errors;
  • H: <Pkg>Hooks<A, C> (default ()): before and after each derived create, update and delete, told which by <Pkg>Before and <Pkg>After (<Pkg>Before::CreateOrder(&mut input), UpdateOrder { patch, current }, DeleteOrder(&row)); a before hook may change the input or patch;
  • E: <Pkg>Events<A, C> (default ()): on_write, after every write, inside its transaction.

Each derived operation and transition command is a method: list_<entity> (page size 50 unless api.pagination says otherwise, and the total when the query asks with_count), get_<entity>, <relation>_of_<entity>, create_, update_, delete_, and <transition>_<entity>; a declared operation is <operation>. A write opens one transaction, or runs inside a caller's through its _in twin (create_order_in(&mut tx, ...)), and a refusal anywhere undoes it. The checks run in ADR-0066 §6's order: the row (404 not_found), validation (400), the permission point (403 forbidden), the state (409 invalid_transition), the body, the guarded update (409 invalid_transition, or transition_condition_failed when the row is in a legal state and the @condition failed) and the event hook. An update that changes a lifecycle field is 409 state_managed, and so is a create naming a state other than the initial one (unless rules.create allows any). An @autoSet timestamp is stamped by the service, whatever the caller sent (ADR-0045 Amendment 1). Every error is a ServiceError, whose code() and status() are the wire's.

tests/service.rs runs the rules corpus's scenarios (fixtures/api-contract-conformance/rules/) through the generated service on SQLite, with the README's policy, bodies, hook and audit event, and checks the settings variants in the generated text. The same scenarios run again over HTTP through the routes below.

service-routes: the service over HTTP, on axum

service-routes (ADR-0067 layer 5) writes a <package>_http crate per package with a service, and the shared meta_http crate. It is the one transport generator, and it is opt-in: it runs only when metaobjects.config.json lists it, which cargo meta init --transport does. Its router(service, actor) returns an axum::Router to mount under the API root (apiPrefix, say /api):

let app = axum::Router::new().nest("/api", tasks_http::router(
    TasksService::new(pool, Team),            // the generated service, with the app's policy
    meta_http::ActorFn(|headers: &HeaderMap| user(headers)),
));

actor is an ActorSource<A>: the app's own authentication, a function of the request's headers (ActorFn), or (), which reads no one. The routes hand the actor to the service untouched; they check nothing of the rules themselves. Each entity gets GET and POST on /<plural>, GET, PATCH, PUT and DELETE on /<plural>/{id}, GET /<plural>/{id}/<relation> for each traversal, POST /<plural>/{id}/<transition> for each transition command, and each declared operation its binding.rest route. An entity that cannot be written answers its write verbs with 405 method_not_allowed.

The list query is the REST standard every port serves: filter[<field>][<op>]= with the nine operators and or/and groups, search=<term> over the search fields, sort=<field>:asc|desc, limit and offset (page size 50 unless api.pagination says otherwise; a limit over its maxLimit is a 400 limit_too_large), and withCount=true for the total. The allowlist is the model's: a field is filterable with @filterable, sortable with @sortable (or, with no @sortable, when it is filterable), and a field with a password view is neither. Every refusal is a named 400 (invalid_filter_field, invalid_sort, pagination.invalid_value, and the rest of fixtures/list-query-conformance/). A body is checked before the service sees it: invalid_json, unknown_fields (unless api.unknownFields is drop), validation with every issue, and constraint_violation for a unique key the store refused. A credential field (one with a password view) is never written or returned by a route: a body key naming it is ignored, and no response carries it.

An entity whose source has a read-only view beside its table (source.rdb @kind: view in the read role) is written to the table and read back through the view, so the fields that view derives (origin.*) are in every answer, a write's included.

requirements and agent-docs: the requirement and agent pages

requirements writes the requirement ledger as requirements.toon, for tools; agent-docs writes agent/requirements.md (the node index and the embedded ledger) and the model-only agent/schema.md (tables, views, relationships and enums, without the SQL types, defaults, indexes and checks only the TypeScript migration engine knows; the table, view and relationships of a reference node, or of a dependency's object that scope.include does not name, are left out, as not this run's to generate). Both read the docs view alone and render the shared templates/requirements/ and templates/agent/ Mustache pages, so the bytes are every port's (fixtures/docs-page-conformance/, run by tests/docs_page_conformance.rs). A same-path file under the project's templates/ wins. No requirement.* node, no requirement page; no table and no view, no schema page. The schema page says to change the schema with meta migrate when .metaobjects/config.json sets migrate.dialect. Nobody reads a human requirements.md any more: the explorer shows the ledger. agent/ui.md stays a TypeScript page.

What is absent here: there is no requirement gate in cargo meta verify, and these generators do not yet apply the requirement ledger (dependencies::requirement_ledger), the RS-V2 work. So the pages list every requirement the view carries, an imported package's included, where other ports list only those the ledger counts. The always-on agent file comes from cargo meta agent-docs (no Node), which names these pages once a gen has written them.

shared-model: publishing a shared model

shared-model (FR-023) publishes a slice of your model so another repository can depend on it. It writes two files at the root of the output directory: <name>.metaobjects.json, one canonical-JSON metadata.root document holding the selected top-level nodes, and metaobjects.pkg.json, the manifest a consumer's meta deps sync reads (name, version, metamodelVersion, the artifact's file name and sha256, and the sorted packages and nodes). It is opt-in, and runs from its entry with options:

{ "generators": [{ "name": "shared-model", "options": { "name": "acme-common", "include": ["acme::common::**"], "exclude": ["acme::common::Secret"] } }] }

name (the dependency key a consumer syncs under) and include (scope patterns of the top-level nodes to publish) are required. exclude applies after include; requirements: true also publishes the selected requirement.* nodes; files names which of the project's own metadata files may contribute, relative to the project root (default: all of them); version is the published version, by default the nearest Cargo.toml [package] (or [workspace.package]) or package.json version at or above the project root; a [package] that inherits its version (version.workspace = true or version = { workspace = true }) publishes the nearest [workspace.package] version and nothing else. template.* nodes are never published. The files load on their own, as a consumer loads the artifact, and a selection that is empty, not closed over extends, reference attributes and requirement claims, or that needs vocabulary the core providers do not register is refused with the reason (ERR_SHARED_MODEL, naming each referrer -> target pair), and nothing is written. The bytes of the files it reads are part of the skip key, since the artifact is serialized from what each file declares, not from the effective model. A run that infers its version is never skipped, because the manifest it reads can lie outside the project.

The bytes are every port's: the selection, the closure check and both files come from metaobjects::shared_model (build_shared_model, render_shared_model_files), held to fixtures/shared-model-conformance/. cargo meta eject shared-model copies the generator, which decides only which files contribute and where the version comes from; the engine stays in the crate.

docs-site: the model explorer

The model explorer (Explorer site), written with no Node: the generator builds the docs view from the model this run loaded, and writes it with the explorer app under site/ in the output directory:

generated/site/
  index.html, assets/          the app: the same bytes in every port (the repository's explorer-app/)
  explorer.json                the docs view, byte for byte what every port writes for the model
  explorer-model.js            the docs view, the prompt texts, the title and the footer, as one script
  explorer-stamp.js            the model's hash, which the open app polls to reload itself

List it with its options:

{ "generators": ["entity", "client", { "name": "docs-site", "options": { "prompts": "prompts" } }] }
Option Default What it does
subDir site The directory under the output it writes to; "" writes at the output root
title the project directory's name The header and the home page title
stamp none Footer text, such as a version; none keeps the site a function of the model alone
prompts none A directory, relative to the project, of prompt .mustache sources outside the metadata directories and templates/ (those two are always searched)
verifyFile none A verify --format json payload whose requirement rows the explorer shows. cargo meta verify has no requirement gate, so write it with another port's verify
routesFile none The routes the application mounts, [{ node, method, path }] with node a resolution key

Each node's page lists the files generated for it, read from the provenance the generators record in the hash manifest (generatedBy and sourceNode on each file written for one node; a shared crate's files stay a plain hash): the files this run wrote, and what the committed manifest records outside the directories the run clears. The engine runs the generator after every other one, so cargo meta verify renders the same lists the write run wrote.

The site builder is engine, not a reference generator: cargo meta eject docs-site refuses, and gen --list marks it [package-only]. What a project owns is the theme. Files in codegen/docs-site/ replace the shipped ones: assets/explorer.css, assets/explorer.js, and templates/explorer-*.mustache, which the app uses in place of its own. Editing one regenerates.

To read the docs view from the running application instead, mount GET /_meta/docs (Metadata API) and open index.html?source=<url>.

Owning a generator

A generator is a reference helper (ADR-0034): copy it and change the copy. Rust is compiled, so an owned generator cannot be loaded from source at run time. eject therefore writes a small crate the project owns:

codegen/
  Cargo.toml             depends on the engine; written once, then yours
  main.rs                the generator list; written once, then yours
  generators/<name>.rs   one owned copy per ejected generator

While that crate exists, cargo meta gen and cargo meta verify build and run it, so there is still one way to generate. A copy is the shipped source byte for byte, and it imports only the engine surface (metaobjects_codegen::{emitted, generator, names, plan, writer}); the shared text a generator writes (the transport, the command helpers, the store, the service and HTTP mechanics) comes from the engine's emitted module, so an owned copy that wants it different edits the function that writes that crate (transport_crate in an owned client) or overrides that piece. Every file in codegen/ is an input of the skip key, whether eject wrote it or you added it (a module beside the generator list, say), so editing any of them regenerates. Only the crate's build directory (codegen/target) and its Cargo.lock are left out. A crate it depends on by path (a linked checkout of a pack's generators, say), in a build table or a [patch], is an input too, followed through that crate's own path dependencies and the workspace it inherits from. The metaobjects and metaobjects-codegen engine crates are left out, because the engine's own hash already covers their source; a path dependency on metaobjects-render is hashed like any other.

An owned generator need not be a copy. Each code generator that writes crates has a parts trait (EntityParts, ClientParts, CommandsParts, RenderHelperParts, OutputParserParts, OutputPromptParts, RulesNamesParts, ContractTypesParts, RepositoryParts, ServiceParts and ServiceRoutesParts) with one default method per piece of its output (a crate's manifest, its lib.rs, one entity's file, one item). A generator of your own implements it, overrides the piece it changes and returns self.render(ctx) from generate; every other file is the shipped generator's, byte for byte. An override can call the shipped piece, a public function of the same module and name. The traits carry the same no-stability caveat as the rest of the engine API (ADR-0063 item 7).

cargo meta gen --list shows each owned copy as identical, or as DIFFERS with how many shipped lines it lacks and how many are its own. cargo meta eject --undo removes copies that are still identical (--force discards an edited one) and tells you the two lines of codegen/main.rs to change; it never edits that file.

Nothing discovers or loads a third-party generator: the owned crate is the supported way to add your team's own generator. Write it against metaobjects-codegen's authoring surface (the generator, emitted, plan, writer and names modules) and add it to the generator list. That surface is versioned with the crate under the compatibility policy; the rest of the crate is internal and may change in any release (ADR-0065 Amendment 5). An owned copy is pinned to the exact engine its Cargo.toml names.

The owned crate's Cargo.toml names no machine. By default the three engine crates are the crates.io release the tool is, pinned exactly (metaobjects = "=<version>"), since an owned copy is that release's source. Until a release exists, or to build against your own changes, eject with --engine <directory | git-url[#rev]>: a directory is a checkout's server/rust (relative to the project root), and a git URL names a repository and an optional revision.

Conformance

The port runs the shared corpora below. Five of them keep a ledger: a fixture that fails and is not on the port's ledger prints a warning and does not fail the rust lane until ADR-0065 turns the warning off; a fixture that passed and regresses still fails it, because each ledger records what the port passes today — a minPassing count, or (the render corpus) the cases that must still differ — and may only shrink. The others have no ledger: each case asserts its result outright.

Corpus Rust today
registry-conformance byte-identical manifest
conformance (the metamodel corpus) 599 of 599, and the ledger is empty
yaml-conformance all 19, and the ledger is empty
source-resolution-conformance all 30 cases
provider-composition-conformance all 18 cases (11 error-shape, 4 compose-load, 3 attr-ownership): the provider error codes and ERR_REGISTRY_SEALED through providers::compose_registry, a consumer provider's extend of a core subtype surviving a strict load, and the owner TypeRegistry::attr_owner and common_attr_owner report for each attribute (ADR-0062), from the corpus's canonical named providers (metaobjects/tests/provider_composition_conformance.rs)
reference-source-conformance all 6 cases: a reference: true source loads so references resolve against it, a node declared only in reference files is a reference node no generator emits, and a template.* declared only there is refused (ERR_REFERENCE_SOURCE_TEMPLATE)
dependency-conformance the resolution arm, all 36 cases: a declared dependency loads from its committed snapshot (.metaobjects/deps/<name>/), verified against .metaobjects/deps.lock.json (a stale, missing or mismatched snapshot is ERR_DEPENDENCY_SNAPSHOT_STALE), its objects are left out of generation unless scope.include names their package, and a new node declared into its package is refused (ERR_DEPENDENCY_PACKAGE_NOT_OWNED). The 7 ledger cases are asserted through dependencies::requirement_ledger, since the port has no requirement gate yet. The check arm, all 11 cases, through dependency_check::check_dependencies, the engine behind verify --deps
scope-conformance all 10 cases, through metaobjects::scope, the matcher migrate.scope is read with
shared-model-conformance all 15 cases (9 publish, 6 refusal), through metaobjects::shared_model, the engine the shared-model generator calls (metaobjects/tests/shared_model_conformance.rs)
naming-conformance all of it
codegen-skip-conformance every case the port can meet, on both columns (what gen must do and what a local verify --codegen may do), including an edited owned generator and a forced CI verify (ADR-0064). The one it cannot meet, about the three-way merge, is named with its reason in the runner; the template cases (a body edited, a body added) run.
rules-check-conformance all 16 cases: the diagnostics (severity, code, node address, message text) and the summary of the verify business-rules gate, under each case's rules and verify settings
validation-conformance every case, through the generated validate(): each payload is read into the generated create body by a probe the real compiler builds
decimal-wire, duration-wire every vector, against the code the support crate carries
writer-conformance every core and rust case of the code writer (ADR-0063); each rust case is a rustfmt fixed point
template-output-render-conformance the welcome page and email, the nested payload and the cross-package payloads, rendered through generated render-helper crates that a probe builds; the drift cases fail gen, and a body edited after generation fails the render
verify-conformance all 45 cases, through metaobjects_render::verify, the check cargo meta verify --templates runs, with no ledgered case and no Rust expectation file
generator-registry-conformance the shipped generators are exactly the registry's rust entries, with its tier and layer, and the eject contract's census, completeness and depth hold (ADR-0034 Amendment 8)
render-conformance every case, byte-identical through metaobjects_render::render (the crate's own Mustache engine); the drift ledger is empty, and a case added to it must still differ
response-verify-conformance all 7 cases: the reply check the template gate adds — ERR_RESPONSE_UNRESOLVED, and, for an xml prompt, ERR_RESPONSE_TAG_UNDECLARED
extract-conformance all 58 cases, through metaobjects_render::extract::extract
output-prompt-conformance all 17 cases in all three @promptStyle styles, with the extract round trip, through metaobjects_render::prompt::render_output_format

Three gates have no corpus of their own:

  • The codegen-compile gate. The small two-package model and the public fitness model are generated by every shipped generator, each into a scratch Cargo workspace that cargo check builds and rustfmt --check reads file by file. A probe then uses the generated types and drives the generated client and commands over HTTP against a local server that records each request. The workspace resolves from a committed lockfile (metaobjects-codegen/tests/gen-check.lock), and the gate fails when cargo needed a package that lockfile does not name.
  • The scale gate. Twenty copies of the fitness model, each its own package: the output must be exactly twenty times one copy's files and no more than twenty times its bytes, one copy's output has a byte budget and a longest-file budget, and all hundred and twenty-three crates must build. The times are printed; their ceilings are set far above a normal run and catch a step that stopped scaling, not a slow machine.
  • The layout ratchet (ADR-0063 item 6, metaobjects-codegen/tests/layout_ratchet.rs). It parses every source file under metaobjects-codegen/src/ except the writer, which covers the generators and the fixed text in emitted/, and it fails on a line break or an indentation run typed outside the writer. tests/layout_ratchet/allowlist.json names the files that write something other than generated code (terminal messages, hash input, JSON), each with a reason. The list only shrinks: the test compares it with the copy at the branch's merge base.

The API corpora run too, with no ledger in the list-query one:

Corpus Rust today
persistence-conformance all 56 scenarios, through the generated repository crates (see repository)
list-query-conformance all 95 cases, through the list parser meta_http ships, with the allowlist derived from the fixture's model as the generator derives it (tests/list_query_conformance.rs)
api-contract-conformance the generated lane: each sub-corpus's model generated into a project, its routes served on SQLite and every scenario sent over HTTP (tests/api_contract.rs). core 34 scenarios, write-through 2, secret-fields 3, list-query 21, rules 14 and one in each of its three settings variants: 77 scenarios, 151 requests. Its ledger lists the sub-corpora not built yet (see What does not exist yet), and a sub-corpus neither run nor ledgered fails the test. There is no hand-rolled reference-server lane: the generated lane is the one that tests a generator.

Load benchmark

load_bench (a Cargo example) times loading a 20-times model built in memory from the public conformance fitness model, per load phase and per validation pass, with the peak memory of the process. Its invocations, the baseline options and the committed baseline are documented in scripts/bench-load/README.md.

scripts/bench-load/ runs this same model through every port, stage by stage (load and validate, a no-change generation run, a full generation, a one-entity-edit regeneration, peak memory), so a port's speed can be tracked per feature next to the others.

What does not exist yet

  • Other generators. The documentation generators are metamodel-docs and docs-site: there is no per-entity reference page generator (docs, api-docs) and no static export of the explorer. Projections and reports get no route, client or commands yet, and a template over a sourceless object.projection payload gets no render function.
  • The rest of the API-contract corpus. The transport passes core, write-through, secret-fields, list-query and rules with its settings variants. These sub-corpora are not built yet and are on the runner's ledger (metaobjects-codegen/tests/api_contract.rs): tph and secret-fields/tph (single-table inheritance), m2m (many-to-many traversal), jsonb (object fields stored as JSON), projection, report, decimal, duration, api-settings and enum-default.
  • Other gates. verify --codegen, verify --templates, verify --deps and the business-rules gate are the ones it has. There is no requirement gate and no fmt.
  • A three-way merge. A hand-edited generated file is refused by name, as in every port but TypeScript; change the model or own the generator instead.
  • A metadata-driven runtime. No ObjectManager and no web adapter over it: data access is the generated store crates, the generated service crates run the rules over them, and the generated service-routes crates serve them.
  • Publication. The crates are publishable and the lane proves they package (cargo publish --dry-run), but nothing is published: there is no crates.io release yet, and publishing waits for the owner's go (ADR-0065 Amendment 3).

A note on the name cargo-meta. The crates.io name cargo-meta is held by an unrelated crate, so the CLI package is metaobjects-cli and installs a cargo-meta binary: the command you type is still cargo meta. Nothing is reserved on crates.io; reserving a name is a publish, and waits for the owner's go.

The schema stays where it is for every port: migrations run in the project's own tool or the Node meta migrate reference helper, and database checks in the Node meta (ADR-0015; What differs by language).

Build and test

You need rustup. The workspace pins its toolchain in server/rust/rust-toolchain.toml (1.94.1, with rustfmt and clippy), so cargo picks it up on its own.

cd server/rust
cargo build
cargo test --locked                        # the corpora, the unit tests and the codegen gates (these run cargo check on generated crates)
cargo fmt --all --check
cargo clippy --locked --all-targets -- -D warnings

The codegen gates build generated crates under server/rust/target/gen-check/ and write their scratch projects under $DEV_SCRATCH (the system temp directory when it is unset), removing them afterwards. The first run downloads and builds reqwest, tokio and clap once. When the generated crates' dependencies change, refresh the gate's lockfile deliberately: UPDATE_GEN_LOCK=1 cargo test -p metaobjects-codegen --test compile generated_crates.

The repository's one pre-PR script runs the same steps, plus the load_bench and gen_bench tables (printed, never gated on a number):

scripts/ci-local.sh --only rust

A change under server/rust/ selects the rust lane in scripts/ci-local.sh --affected, the PR checks and the local CI. Raise the pinned toolchain deliberately, in its own commit.

Use the crate from a checkout, as a path dependency:

[dependencies]
metaobjects = { path = "<repo-root>/server/rust/metaobjects" }
use metaobjects::collection::resolve_collection;
use metaobjects::loader::Loader;
use metaobjects::naming::NamingRegistry;
use metaobjects::providers::default_registry;

let registry = default_registry()?;
let collection = resolve_collection(project_dir, None, &NamingRegistry::builtin())?;
let (sources, read_errors) = collection.read_sources();
let result = Loader::new(&registry).load_sources(&sources);
assert!(read_errors.is_empty() && result.errors.is_empty());

The API carries no stability promise and may change; read server/rust/metaobjects/tests/project_load.rs for a working call.

A whole service: examples/rust-service/

examples/rust-service/ is the port's stand-in estate: a tasks API with a lifecycle, whose model, store, rules, service and axum routes are generated from one YAML file, and whose hand-written part is the policy, the caller and the table. The rust lane checks that its committed generated/ crates are what gen writes today (verify --full) and runs its HTTP test.

A worked codegen exercise: examples/tasks-axi-rust/

examples/tasks-axi-rust/ is an experiment, not a product, and it is not the port's generator (that is cargo meta gen, above; it predates the commands generator). It reimplements the read path of an agent-facing task tracker as a small Rust binary and generates the Rust for its rows, enums, error codes, argument parser, help text and dispatcher from a MetaObjects model, with an owned TypeScript generator (ADR-0034). The real tool is the oracle: for every implemented command and flag the two print the same bytes and exit with the same code.

It changes nothing in MetaObjects. Its committed generated crate is the set of golden outputs a Rust-native generator would have to reproduce, and its README records what the model had to carry. Read its README, not this page, for what it does and does not do.

See also