Skip to content

Commit 516d58f

Browse files
committed
refactor: instance schema validation compile time
Signed-off-by: devjow <me@devjow.com>
1 parent 92ac8bc commit 516d58f

21 files changed

Lines changed: 1093 additions & 595 deletions

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,10 @@ glob = "0.3"
188188
# CLI and terminal output
189189
colored = "3.0"
190190

191+
# Rust syntax parsing (used by gts-cli for struct expression extraction)
192+
syn = { version = "2.0", features = ["full", "extra-traits"] }
193+
quote = "1.0"
194+
proc-macro2 = "1.0"
195+
191196
# Format parsing
192197
serde-saphyr = "0.0.10"

gts-cli/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ chrono.workspace = true
3434
regex.workspace = true
3535
walkdir.workspace = true
3636
jsonschema.workspace = true
37+
syn.workspace = true
38+
quote.workspace = true
39+
proc-macro2.workspace = true
3740

3841
[dev-dependencies]
3942
tempfile = "3.8"

gts-cli/src/gen_instances/mod.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod attrs;
22
pub mod parser;
33
pub mod schema_check;
4-
pub mod string_lit;
4+
pub mod struct_expr;
55
pub mod writer;
66

77
use anyhow::{Result, bail};
@@ -179,7 +179,9 @@ fn print_summary(files_scanned: usize, files_skipped: usize, instances_generated
179179
println!(" Files skipped: {files_skipped}");
180180
println!(" Instances generated: {instances_generated}");
181181
if instances_generated == 0 {
182-
println!("\n No instances found. Annotate consts with `#[gts_well_known_instance(...)]`.");
182+
println!(
183+
"\n No instances found. Annotate fn items with `#[gts_well_known_instance(...)]`."
184+
);
183185
}
184186
}
185187

@@ -193,16 +195,18 @@ mod tests {
193195
fs::write(dir.join(name), content).unwrap();
194196
}
195197

196-
fn valid_src(id: &str, json_body: &str) -> String {
198+
fn valid_src(id: &str, struct_body: &str) -> String {
197199
format!(
198200
concat!(
199201
"#[gts_well_known_instance(\n",
200202
" dir_path = \"instances\",\n",
201203
" id = \"{}\"\n",
202204
")]\n",
203-
"pub const FOO: &str = {};\n"
205+
"fn get_instance_item_v1() -> MyStruct {{\n",
206+
" {}\n",
207+
"}}\n"
204208
),
205-
id, json_body
209+
id, struct_body
206210
)
207211
}
208212

@@ -216,7 +220,7 @@ mod tests {
216220
"module.rs",
217221
&valid_src(
218222
"gts.x.core.events.topic.v1~x.commerce._.orders.v1.0",
219-
r#""{\"name\": \"orders\", \"partitions\": 16}""#,
223+
r#"MyStruct { name: String::from("orders"), partitions: 16 }"#,
220224
),
221225
);
222226

@@ -256,12 +260,16 @@ mod tests {
256260
" dir_path = \"instances\",\n",
257261
" id = \"gts.x.core.events.topic.v1~x.commerce._.orders.v1.0\"\n",
258262
")]\n",
259-
"const A: &str = \"{\\\"name\\\": \\\"a\\\"}\";\n",
263+
"fn get_instance_orders_v1() -> MyStruct {\n",
264+
" MyStruct { name: String::from(\"a\") }\n",
265+
"}\n",
260266
"#[gts_well_known_instance(\n",
261267
" dir_path = \"instances\",\n",
262268
" id = \"gts.x.core.events.topic.v1~x.commerce._.orders.v1.0\"\n",
263269
")]\n",
264-
"const B: &str = \"{\\\"name\\\": \\\"b\\\"}\";\n"
270+
"fn get_instance_orders2_v1() -> MyStruct {\n",
271+
" MyStruct { name: String::from(\"b\") }\n",
272+
"}\n"
265273
);
266274
write_src(&root, "dup.rs", dup_src);
267275

@@ -295,7 +303,9 @@ mod tests {
295303
" dir_path = \"instances\",\n",
296304
" id = \"bad-no-tilde\"\n",
297305
")]\n",
298-
"const X: &str = \"{}\";\n"
306+
"fn get_instance_bad_v1() -> MyStruct {\n",
307+
" MyStruct { name: String::from(\"x\") }\n",
308+
"}\n"
299309
),
300310
);
301311

0 commit comments

Comments
 (0)