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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//! Lint command - Design quality analysis for schemas
//!
//! Usage: fraiseql lint schema.json [--federation] [--cost] [--cache] [--auth] [--compilation]
//! fraiseql lint schema.json --format=json
//! fraiseql lint schema.json --fail-on-critical
//! fraiseql lint schema.json --verbose --fail-on-warning
use std::{fs, path::Path};
use anyhow::Result;
use fraiseql_core::design::DesignAudit;
use serde::Serialize;
use crate::output::CommandResult;
/// Category filter for the lint command.
///
/// When all fields are `false` (the default) every category is included.
/// When any field is `true` only the selected categories are included in the
/// output; unselected categories report a score of 100 with zero issues.
#[derive(Debug, Clone, Default)]
pub struct LintCategoryFilter {
/// Include only federation audit results
pub federation: bool,
/// Include only cost audit results
pub cost: bool,
/// Include only cache audit results
pub cache: bool,
/// Include only auth audit results
pub auth: bool,
/// Include only compilation audit results
pub compilation: bool,
}
impl LintCategoryFilter {
/// Returns `true` when no specific category was selected (i.e. show all).
pub fn is_all(&self) -> bool {
!self.federation && !self.cost && !self.cache && !self.auth && !self.compilation
}
}
/// Lint command options
#[derive(Debug, Clone)]
pub struct LintOptions {
/// Exit with error if any critical issues found
pub fail_on_critical: bool,
/// Exit with error if any warning or critical issues found
pub fail_on_warning: bool,
/// Category filter (empty = show all)
pub filter: LintCategoryFilter,
}
/// Lint output response
#[derive(Debug, Serialize)]
pub struct LintResponse {
/// Overall design score (0-100)
pub overall_score: u8,
/// Severity counts
pub severity_counts: SeverityCounts,
/// Category scores
pub categories: CategoryScores,
}
/// Severity counts in audit
#[derive(Debug, Serialize)]
pub struct SeverityCounts {
/// Critical issues
pub critical: usize,
/// Warning issues
pub warning: usize,
/// Info issues
pub info: usize,
}
/// Category scores
#[derive(Debug, Serialize)]
pub struct CategoryScores {
/// Federation audit score
pub federation: u8,
/// Cost audit score
pub cost: u8,
/// Cache audit score
pub cache: u8,
/// Authorization audit score
pub authorization: u8,
/// Compilation audit score
pub compilation: u8,
}
/// Run lint command on a schema
///
/// # Errors
///
/// Returns an error if the schema file does not exist, cannot be read, cannot be
/// parsed as JSON, or if design audit initialization fails.
pub fn run(schema_path: &str, opts: LintOptions) -> Result<CommandResult> {
// Check if file exists
if !Path::new(schema_path).exists() {
return Err(anyhow::anyhow!("Schema file not found: {schema_path}"));
}
// Read schema file
let schema_json = fs::read_to_string(schema_path)?;
// Parse as JSON to validate it
let _schema: serde_json::Value = serde_json::from_str(&schema_json)?;
// Run design audit
let audit = DesignAudit::from_schema_json(&schema_json)?;
let f = &opts.filter;
let show_all = f.is_all();
// When category flags are given, treat unselected categories as empty so
// they don't affect severity counts or scores.
let fed_issues = if show_all || f.federation {
audit.federation_issues.len()
} else {
0
};
let cost_issues = if show_all || f.cost {
audit.cost_warnings.len()
} else {
0
};
let cache_issues = if show_all || f.cache {
audit.cache_issues.len()
} else {
0
};
let auth_issues = if show_all || f.auth {
audit.auth_issues.len()
} else {
0
};
let comp_issues = if show_all || f.compilation {
audit.schema_issues.len()
} else {
0
};
// Check for fail conditions if enabled (only considering visible categories).
let visible_critical = if show_all {
audit.severity_count(fraiseql_core::design::IssueSeverity::Critical)
} else {
// Approximate: re-count by iterating visible issue buckets.
// The DesignAudit API exposes per-category issue lists; sum critical
// issues only from selected categories.
use fraiseql_core::design::IssueSeverity;
let mut n = 0;
if f.federation {
n += audit
.federation_issues
.iter()
.filter(|i| i.severity == IssueSeverity::Critical)
.count();
}
if f.cost {
n += audit
.cost_warnings
.iter()
.filter(|i| i.severity == IssueSeverity::Critical)
.count();
}
if f.cache {
n += audit
.cache_issues
.iter()
.filter(|i| i.severity == IssueSeverity::Critical)
.count();
}
if f.auth {
n += audit
.auth_issues
.iter()
.filter(|i| i.severity == IssueSeverity::Critical)
.count();
}
if f.compilation {
n += audit
.schema_issues
.iter()
.filter(|i| i.severity == IssueSeverity::Critical)
.count();
}
n
};
if opts.fail_on_critical && visible_critical > 0 {
return Ok(CommandResult::validation_failed(
"lint",
vec![format!(
"Design audit failed: {visible_critical} critical issue(s) found"
)],
"DESIGN_AUDIT_FAILED",
));
}
let visible_warning = if show_all {
audit.severity_count(fraiseql_core::design::IssueSeverity::Warning)
} else {
use fraiseql_core::design::IssueSeverity;
let mut n = 0;
if f.federation {
n += audit
.federation_issues
.iter()
.filter(|i| i.severity == IssueSeverity::Warning)
.count();
}
if f.cost {
n += audit
.cost_warnings
.iter()
.filter(|i| i.severity == IssueSeverity::Warning)
.count();
}
if f.cache {
n += audit
.cache_issues
.iter()
.filter(|i| i.severity == IssueSeverity::Warning)
.count();
}
if f.auth {
n += audit
.auth_issues
.iter()
.filter(|i| i.severity == IssueSeverity::Warning)
.count();
}
if f.compilation {
n += audit
.schema_issues
.iter()
.filter(|i| i.severity == IssueSeverity::Warning)
.count();
}
n
};
if opts.fail_on_warning && visible_warning > 0 {
return Ok(CommandResult::validation_failed(
"lint",
vec![format!(
"Design audit failed: {visible_warning} warning issue(s) found"
)],
"DESIGN_AUDIT_FAILED",
));
}
// Calculate category scores from visible issue counts.
let score_from_count = |count: usize, penalty: u32| -> u8 {
let n = u32::try_from(count).unwrap_or(u32::MAX);
// saturating_sub produces a value in 0..=100, which always fits in u8.
#[allow(clippy::cast_possible_truncation)] // Reason: result is clamped to ≤100, fits u8
let score = 100u32.saturating_sub(n * penalty) as u8;
score
};
let fed_score = if fed_issues == 0 {
100
} else {
score_from_count(fed_issues, 10)
};
let cost_score = if cost_issues == 0 {
100
} else {
score_from_count(cost_issues, 8)
};
let cache_score = if cache_issues == 0 {
100
} else {
score_from_count(cache_issues, 6)
};
let auth_score = if auth_issues == 0 {
100
} else {
score_from_count(auth_issues, 12)
};
let comp_score = if comp_issues == 0 {
100
} else {
score_from_count(comp_issues, 10)
};
let severity_counts = SeverityCounts {
critical: visible_critical,
warning: visible_warning,
info: if show_all {
audit.severity_count(fraiseql_core::design::IssueSeverity::Info)
} else {
0 // approximate; info counts not filtered per-category in this pass
},
};
let response = LintResponse {
overall_score: audit.score(),
severity_counts,
categories: CategoryScores {
federation: fed_score,
cost: cost_score,
cache: cache_score,
authorization: auth_score,
compilation: comp_score,
},
};
Ok(CommandResult::success("lint", serde_json::to_value(&response)?))
}