Skip to main content

openapi_nexus/generators/rust/ureq/
runtime.rs

1//! Hardcoded Rust runtime source files for ureq (synchronous).
2
3use crate::codegen::traits::file_writer::FileInfo;
4use crate::generators::rust::common::project_files::with_header;
5use crate::generators::rust::common::runtime::render_api_call_error;
6use sigil_stitch::type_name::TypeName;
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(
16    header: &str,
17    include_api_call_error: bool,
18    include_upload_file: bool,
19) -> Vec<FileInfo> {
20    let mut mod_rs = MOD_RS.to_string();
21    let mut error_rs = ERROR_RS.to_string();
22    if include_api_call_error {
23        error_rs.push('\n');
24        error_rs.push_str(
25            &render_api_call_error(TypeName::qualified("ureq::http", "HeaderMap"))
26                .expect("ureq ApiCallError runtime renders"),
27        );
28    }
29    let mut files = vec![
30        FileInfo::runtime("client.rs".to_string(), with_header(header, CLIENT_RS)),
31        FileInfo::runtime("error.rs".to_string(), with_header(header, &error_rs)),
32        FileInfo::runtime("auth.rs".to_string(), with_header(header, AUTH_RS)),
33    ];
34    if include_upload_file {
35        mod_rs.push_str(
36            "mod upload_file;\npub use upload_file::{multipart_header_value, UploadFile};\n",
37        );
38        files.push(FileInfo::runtime(
39            "upload_file.rs".to_string(),
40            with_header(header, UPLOAD_FILE_RS),
41        ));
42    }
43    files.push(FileInfo::runtime(
44        "mod.rs".to_string(),
45        with_header(header, &mod_rs),
46    ));
47    files
48}