forked from Haroldwonder/AnchorKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
169 lines (146 loc) · 5.56 KB
/
Copy pathbuild.rs
File metadata and controls
169 lines (146 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::path::Path;
use std::process::Command;
fn main() {
// Always watch the build script itself and top-level schema/validator
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=config_schema.json");
println!("cargo:rerun-if-changed=validate_config_strict.py");
println!("cargo:rerun-if-changed=src/config.rs");
println!("cargo:rerun-if-changed=src/validation.rs");
// Emit rerun-if-changed for every individual config file so Cargo
// skips re-running this script when nothing in configs/ has changed.
// (A directory-level directive only watches the dir entry itself, not
// the files inside it, so we enumerate them explicitly.)
if let Ok(entries) = std::fs::read_dir("configs") {
for entry in entries.flatten() {
let path = entry.path();
if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
if ext == "json" || ext == "toml" {
println!("cargo:rerun-if-changed={}", path.display());
}
}
}
}
// Strict compile-time validation to prevent misconfiguration bugs
validate_configs_at_build();
validate_schema_consistency();
}
fn validate_configs_at_build() {
let validator = Path::new("validate_config_strict.py");
let schema = Path::new("config_schema.json");
let configs_dir = Path::new("configs");
if !validator.exists() || !schema.exists() || !configs_dir.exists() {
println!("cargo:warning=Skipping config validation: missing validator or configs");
return;
}
// Check if Python3 is available
let python_check = Command::new("python3").arg("--version").output();
if python_check.is_err() {
println!("cargo:warning=Python3 not found, skipping config validation");
return;
}
// Check if required Python modules are installed
let module_check = Command::new("python3")
.arg("-c")
.arg("import jsonschema, toml")
.output();
match module_check {
Ok(result) if !result.status.success() => {
println!("cargo:warning=Python modules (jsonschema, toml) not installed. Run: pip3 install jsonschema toml");
println!("cargo:warning=Skipping compile-time config validation");
return;
}
Err(_) => {
println!("cargo:warning=Failed to check Python modules");
println!("cargo:warning=Skipping compile-time config validation");
return;
}
_ => {}
}
println!("cargo:warning=Running strict schema validation at compile time...");
// Validate each config file
let config_files = match std::fs::read_dir(configs_dir) {
Ok(files) => files,
Err(_) => {
println!("cargo:warning=Failed to read configs directory");
return;
}
};
let mut validated_count = 0;
for entry in config_files {
let entry = match entry {
Ok(e) => e,
Err(_) => continue,
};
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) == Some("json")
|| path.extension().and_then(|s| s.to_str()) == Some("toml")
{
let output = Command::new("python3")
.arg(validator)
.arg(&path)
.arg(schema)
.output();
match output {
Ok(result) if !result.status.success() => {
let stderr = String::from_utf8_lossy(&result.stderr);
let stdout = String::from_utf8_lossy(&result.stdout);
panic!(
"\n\n❌ STRICT VALIDATION FAILED ❌\nConfiguration validation failed for {:?}:\n{}\n{}\n",
path.file_name().unwrap(),
stdout,
stderr
);
}
Ok(_) => {
validated_count += 1;
println!("cargo:warning=✓ Validated: {:?}", path.file_name().unwrap());
}
Err(e) => {
println!(
"cargo:warning=Failed to validate {:?}: {}",
path.file_name().unwrap(),
e
);
}
}
}
}
if validated_count > 0 {
println!(
"cargo:warning=✓ Successfully validated {} configuration file(s)",
validated_count
);
}
}
/// Validate that schema constraints match Rust constants
fn validate_schema_consistency() {
use std::fs;
let schema_path = Path::new("config_schema.json");
if !schema_path.exists() {
return;
}
let schema_content = match fs::read_to_string(schema_path) {
Ok(content) => content,
Err(_) => return,
};
// Basic consistency checks
let checks = vec![
("\"maxLength\": 64", "MAX_NAME_LEN"),
("\"maxLength\": 16", "MAX_VERSION_LEN"),
("\"maxLength\": 32", "MAX_NETWORK_LEN"),
("\"maxLength\": 256", "MAX_ENDPOINT_LEN"),
("\"maxItems\": 100", "MAX_ATTESTORS"),
("\"maximum\": 86400", "MAX_SESSION_TIMEOUT"),
("\"maximum\": 10000", "MAX_OPERATIONS"),
];
for (schema_val, const_name) in checks {
if !schema_content.contains(schema_val) {
println!(
"cargo:warning=Schema consistency check: {} might not match {}",
const_name, schema_val
);
}
}
println!("cargo:warning=✓ Schema consistency validated");
}