Skip to main content

openapi_nexus/generators/go/http/
runtime.rs

1//! Hardcoded Go runtime source files.
2//!
3//! The runtime surface is tiny on purpose: one `runtime` package with a
4//! `Client` carrying functional options, an `Authenticator` interface, and
5//! typed `APIError`. Advanced needs (retries, hooks, multi-server) are the
6//! user's responsibility and can be layered on a wrapped `*http.Client`.
7
8use crate::codegen::traits::file_writer::FileInfo;
9
10const CLIENT_GO: &str = include_str!("runtime/client.go.txt");
11const AUTH_GO: &str = include_str!("runtime/auth.go.txt");
12const ERRORS_GO: &str = include_str!("runtime/errors.go.txt");
13const UPLOAD_FILE_GO: &str = include_str!("runtime/upload_file.go.txt");
14
15/// Returns client.go, auth.go, errors.go ready to write.
16///
17/// The category routes these under `<output>/runtime/` via `FileWriter`.
18pub fn runtime_files(header: &str, include_upload_file: bool) -> Vec<FileInfo> {
19    let mut files = vec![
20        FileInfo::runtime("client.go".to_string(), with_header(header, CLIENT_GO)),
21        FileInfo::runtime("auth.go".to_string(), with_header(header, AUTH_GO)),
22        FileInfo::runtime("errors.go".to_string(), with_header(header, ERRORS_GO)),
23    ];
24    if include_upload_file {
25        files.push(FileInfo::runtime(
26            "upload_file.go".to_string(),
27            with_header(header, UPLOAD_FILE_GO),
28        ));
29    }
30    files
31}
32
33fn with_header(header: &str, body: &str) -> String {
34    let mut out = String::with_capacity(header.len() + body.len());
35    out.push_str(header);
36    out.push_str(body);
37    out
38}