Skip to main content

Crate roas_codegen

Crate roas_codegen 

Source
Expand description

Generate Rust and Go types from OpenAPI descriptions.

The pipeline is: detect the version, parse the typed model, normalize everything to OpenAPI 3.2 while recording what that loses, validate, lower every schema to a representation where the hard decisions are already made, and render. SourceDocument::parse and validate turn bytes into a ValidatedSourceDocument; generate turns that and a Config into a Generation: files in memory, every diagnostic, and the dependency list the generated code needs, in Rust or in Go.

use roas_codegen::{
    ConfigFile, Input, SourceDocument, Target, generate, generation_options, validate,
};
use url::Url;

let bytes = std::fs::read("openapi.json")?;
let document = SourceDocument::parse(Input::Json(bytes), Url::parse("file:///openapi.json")?)?;
let document = validate(document, generation_options())?;
let mut file = ConfigFile::default();
file.target = Some(Target::Rust);
let config = file.build()?;
let generation = generate(&document, &config)?;
for file in &generation.files {
    println!("{}: {} bytes", file.path.display(), file.contents.len());
}
for diagnostic in &generation.diagnostics {
    eprintln!("{diagnostic}");
}

Structs§

CheckReport
What a check() found, path by path, relative to the output directory.
Config
What generation consumes. Fully resolved: no Option where the generator needs an answer, no Deserialize, no Default — obtained only through ConfigFile::build.
ConfigFile
What a configuration file may say. Everything optional; nothing validated beyond the shape.
Dependencies
What the generated code depends on, computed from what was emitted. Nothing generated depends on roas.
Diagnostic
One thing the generator has to say about the description.
FieldConfig
Per-field settings, addressed by the property name as written in the description.
GeneratedFile
One generated file, relative to the output directory the caller chooses. Generating writes no output files; write does.
Generation
The result of a generation: files in memory, everything the generator had to say, and the dependency list.
GoConfig
Go settings, resolved.
GoConfigFile
Go settings as a file may state them.
GoDependency
A Go module the generated code imports from, beyond the standard library.
RustConfig
Rust settings, resolved.
RustConfigFile
Rust settings as a file may state them.
RustDependency
A Rust dependency the generated code needs.
SchemaId
The identity of a schema, or of any other location in a description: the document’s URI plus a JSON Pointer (RFC 6901) into it.
SourceDocument
A description, parsed and normalized to OpenAPI 3.2, together with the source it came from.
SourceValidationError
The document failed roas validation. The document is returned, not consumed, so it can be inspected or validated again with other options. Boxed, since the whole 3.2 model rides inside.
Substitution
A user-provided type standing in for a schema or a format.
TemplateContext
The context the templates receive, and what the generator said while building it — a type an Error suppressed is missing from context.
TypeConfig
Per-type settings, addressed by the component name as written in the description.
TypeSpelling
How a substituted type is spelled in one language.
ValidatedSourceDocument
A SourceDocument that passed validate.
WriteOptions
How a write() treats the output directory.
WriteReport
What a write() did, path by path, relative to the output directory.

Enums§

ConfigError
Why a configuration could not be loaded or resolved.
DiagnosticKind
What kind of thing a diagnostic reports. The message says it in words; the kind lets a caller act on it without parsing them.
Fidelity
How faithfully the numbers in a document reflect what was written.
GenerateError
Generation itself did not complete. Distinct from a diagnostic: a diagnostic is an answerable fact about the description, with partial output; this is about the configuration, the templates or the toolchain, and no output is meaningful.
Input
The bytes or value a description arrives as.
Layout
How generated types are spread over files.
Severity
How much a diagnostic matters.
SourceError
Why a description could not become a SourceDocument.
SourceVersion
The OpenAPI version a description was written in.
Target
The language to generate.
WriteError
Why the output directory could not be written or checked.

Constants§

TEMPLATE_API
The version of the view model and block contract templates are written against. A user’s template directory declares it.
TEMPLATE_MANIFEST
The file in a template directory that declares its template_api.

Functions§

check
Compare out_dir with what write() would leave there, without writing anything: file contents, owned paths and the manifest itself. It applies the same refusals as write() — error diagnostics without allow_partial, symbolic links, non-directories, paths another target owns, unowned files in the way — so a directory reported out of date is one a write could update.
generate
Generate from a validated document.
generation_options
The roas validation checks that say nothing about the schemas being generated: unused components and path items, undeclared tags, empty titles, versions, response descriptions and external-documentation URLs, duplicate operation ids, malformed URLs. Generation starts from this set; a caller adds to it.
go_file_name
A file stem derived from free text such as a label: lowercase, other characters to _, never empty, never digit-led, and never one go build would constrain.
manifest_name
The manifest’s file name for a target: .roas-codegen-<label>-<language>.json.
render
The files as the templates render them, before the syntax check and the formatter: what to look at when a template produces something the formatter refuses.
rust_module_name
A Rust module name derived from free text such as a label or a file name: lowercase, other characters to _, never empty, never digit-led, never a keyword. Accepted as rust.module unless a substitution imports or names a crate of that name, which generate refuses.
scaffold
A template directory that changes nothing, to show what can be changed: for each default template rendering under config (by its target, layout and validation), and each one it includes, a template at the path an override takes, extending the default and filling every block with the default’s content, plus the roas-templates.toml manifest. Used as template_dir it renders exactly what the defaults do. It describes the defaults, whatever template_dir says; it is what roas openapi generate --dump-scaffold writes.
template_context
The exact context the templates receive. For writing templates; it is what roas openapi generate --print-context prints.
validate
Validate a document with roas, skipping external references.
write
Write a generation into out_dir.