ferrous-forge 1.9.3

System-wide Rust development standards enforcer
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Cargo.toml validation functions

use crate::Result;
use crate::validation::{Severity, Violation, ViolationType};
use std::path::Path;
use tokio::fs;
use toml::Value;

/// Validates a `Cargo.toml` file for standards compliance (reads file itself).
/// Used by tests and legacy callers.
///
/// # Errors
///
/// Returns an error if the file cannot be read.
pub async fn validate_cargo_toml(
    cargo_file: &Path,
    violations: &mut Vec<Violation>,
    required_edition: &str,
    required_rust_version: &str,
) -> Result<()> {
    let content = fs::read_to_string(cargo_file).await?;
    let lines: Vec<&str> = content.lines().collect();
    validate_cargo_toml_content(
        cargo_file,
        &lines,
        violations,
        required_edition,
        required_rust_version,
    );
    Ok(())
}

/// Validates `Cargo.toml` content that has already been read into lines.
/// Used by `file_checks.rs` to avoid double file reads.
pub fn validate_cargo_toml_content(
    cargo_file: &Path,
    lines: &[&str],
    violations: &mut Vec<Violation>,
    required_edition: &str,
    required_rust_version: &str,
) {
    // Parse the manifest with the TOML parser so we correctly understand
    // workspace inheritance (`version.workspace = true`, `edition = { workspace = true }`)
    // and virtual workspaces. If parsing fails cargo itself will surface the error;
    // don't double-report.
    let content = lines.join("\n");
    let parsed = match toml::from_str::<Value>(&content) {
        Ok(v) => v,
        Err(_) => return,
    };

    validate_edition(&parsed, lines, cargo_file, violations, required_edition);
    validate_rust_version_field(
        &parsed,
        lines,
        cargo_file,
        violations,
        required_rust_version,
    );
}

/// Resolution of a package field after accounting for workspace inheritance
enum ResolvedField<'a> {
    /// Literal string value (e.g. `edition = "2024"`)
    Value(&'a str),
    /// `{ workspace = true }` — the member inherits from the workspace root
    WorkspaceInherited,
    /// Present but not a string and not workspace inheritance — malformed; skip
    Other,
    /// Not present at all
    Absent,
}

fn classify_field(value: &Value) -> ResolvedField<'_> {
    if let Some(s) = value.as_str() {
        return ResolvedField::Value(s);
    }
    if let Some(tbl) = value.as_table()
        && tbl.get("workspace").and_then(Value::as_bool) == Some(true)
    {
        return ResolvedField::WorkspaceInherited;
    }
    ResolvedField::Other
}

/// Resolve a field by checking `[package].<field>` then `[workspace.package].<field>`.
fn resolve_field<'a>(parsed: &'a Value, field: &str) -> ResolvedField<'a> {
    if let Some(value) = parsed.get("package").and_then(|p| p.get(field)) {
        return classify_field(value);
    }
    if let Some(value) = parsed
        .get("workspace")
        .and_then(|w| w.get("package"))
        .and_then(|p| p.get(field))
    {
        return classify_field(value);
    }
    ResolvedField::Absent
}

/// A pure virtual manifest has `[workspace]` but no `[package]` section.
fn is_virtual_workspace(parsed: &Value) -> bool {
    parsed.get("package").is_none() && parsed.get("workspace").is_some()
}

/// Locate the 1-indexed line of a field in the original source for error reporting.
/// Matches both `edition = "..."` and `edition.workspace = true` forms.
fn find_field_line(lines: &[&str], field: &str) -> usize {
    let eq_key = format!("{field} =");
    let tight_key = format!("{field}=");
    let dotted_key = format!("{field}.workspace");
    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim_start();
        if trimmed.starts_with(&eq_key)
            || trimmed.starts_with(&tight_key)
            || trimmed.starts_with(&dotted_key)
        {
            return i + 1;
        }
    }
    0
}

