diff --git a/quotesdb/build.rs b/quotesdb/build.rs new file mode 100644 index 0000000..cc934fd --- /dev/null +++ b/quotesdb/build.rs @@ -0,0 +1,31 @@ +//! 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"); +}