openapi-nexus 0.1.3

OpenAPI 3.x multi-language code generator
Documentation
//! Shared project file helpers for all Rust generators.

use crate::codegen::traits::file_writer::FileInfo;
use crate::ir::types::IrInfo;
use heck::ToKebabCase as _;

/// Render the standard file header comment block.
pub fn render_file_header(info: &IrInfo) -> String {
    let mut out = String::new();
    out.push_str("// @generated\n");
    out.push_str("// Code generated by openapi-nexus. DO NOT EDIT.\n");
    out.push_str("//\n");
    out.push_str(&format!("// {}{}\n", info.title, info.version));
    if let Some(desc) = &info.description {
        for line in desc.lines() {
            out.push_str(&format!("// {line}\n"));
        }
    }
    out.push('\n');
    out
}

/// Generate `lib.rs` with standard module re-exports.
pub fn lib_rs_file(header: &str) -> FileInfo {
    let content = format!(
        "{header}#![allow(clippy::all)]\n\npub mod apis;\npub mod models;\npub mod runtime;\n"
    );
    FileInfo::project("src/lib.rs".to_string(), content)
}

/// Generate `README.md` from IR info.
pub fn readme_file(info: &IrInfo) -> FileInfo {
    let title = &info.title;
    let version = &info.version;
    let description = info
        .description
        .clone()
        .unwrap_or_else(|| "Generated Rust SDK.".to_string());
    let crate_name = info.title.to_kebab_case();
    let content = format!(
        "# {title}\n\n{description}\n\nVersion: `{version}`\n\nGenerated by [openapi-nexus](https://github.com/adamcavendish/openapi-nexus) for `{crate_name}`.\n"
    );
    FileInfo::readme("README.md".to_string(), content)
}

/// Prepend the file header to a body string.
pub fn with_header(header: &str, body: &str) -> String {
    let mut out = String::with_capacity(header.len() + body.len());
    out.push_str(header);
    out.push_str(body);
    out
}