immich-tools/build.rs

40 lines
1.2 KiB
Rust

use openapiv3::OpenAPI;
use std::{
env,
fs::{self, File},
path::Path,
};
fn main() {
let src = "./src/immich-openapi-specs.json";
let out_dir = Path::new(&env::var("OUT_DIR").unwrap()).to_path_buf();
let file = File::open(src).unwrap();
let spec = serde_json::from_reader(file).unwrap();
let client = generate_client(&spec);
let client_dst = out_dir.join("client.rs");
fs::write(&client_dst, client).unwrap();
let mock = generate_mock(&spec);
let mock_dst = out_dir.join("mock.rs");
fs::write(&mock_dst, mock).unwrap();
println!("cargo:rerun-if-changed={src}");
println!("cargo:rerun-if-changed={}", client_dst.display());
println!("cargo:rerun-if-changed={}", mock_dst.display());
}
fn generate_client(spec: &OpenAPI) -> String {
let mut generator = progenitor::Generator::default();
let tokens = generator.generate_tokens(spec).unwrap();
let ast = syn::parse2(tokens).unwrap();
prettyplease::unparse(&ast)
}
fn generate_mock(spec: &OpenAPI) -> String {
let mut generator = progenitor::Generator::default();
let tokens = generator.httpmock(spec, "crate").unwrap();
let ast = syn::parse2(tokens).unwrap();
prettyplease::unparse(&ast)
}