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
parent
b695cb5b6d
commit
9f28d99a93
@ -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");
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue