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("// Code generated by openapi-nexus. DO NOT EDIT.\n");
11    out.push_str("//\n");
12    out.push_str(&format!("// {} — {}\n", info.title, info.version));
13    if let Some(desc) = &info.description {
14        for line in desc.lines() {
15            out.push_str(&format!("// {line}\n"));
16        }
17    }
18    out.push('\n');
19    out
20}
21
22/// Generate `lib.rs` with standard module re-exports.
23pub fn lib_rs_file(header: &str) -> FileInfo {
24    let content = format!("{header}pub mod apis;\npub mod models;\npub mod runtime;\n");
25    FileInfo::project("src/lib.rs".to_string(), content)
26}
27
28/// Generate `README.md` from IR info.
29pub fn readme_file(info: &IrInfo) -> FileInfo {
30    let title = &info.title;
31    let version = &info.version;
32    let description = info
33        .description
34        .clone()
35        .unwrap_or_else(|| "Generated Rust SDK.".to_string());
36    let crate_name = info.title.to_kebab_case();
37    let content = format!(
38        "# {title}\n\n{description}\n\nVersion: `{version}`\n\nGenerated by [openapi-nexus](https://github.com/adamcavendish/openapi-nexus) for `{crate_name}`.\n"
39    );
40    FileInfo::readme("README.md".to_string(), content)
41}
42
43/// Prepend the file header to a body string.
44pub fn with_header(header: &str, body: &str) -> String {
45    let mut out = String::with_capacity(header.len() + body.len());
46    out.push_str(header);
47    out.push_str(body);
48    out
49}