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§
- Check
Report - What a
check()found, path by path, relative to the output directory. - Config
- What generation consumes. Fully resolved: no
Optionwhere the generator needs an answer, noDeserialize, noDefault— obtained only throughConfigFile::build. - Config
File - 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.
- Field
Config - Per-field settings, addressed by the property name as written in the description.
- Generated
File - One generated file, relative to the output directory the caller
chooses. Generating writes no output files;
writedoes. - Generation
- The result of a generation: files in memory, everything the generator had to say, and the dependency list.
- GoConfig
- Go settings, resolved.
- GoConfig
File - Go settings as a file may state them.
- GoDependency
- A Go module the generated code imports from, beyond the standard library.
- Rust
Config - Rust settings, resolved.
- Rust
Config File - Rust settings as a file may state them.
- Rust
Dependency - A Rust dependency the generated code needs.
- Schema
Id - 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.
- Source
Document - A description, parsed and normalized to OpenAPI 3.2, together with the source it came from.
- Source
Validation Error - The document failed
roasvalidation. 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. - Template
Context - The context the templates receive, and what the generator said while
building it — a type an
Errorsuppressed is missing fromcontext. - Type
Config - Per-type settings, addressed by the component name as written in the description.
- Type
Spelling - How a substituted type is spelled in one language.
- Validated
Source Document - A
SourceDocumentthat passedvalidate. - Write
Options - How a
write()treats the output directory. - Write
Report - What a
write()did, path by path, relative to the output directory.
Enums§
- Config
Error - Why a configuration could not be loaded or resolved.
- Diagnostic
Kind - 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.
- Generate
Error - 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.
- Source
Error - Why a description could not become a
SourceDocument. - Source
Version - The OpenAPI version a description was written in.
- Target
- The language to generate.
- Write
Error - 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_dirwith whatwrite()would leave there, without writing anything: file contents, owned paths and the manifest itself. It applies the same refusals aswrite()— error diagnostics withoutallow_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
roasvalidation 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 onego buildwould 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 asrust.moduleunless a substitution imports or names a crate of that name, whichgeneraterefuses. - scaffold
- A template directory that changes nothing, to show what can be
changed: for each default template rendering under
config(by itstarget,layoutandvalidation), 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 theroas-templates.tomlmanifest. Used astemplate_dirit renders exactly what the defaults do. It describes the defaults, whatevertemplate_dirsays; it is whatroas openapi generate --dump-scaffoldwrites. - template_
context - The exact context the templates receive. For writing templates; it is
what
roas openapi generate --print-contextprints. - validate
- Validate a document with
roas, skipping external references. - write
- Write a generation into
out_dir.