-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain.rs
More file actions
270 lines (239 loc) · 8.13 KB
/
Copy pathmain.rs
File metadata and controls
270 lines (239 loc) · 8.13 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// *******************************************************************************
// Copyright (c) 2026 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// <https://www.apache.org/licenses/LICENSE-2.0>
//
// SPDX-License-Identifier: Apache-2.0
// *******************************************************************************
//! Validation CLI entrypoint.
//!
//! Supports architecture and design validations inferred from provided
//! input types.
use std::fs;
use std::mem;
use std::process;
use clap::Parser;
use validation::{
validate_bazel_component, validate_component_class, BazelArchitecture, BazelInput, BazelReader,
ClassDiagramIndex, ClassDiagramInputs, ClassDiagramReader, ComponentDiagramArchitecture,
ComponentDiagramInputs, ComponentDiagramReader, Errors, Reader, RequiredInput,
SelectedValidator, ValidatorSpec, ALL_VALIDATORS,
};
#[derive(Parser, Debug)]
#[command(name = "validation")]
#[command(version = "1.0")]
#[command(about = "Validate architecture and design consistency from PlantUML exports")]
struct Args {
#[arg(long)]
architecture_json: Option<String>,
#[arg(long = "component-fbs", num_args = 1..)]
component_fbs: Option<Vec<String>>,
#[arg(long = "class-fbs", num_args = 1..)]
class_fbs: Option<Vec<String>>,
#[arg(long)]
output: Option<String>,
/// When set, validation errors are printed as warnings and the tool exits
/// with code 0. Intended for use during development (maturity=development).
#[arg(long, default_value_t = false)]
warn_on_errors: bool,
}
struct ValidationCliInputs {
architecture_json: Option<String>,
component_fbs: Vec<String>,
class_fbs: Vec<String>,
}
struct ValidationContext {
base_errors: Errors,
bazel: Option<BazelArchitecture>,
component: Option<ComponentDiagramArchitecture>,
class: Option<ClassDiagramIndex>,
}
impl ValidationContext {
fn has_input(&self, input: RequiredInput) -> bool {
match input {
RequiredInput::Bazel => self.bazel.is_some(),
RequiredInput::Component => self.component.is_some(),
RequiredInput::Class => self.class.is_some(),
}
}
}
fn read_and_convert<R, O>(
input: &R::Input,
errors: &mut Errors,
convert: impl Fn(R::Raw, &mut Errors) -> O,
) -> Result<Option<O>, String>
where
R: Reader,
{
if !R::is_present(input) {
return Ok(None);
}
let raw = R::read(input).map_err(|e| e.to_string())?;
Ok(Some(convert(raw, errors)))
}
fn run(args: Args) -> Result<(), String> {
let inputs = ValidationCliInputs {
architecture_json: args.architecture_json,
component_fbs: args.component_fbs.unwrap_or_default(),
class_fbs: args.class_fbs.unwrap_or_default(),
};
let mut context = build_validation_context(inputs)?;
let validators = resolve_validators(&context)?;
run_selected_validators(
args.output.as_deref(),
args.warn_on_errors,
&validators,
&mut context,
)
}
fn resolve_validators(context: &ValidationContext) -> Result<Vec<SelectedValidator>, String> {
let inferred = ALL_VALIDATORS
.iter()
.copied()
.filter(|validator| validator.can_run(|input| context.has_input(input)))
.collect::<Vec<_>>();
if inferred.is_empty() {
Err(
"Unable to infer any validation to run from inputs. Provide compatible input files (for example: --architecture-json with --component-fbs, or --component-fbs with --class-fbs)."
.to_string(),
)
} else {
Ok(inferred)
}
}
fn run_selected_validators(
output_path: Option<&str>,
warn_on_errors: bool,
validators: &[SelectedValidator],
context: &mut ValidationContext,
) -> Result<(), String> {
let mut errors = mem::take(&mut context.base_errors);
for validator in validators {
merge_errors(&mut errors, run_validator(*validator, context));
}
finish_validation(output_path, warn_on_errors, &errors)
}
fn run_validator(validator: SelectedValidator, context: &ValidationContext) -> Errors {
match validator {
SelectedValidator::BazelComponent => {
let (bazel, component) = bazel_component_refs(context)
.expect("BazelComponent validator requires Bazel and component inputs");
validate_bazel_component(bazel, component, Errors::default())
}
SelectedValidator::ComponentClass => {
let (component, class) = component_class_refs(context)
.expect("ComponentClass validator requires component and class inputs");
validate_component_class(component, class, Errors::default())
}
}
}
fn build_validation_context(inputs: ValidationCliInputs) -> Result<ValidationContext, String> {
let mut errors = Errors::default();
let bazel = match inputs.architecture_json.as_deref() {
Some(path) => read_and_convert::<BazelReader, BazelArchitecture>(
path,
&mut errors,
|raw: BazelInput, errs| raw.to_bazel_architecture(errs),
)?,
None => None,
};
let component = read_and_convert::<ComponentDiagramReader, ComponentDiagramArchitecture>(
inputs.component_fbs.as_slice(),
&mut errors,
|raw: ComponentDiagramInputs, errs| raw.to_diagram_architecture(errs),
)?;
let class = read_and_convert::<ClassDiagramReader, ClassDiagramIndex>(
inputs.class_fbs.as_slice(),
&mut errors,
|raw: ClassDiagramInputs, errs| raw.to_class_diagram_index(errs),
)?;
Ok(ValidationContext {
base_errors: errors,
bazel,
component,
class,
})
}
fn bazel_component_refs(
context: &ValidationContext,
) -> Option<(&BazelArchitecture, &ComponentDiagramArchitecture)> {
Some((context.bazel.as_ref()?, context.component.as_ref()?))
}
fn component_class_refs(
context: &ValidationContext,
) -> Option<(&ComponentDiagramArchitecture, &ClassDiagramIndex)> {
Some((context.component.as_ref()?, context.class.as_ref()?))
}
fn merge_errors(target: &mut Errors, incoming: Errors) {
target.messages.extend(incoming.messages);
if !incoming.debug_output.is_empty() {
if !target.debug_output.is_empty() {
target.debug_output.push_str("\n\n");
}
target.debug_output.push_str(&incoming.debug_output);
}
}
fn finish_validation(
output_path: Option<&str>,
warn_on_errors: bool,
errors: &Errors,
) -> Result<(), String> {
if let Some(path) = output_path {
write_log(path, errors)?;
}
if errors.is_empty() {
Ok(())
} else {
let details = errors
.messages
.iter()
.enumerate()
.map(|(i, msg)| format!(" [{}] {}", i + 1, msg))
.collect::<Vec<_>>()
.join("\n\n");
let output = format!(
"Verification FAILED ({} error(s)):\n\n{}",
errors.messages.len(),
details
);
if warn_on_errors {
eprintln!("WARNING: {output}");
Ok(())
} else {
Err(output)
}
}
}
fn write_log(path: &str, errors: &Errors) -> Result<(), String> {
let content = if errors.is_empty() {
format!("PASS\n\n{}", errors.debug_output)
} else {
let details = errors
.messages
.iter()
.enumerate()
.map(|(i, msg)| format!("[{}] {}", i + 1, msg))
.collect::<Vec<_>>()
.join("\n\n");
let mut s = format!(
"FAILED ({} error(s)):\n\n{}",
errors.messages.len(),
details
);
s.push_str("\n--- Debug Information ---\n\n");
s.push_str(&errors.debug_output);
s
};
fs::write(path, content).map_err(|e| format!("Failed to write output file {path}: {e}"))
}
fn main() {
if let Err(msg) = run(Args::parse()) {
eprintln!("{msg}");
process::exit(1);
}
}