/// Check edition field against locked required value
fn validate_edition(
    parsed: &Value,
    lines: &[&str],
    cargo_file: &Path,
    violations: &mut Vec<Violation>,
    required_edition: &str,
) {
    match resolve_field(parsed, "edition") {
        ResolvedField::Value(found_edition) => {
            if found_edition != required_edition {
                violations.push(Violation {
                    violation_type: ViolationType::WrongEdition,
                    file: cargo_file.to_path_buf(),
                    line: find_field_line(lines, "edition"),
                    message: format!(
                        "FERROUS FORGE [LOCKED SETTING] — Edition Violation\n\
                         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\
                         \n\
                           Found:    edition = \"{found_edition}\"\n\
                           Required: edition = \"{required_edition}\"\n\
                           Lock:     .ferrous-forge/config.toml → required_edition = \"{required_edition}\"\n\
                         \n\
                           ⚠ AI AGENT NOTICE: This edition is locked by project configuration.\n\
                           DO NOT change required_edition without human approval.\n\
                           DO NOT downgrade Cargo.toml to match a lower edition.\n\
                         \n\
                           To fix: Update edition = \"{required_edition}\" in Cargo.toml, then run:\n\
                             cargo fix --edition"
                    ),
                    severity: Severity::Error,
                });
            }
        }
        ResolvedField::WorkspaceInherited => {
            // Member inherits from workspace root; that root's manifest is
            // validated separately when walking the project.
        }
        ResolvedField::Other => {
            // Malformed TOML value — cargo itself will reject; don't double-error.
        }
        ResolvedField::Absent => {
            // Virtual workspaces without an explicit [workspace.package].edition
            // are valid in cargo; members then must set their own. Only report if
            // this manifest actually defines a [package] section.
            if !is_virtual_workspace(parsed) {
                violations.push(Violation {
                    violation_type: ViolationType::WrongEdition,
                    file: cargo_file.to_path_buf(),
                    line: 0,
                    message: format!(
                        "FERROUS FORGE [LOCKED SETTING] — Missing Edition\n\
                         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\
                         \n\
                           Missing edition specification in Cargo.toml.\n\
                           Required: edition = \"{required_edition}\"\n\
                         \n\
                           ⚠ AI AGENT NOTICE: Add edition = \"{required_edition}\" to [package] section."
                    ),
                    severity: Severity::Error,
                });
            }
        }
    }
}

/// Check rust-version field against locked required value
fn validate_rust_version_field(
    parsed: &Value,
    lines: &[&str],
    cargo_file: &Path,
    violations: &mut Vec<Violation>,
    required_rust_version: &str,
) {
    if required_rust_version.is_empty() {
        return;
    }

    match resolve_field(parsed, "rust-version") {
        ResolvedField::Value(found_version) => {
            if found_version != required_rust_version {
                violations.push(Violation {
                    violation_type: ViolationType::OldRustVersion,
                    file: cargo_file.to_path_buf(),
                    line: find_field_line(lines, "rust-version"),
                    message: format!(
                        "FERROUS FORGE [LOCKED SETTING] — Rust Version Violation\n\
                         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\
                         \n\
                           Found:    rust-version = \"{found_version}\"\n\
                           Required: rust-version = \"{required_rust_version}\"\n\
                           Lock:     .ferrous-forge/config.toml → required_rust_version = \"{required_rust_version}\"\n\
                         \n\
                           ⚠ AI AGENT NOTICE: This rust-version is locked by project configuration.\n\
                           DO NOT change required_rust_version without human approval.\n\
                           DO NOT downgrade rust-version to resolve compilation errors."
                    ),
                    severity: Severity::Error,
                });
            }
        }
        // Workspace-inherited or absent → nothing to validate here.
        // `rust-version` is optional in cargo so missing is acceptable.
        ResolvedField::WorkspaceInherited | ResolvedField::Other | ResolvedField::Absent => {}
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::panic)]
mod tests {
    use super::*;
    use crate::config::Config;

    fn parse(src: &str) -> Value {
        toml::from_str(src).unwrap()
    }

    /// Build a [package] fixture that matches the current default config so tests
    /// remain correct if `Config::default()` evolves.
    fn manifest_with_literal_fields() -> (String, Config) {
        let config = Config::default();
        let toml = format!(
            r#"
[package]
name = "x"
version = "0.1.0"
edition = "{edition}"
rust-version = "{rust}"
"#,
            edition = config.required_edition,
            rust = config.required_rust_version,
        );
        (toml, config)
    }

