Skip to main content

max / makenotwork

1.3 KB · 36 lines History Blame Raw
1 //! Write the OpenAPI spec to `openapi.json` at the crate root.
2 //!
3 //! The spec is a checked-in artifact rather than something only served at
4 //! runtime, because it is the SyncKit wire contract and the client lives in
5 //! another repo. A file can be vendored and diffed; a live endpoint cannot be
6 //! reached from `synckit-client`'s test suite.
7 //!
8 //! `openapi::tests::committed_spec_matches_generated` fails when this output is
9 //! stale, so the regeneration step is:
10 //!
11 //! ```sh
12 //! cargo run --bin export-openapi
13 //! ```
14 //!
15 //! `--stdout` writes the same bytes to standard output instead of the file, so
16 //! a caller can diff the generated spec against a committed copy without
17 //! touching the source tree. The pre-commit hook uses it to compare against the
18 //! *staged* `openapi.json`, which is the copy the commit would actually carry.
19
20 use std::io::Write as _;
21
22 fn main() -> std::io::Result<()> {
23 let spec = makenotwork::openapi::spec_json();
24
25 if std::env::args().any(|a| a == "--stdout") {
26 std::io::stdout().write_all(spec.as_bytes())?;
27 return Ok(());
28 }
29
30 let path = concat!(env!("CARGO_MANIFEST_DIR"), "/openapi.json");
31 let mut file = std::fs::File::create(path)?;
32 file.write_all(spec.as_bytes())?;
33 println!("wrote {path}");
34 Ok(())
35 }
36