Skip to main content

openapi_nexus/generators/rust/reqwest/
runtime.rs

1//! Hardcoded Rust runtime source files.
2//!
3//! The runtime provides a reqwest-based `Client`, `Error` enum, and `Auth`
4//! trait. These ship as-is in every generated SDK.
5
6use crate::codegen::traits::file_writer::FileInfo;
7
8const CLIENT_RS: &str = include_str!("runtime/client.rs.txt");
9const ERROR_RS: &str = include_str!("runtime/error.rs.txt");
10const AUTH_RS: &str = include_str!("runtime/auth.rs.txt");
11const MOD_RS: &str = include_str!("runtime/mod.rs.txt");
12const UPLOAD_FILE_RS: &str = include_str!("runtime/upload_file.rs.txt");
13
14/// Returns runtime files ready to write.
15pub fn runtime_files(header: &str, include_upload_file: bool) -> Vec<FileInfo> {
16    let mut mod_rs = MOD_RS.to_string();
17    let mut files = vec![
18        FileInfo::runtime("client.rs".to_string(), with_header(header, CLIENT_RS)),
19        FileInfo::runtime("error.rs".to_string(), with_header(header, ERROR_RS)),
20        FileInfo::runtime("auth.rs".to_string(), with_header(header, AUTH_RS)),
21    ];
22    if include_upload_file {
23        mod_rs.push_str(
24            "mod upload_file;\npub use upload_file::{multipart_header_value, UploadFile};\n",
25        );
26        files.push(FileInfo::runtime(
27            "upload_file.rs".to_string(),
28            with_header(header, UPLOAD_FILE_RS),
29        ));
30    }
31    files.push(FileInfo::runtime(
32        "mod.rs".to_string(),
33        with_header(header, &mod_rs),
34    ));
35    files
36}
37
38fn with_header(header: &str, body: &str) -> String {
39    let mut out = String::with_capacity(header.len() + body.len());
40    out.push_str(header);
41    out.push_str(body);
42    out
43}