Skip to main content

openapi_nexus/generators/rust/common/
project_files.rs

1//! Shared project file helpers for all Rust generators.
2
3use crate::codegen::traits::file_writer::FileInfo;
4use crate::ir::types::IrInfo;
5use heck::ToKebabCase as _;
6
7/// Render the standard file header comment block.
8pub fn render_file_header(info: &IrInfo) -> String {
9    let mut out = String::new();
10    out.push_str("// @generated\n");
11    out.push_str("// Code generated by openapi-nexus. DO NOT EDIT.\n");
12    out.push_str("//\n");
13    out.push_str(&format!("// {} — {}\n", info.title, info.version));
14    if let Some(desc) = &info.description {
15        for line in desc.lines() {
16            out.push_str(&format!("// {line}\n"));
17        }
18    }
19    out.push('\n');
20    out
21}
22
23/// Generate `lib.rs` with standard module re-exports.
24pub fn lib_rs_file(header: &str) -> FileInfo {
25    let content = format!(
26        "{header}#![allow(clippy::all)]\n\npub mod apis;\npub mod models;\npub mod runtime;\n"
27    );
28    FileInfo::project("src/lib.rs".to_string(), content)
29}
30
31/// Generate `README.md` from IR info.
32pub fn readme_file(info: &IrInfo) -> FileInfo {
33    let title = &info.title;
34    let version = &info.version;
35    let description = info
36        .description
37        .clone()
38        .unwrap_or_else(|| "Generated Rust SDK.".to_string());
39    let crate_name = info.title.to_kebab_case();
40    let content = format!(
41        "# {title}\n\n{description}\n\nVersion: `{version}`\n\nGenerated by [openapi-nexus](https://github.com/rust-codegen-group/openapi-nexus) for `{crate_name}`.\n"
42    );
43    FileInfo::readme("README.md".to_string(), content)
44}
45
46/// Prepend the file header to a body string.
47pub fn with_header(header: &str, body: &str) -> String {
48    let mut out = String::with_capacity(header.len() + body.len());
49    out.push_str(header);
50    out.push_str(body);
51    out
52}