You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.2 KiB
Rust

//! Build script for the `quotesdb` crate.
//!
//! Reads `api/openapi.yaml`, parses it into a `serde_json::Value`, and writes
//! compact JSON to `$OUT_DIR/openapi.json` at compile time.
//!
//! The API handler uses the output via:
//! ```ignore
//! const OPENAPI_JSON: &str = include_str!(concat!(env!("OUT_DIR"), "/openapi.json"));
//! ```
//!
//! Using `serde_yaml` as a build-dependency only means it never enters the
//! Workers binary or native binary — zero runtime cost. The conversion runs
//! only when `api/openapi.yaml` changes, thanks to `cargo:rerun-if-changed`.
use std::{env, fs, path::Path};
fn main() {
// Re-run this script whenever the OpenAPI spec changes.
println!("cargo:rerun-if-changed=api/openapi.yaml");
let yaml = fs::read_to_string("api/openapi.yaml").expect("failed to read api/openapi.yaml");
let value: serde_json::Value =
serde_yaml::from_str(&yaml).expect("failed to parse api/openapi.yaml");
let json = serde_json::to_string(&value).expect("JSON serialisation failed");
let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set");
let out_path = Path::new(&out_dir).join("openapi.json");
fs::write(&out_path, json).expect("failed to write openapi.json");
}