    #[test]
    fn test_resolve_edition_literal() {
        let (toml, config) = manifest_with_literal_fields();
        let v = parse(&toml);
        match resolve_field(&v, "edition") {
            ResolvedField::Value(s) => assert_eq!(s, config.required_edition),
            other => panic!("expected Value, got {:?}", std::mem::discriminant(&other)),
        }
    }

    #[test]
    fn test_resolve_edition_dotted_workspace_inherit() {
        // `edition.workspace = true` must be understood as inheritance,
        // not as `edition = "true"`.
        let v = parse(
            r#"
[package]
name = "x"
version.workspace = true
edition.workspace = true
"#,
        );
        assert!(matches!(
            resolve_field(&v, "edition"),
            ResolvedField::WorkspaceInherited
        ));
    }

    #[test]
    fn test_resolve_edition_inline_workspace_inherit() {
        let v = parse(
            r#"
[package]
name = "x"
edition = { workspace = true }
"#,
        );
        assert!(matches!(
            resolve_field(&v, "edition"),
            ResolvedField::WorkspaceInherited
        ));
    }

    #[test]
    fn test_resolve_edition_from_workspace_package() {
        // Virtual workspace manifest with [workspace.package].edition
        let config = Config::default();
        let toml = format!(
            r#"
[workspace]
members = ["a", "b"]

[workspace.package]
edition = "{edition}"
version = "0.1.0"
"#,
            edition = config.required_edition,
        );
        let v = parse(&toml);
        match resolve_field(&v, "edition") {
            ResolvedField::Value(s) => assert_eq!(s, config.required_edition),
            other => panic!("expected Value, got {:?}", std::mem::discriminant(&other)),
        }
        assert!(is_virtual_workspace(&v));
    }

    #[test]
    fn test_workspace_member_inheriting_is_not_flagged() {
        let config = Config::default();
        let v = parse(
            r#"
[package]
name = "child"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
"#,
        );
        let mut violations = Vec::new();
        validate_edition(
            &v,
            &[],
            Path::new("Cargo.toml"),
            &mut violations,
            &config.required_edition,
        );
        validate_rust_version_field(
            &v,
            &[],
            Path::new("Cargo.toml"),
            &mut violations,
            &config.required_rust_version,
        );
        assert!(
            violations.is_empty(),
            "workspace-inherited member should not produce violations, got: {violations:?}"
        );
    }

    #[test]
    fn test_virtual_workspace_without_edition_is_not_flagged() {
        let config = Config::default();
        let v = parse(
            r#"
[workspace]
members = ["a"]
"#,
        );
        let mut violations = Vec::new();
        validate_edition(
            &v,
            &[],
            Path::new("Cargo.toml"),
            &mut violations,
            &config.required_edition,
        );
        assert!(violations.is_empty());
    }

    #[test]
    fn test_wrong_edition_still_flagged() {
        // Deliberately set a literal edition that cannot equal the required edition
        // from the default config, regardless of how the default evolves.
        let config = Config::default();
        let wrong_edition = if config.required_edition == "2018" {
            "2015"
        } else {
            "2018"
        };
        let toml = format!(
            r#"
[package]
name = "x"
version = "0.1.0"
edition = "{wrong_edition}"
"#
        );
        let v = parse(&toml);
        let lines: Vec<&str> = toml.lines().collect();
        let mut violations = Vec::new();
        validate_edition(
            &v,
            &lines,
            Path::new("Cargo.toml"),
            &mut violations,
            &config.required_edition,
        );
        assert_eq!(violations.len(), 1);
        // The edition line is the 5th line of the fixture (index 5: blank, [package], name, version, edition)
        assert!(violations[0].line > 0, "expected a concrete line number");
    }

    #[test]
    fn test_find_field_line_handles_dotted_form() {
        let lines = [
            "[package]",
            "name = \"x\"",
            "version.workspace = true",
            "edition.workspace = true",
        ];
        assert_eq!(find_field_line(&lines, "edition"), 4);
        assert_eq!(find_field_line(&lines, "version"), 3);
    }
}