Skip to content

Commit d926c47

Browse files
palfreyPegasust
andauthored
Add json schema (#2193)
* feat: add json schema generation for CasConfig via schemars --------- Co-authored-by: pegasust <pegasucksgg@gmail.com>
1 parent 3354945 commit d926c47

File tree

8 files changed

+163
-11
lines changed

8 files changed

+163
-11
lines changed

.github/workflows/native-cargo.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ jobs:
5050

5151
- name: Test on ${{ runner.os }}
5252
run: cargo test --all --profile=smol
53+
54+
- name: Check schema export
55+
run: cargo run --bin build-schema --features dev-schema --package nativelink-config

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ darwin.bazelrc
2424
nativelink.bazelrc
2525
*.log
2626
buck-out/
27+
nativelink_config.schema.json

Cargo.lock

Lines changed: 30 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nativelink-config/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ humantime = { version = "2.3.0", default-features = false }
1414
rand = { version = "0.9.0", default-features = false, features = [
1515
"thread_rng",
1616
] }
17+
schemars = { version = "1.2.1", default-features = false, features = [
18+
"derive",
19+
"std",
20+
], optional = true }
1721
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
1822
serde_json = { version = "1.0.140", default-features = false, features = [
1923
"std",
@@ -31,3 +35,11 @@ pretty_assertions = { version = "1.4.1", features = [
3135
tracing-test = { version = "0.2.5", default-features = false, features = [
3236
"no-env-filter",
3337
] }
38+
39+
[features]
40+
dev-schema = ["schemars"]
41+
42+
[[bin]]
43+
name = "build-schema"
44+
path = "src/bin/build_schema.rs"
45+
required-features = ["dev-schema"]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! ```sh
2+
//! cargo run --bin build-schema --features dev-schema --package nativelink-config
3+
//! ```
4+
5+
#[cfg(feature = "dev-schema")]
6+
fn main() {
7+
use std::fs::File;
8+
9+
use nativelink_config::cas_server::CasConfig;
10+
use schemars::schema_for;
11+
use serde_json::to_writer_pretty;
12+
const FILE: &str = "nativelink_config.schema.json";
13+
14+
let schema = schema_for!(CasConfig);
15+
to_writer_pretty(File::create(FILE).expect("to create file"), &schema)
16+
.expect("to export schema");
17+
18+
println!("Wrote schema to {FILE}");
19+
}
20+
21+
#[cfg(not(feature = "dev-schema"))]
22+
fn main() {
23+
eprintln!("Enable with --features dev-schema");
24+
}

0 commit comments

Comments
 (0)