1use core::fmt::Write as _;
8
9pub mod synthetic;
10
11use serde::Deserialize;
12
13#[derive(Debug, Clone, Deserialize)]
15pub struct Case {
16 pub name: String,
17 #[serde(rename = "type")]
19 pub ty: String,
20 pub validate: String,
22 pub valid: Vec<serde_json::Value>,
23 pub invalid: Vec<serde_json::Value>,
24}
25
26#[derive(Deserialize)]
27struct File {
28 cases: Vec<Case>,
29}
30
31pub const VECTORS: &str = include_str!("../vectors/rules.json");
33
34pub fn cases() -> Vec<Case> {
39 serde_json::from_str::<File>(VECTORS)
40 .map_or_else(|e| panic!("vectors/rules.json: {e}"), |f| f.cases)
41}
42
43pub fn struct_name(case: &str) -> String {
45 let pascal: String = case
46 .split('_')
47 .map(|w| {
48 let mut c = w.chars();
49 c.next()
50 .map(|f| f.to_ascii_uppercase().to_string() + c.as_str())
51 .unwrap_or_default()
52 })
53 .collect();
54 format!("V{pascal}")
55}
56
57pub fn schema() -> String {
59 let mut s = String::from("package \"vectors.v1\"\n");
60 for c in cases() {
61 let _ = write!(
62 s,
63 "\nstruct {} {{\n field v {:?} {{ validate {{ {} }} }}\n}}\n",
64 struct_name(&c.name),
65 c.ty,
66 c.validate
67 );
68 }
69 s
70}
71
72#[derive(Debug, Clone, PartialEq)]
74pub struct Mismatch {
75 pub case: String,
76 pub value: serde_json::Value,
77 pub expected_valid: bool,
78}
79
80pub fn run(mut accepts: impl FnMut(&Case, &serde_json::Value) -> bool) -> Vec<Mismatch> {
83 let mut out = Vec::new();
84 for c in cases() {
85 for (values, expected_valid) in [(&c.valid, true), (&c.invalid, false)] {
86 for v in values {
87 if accepts(&c, v) != expected_valid {
88 out.push(Mismatch {
89 case: c.name.clone(),
90 value: v.clone(),
91 expected_valid,
92 });
93 }
94 }
95 }
96 }
97 out
98}