Skip to main content

openapi_nexus/generators/rust/aioduct/
runtime.rs

1//! Hardcoded Rust runtime source files for aioduct.
2
3use crate::codegen::traits::file_writer::FileInfo;
4use crate::generators::rust::aioduct::config::{AioductFeatureConfig, AioductTls};
5use crate::generators::rust::common::project_files::with_header;
6use crate::generators::rust::common::runtime::render_api_call_error;
7use sigil_stitch::type_name::TypeName;
8
9const CLIENT_RS_TEMPLATE: &str = include_str!("runtime/client.rs.txt");
10const ERROR_RS: &str = include_str!("runtime/error.rs.txt");
11const AUTH_RS: &str = include_str!("runtime/auth.rs.txt");
12const MOD_RS: &str = include_str!("runtime/mod.rs.txt");
13const UPLOAD_FILE_RS: &str = include_str!("runtime/upload_file.rs.txt");
14
15/// Returns runtime files ready to write.
16pub fn runtime_files(
17    header: &str,
18    aioduct_cfg: &AioductFeatureConfig,
19    include_api_call_error: bool,
20    include_upload_file: bool,
21) -> Vec<FileInfo> {
22    let client_rs = render_client_rs(aioduct_cfg);
23    let mut mod_rs = MOD_RS.to_string();
24    let mut error_rs = ERROR_RS.to_string();
25    if include_api_call_error {
26        error_rs.push('\n');
27        error_rs.push_str(
28            &render_api_call_error(TypeName::qualified("aioduct", "HeaderMap"))
29                .expect("aioduct ApiCallError runtime renders"),
30        );
31    }
32    let mut files = vec![
33        FileInfo::runtime("client.rs".to_string(), with_header(header, &client_rs)),
34        FileInfo::runtime("error.rs".to_string(), with_header(header, &error_rs)),
35        FileInfo::runtime("auth.rs".to_string(), with_header(header, AUTH_RS)),
36    ];
37    if include_upload_file {
38        mod_rs.push_str(
39            "mod upload_file;\npub use upload_file::{multipart_header_value, UploadFile};\n",
40        );
41        files.push(FileInfo::runtime(
42            "upload_file.rs".to_string(),
43            with_header(header, UPLOAD_FILE_RS),
44        ));
45    }
46    files.push(FileInfo::runtime(
47        "mod.rs".to_string(),
48        with_header(header, &mod_rs),
49    ));
50    files
51}
52
53fn render_client_rs(cfg: &AioductFeatureConfig) -> String {
54    let has_http3 = cfg
55        .features
56        .as_ref()
57        .is_some_and(|f| f.contains(&"http3".to_string()));
58    let tls = cfg.tls.as_ref().unwrap_or(&AioductTls::RustlsRing);
59
60    let constructor = match (tls, has_http3) {
61        (AioductTls::RustlsRing | AioductTls::RustlsAwsLcRs, true) => {
62            "aioduct::HttpEngineSend::<R, C>::with_http3().expect(\"aioduct HTTP/3 client build\")"
63        }
64        (AioductTls::RustlsRing | AioductTls::RustlsAwsLcRs, false) => {
65            "aioduct::HttpEngineSend::<R, C>::with_rustls()"
66        }
67        (AioductTls::Disabled, _) => "aioduct::HttpEngineSend::<R, C>::new()",
68    };
69
70    CLIENT_RS_TEMPLATE.replace("{{CLIENT_CONSTRUCTOR}}", constructor)
71}