Skip to main content

satay_codegen/
lib.rs

1#![forbid(unsafe_code)]
2
3mod error;
4mod ident;
5mod model;
6mod parse;
7mod render;
8
9pub use error::{Error, ParseError, ValidationError};
10pub use render::GeneratedFile;
11use tracing::info;
12
13/// Which root module file to emit at the output directory root.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub enum RootModule {
16    /// `mod.rs` (default).
17    #[default]
18    ModRs,
19    /// `lib.rs` for a generated crate root.
20    LibRs,
21}
22
23/// Options for code generation.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub struct GenerateOptions {
26    /// Root module filename (`mod.rs` or `lib.rs`).
27    pub root_module: RootModule,
28}
29
30#[tracing::instrument(err)]
31pub fn generate(spec: &str) -> Result<Vec<GeneratedFile>, Error> {
32    generate_with(spec, GenerateOptions::default())
33}
34
35#[tracing::instrument(err)]
36pub fn generate_with(spec: &str, options: GenerateOptions) -> Result<Vec<GeneratedFile>, Error> {
37    info!("parsing OpenAPI document");
38    let document = parse::parse_document(spec)?;
39    let api = parse::parse_api(&document)?;
40    info!(
41        components = api.components.len(),
42        operations = api.operations.len(),
43        "parsed API"
44    );
45    Ok(render::render_api(&api, options))
46}