From 9f28d99a93acb3913048997070b07ed48a9e0dc4 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Mon, 2 Mar 2026 22:01:53 -0800 Subject: [PATCH] feat(quotesdb): add build.rs to convert api/openapi.yaml to JSON at compile time - Create build.rs at crate root using serde_yaml (build-dep only) to parse api/openapi.yaml and write compact JSON to $OUT_DIR/openapi.json - cargo:rerun-if-changed ensures re-conversion only when spec changes - serde_yaml never enters the Workers or native binary (build-dep only) - Downstream GET /api/ handler consumes via include_str!(concat!(env!("OUT_DIR"), "/openapi.json")) Closes ticket 8892d5 --- quotesdb/build.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 quotesdb/build.rs 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"); +}