rustcop 0.1.5

A Rust style linter and formatter inspired by C#'s StyleCop
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use std::path::{Path, PathBuf};

use globset::{Glob, GlobSetBuilder};
use serde::Deserialize;

/// Generic configuration container that stores raw TOML data
/// and allows deserializing specific sections on demand.
#[derive(Debug, Clone)]
pub struct Config {
    pub(crate) raw: toml::Value,
    config_dir: Option<PathBuf>,
}

/// A raw configuration file with metadata
#[derive(Debug)]
struct RawConfig {
    raw: toml::Value,
    path: PathBuf,
    #[allow(dead_code)]
    is_root: bool,
}

/// Override configuration block
#[derive(Debug, Deserialize, Clone)]
struct Override {
    files: Vec<String>,
    #[serde(default)]
    exclude: Vec<String>,
    #[serde(flatten)]
    config: toml::Value,
}

impl Config {
    /// Load configuration from a TOML file
    pub fn load(path: &Path) -> Result<Self, Box<dyn std::error::Error>> {
        let content = std::fs::read_to_string(path)?;
        let raw: toml::Value = toml::from_str(&content)?;

        // Validate version
        Self::validate_version(&raw)?;

        let config_dir = path.parent().map(|p| p.to_path_buf());
        Ok(Config { raw, config_dir })
    }

    /// Create an empty configuration (all sections will use defaults)
    pub fn empty() -> Self {
        Config {
            raw: toml::Value::Table(Default::default()),
            config_dir: None,
        }
    }

    /// Resolve configuration for a specific source file (hierarchical discovery + overrides)
    ///
    /// This implements the full resolution algorithm from the spec:
    /// 1. Discover rustcop.toml files by walking upward
    /// 2. Stop at root = true
    /// 3. Apply configs from outermost → innermost
    /// 4. Apply matching overrides
    pub fn resolve_for_file(source_file: &Path) -> Result<Self, Box<dyn std::error::Error>> {
        // Discover all config files
        let config_files = Self::discover_configs(source_file)?;

        if config_files.is_empty() {
            return Ok(Self::empty());
        }

        // Start with built-in defaults (empty config)
        let mut merged = toml::Value::Table(toml::map::Map::new());
        let mut final_config_dir = None;

        // Apply configs from outermost → innermost
        for raw_config in config_files {
            if final_config_dir.is_none() {
                final_config_dir = raw_config.path.parent().map(|p| p.to_path_buf());
            }
            Self::merge_tables(&mut merged, &raw_config.raw);
        }

        let mut config = Config {
            raw: merged,
            config_dir: final_config_dir,
        };

        // Apply matching overrides
        config.apply_overrides_for_file(source_file)?;

        Ok(config)
    }

    /// Discover rustcop.toml files by walking upward from the source file
    fn discover_configs(source_file: &Path) -> Result<Vec<RawConfig>, Box<dyn std::error::Error>> {
        let mut configs = Vec::new();
        let mut current_dir = source_file.parent();

        while let Some(dir) = current_dir {
            let config_path = dir.join("rustcop.toml");

            if config_path.exists() {
                let content = std::fs::read_to_string(&config_path)?;
                let raw: toml::Value = toml::from_str(&content)?;

                // Validate version
                Self::validate_version(&raw)?;

                let is_root = raw.get("root").and_then(|v| v.as_bool()).unwrap_or(false);

                configs.push(RawConfig {
                    raw,
                    path: config_path,
                    is_root,
                });

                if is_root {
                    break;
                }
            }

            current_dir = dir.parent();
        }

        // Reverse so outermost is first
        configs.reverse();
        Ok(configs)
    }

    /// Validate the version field
    fn validate_version(raw: &toml::Value) -> Result<(), Box<dyn std::error::Error>> {
        match raw.get("version") {
            Some(toml::Value::Integer(1)) => Ok(()),
            Some(toml::Value::Integer(v)) => Err(format!(
                "Unsupported config version: {}. Only version 1 is supported.",
                v
            )
            .into()),
            Some(_) => Err("Invalid version field: must be an integer".into()),
            None => {
                // Version is optional for now, but we warn
                eprintln!("warning: rustcop.toml is missing 'version' field. Add 'version = 1' to the config.");
                Ok(())
            }
        }
    }

    /// Merge two TOML tables according to spec merge semantics:
    /// - Scalars: replaced
    /// - Arrays: replaced entirely
    /// - Tables: merged recursively
    fn merge_tables(base: &mut toml::Value, override_val: &toml::Value) {
        use toml::Value;

        match (base, override_val) {
            (Value::Table(base_map), Value::Table(override_map)) => {
                for (key, override_value) in override_map {
                    if let Some(base_value) = base_map.get_mut(key) {
                        // Recursively merge if both are tables
                        if matches!(base_value, Value::Table(_))
                            && matches!(override_value, Value::Table(_))
                        {
                            Self::merge_tables(base_value, override_value);
                        } else {
                            // Replace (scalars and arrays)
                            *base_value = override_value.clone();
                        }
                    } else {
                        base_map.insert(key.clone(), override_value.clone());
                    }
                }
            }
            _ => {
                // This shouldn't happen if we're only merging root tables
            }
        }
    }

    /// Apply overrides that match the given source file
    fn apply_overrides_for_file(
        &mut self,
        source_file: &Path,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Extract overrides array
        let overrides = match self.raw.get("overrides") {
            Some(toml::Value::Array(arr)) => arr.clone(),
            _ => return Ok(()), // No overrides
        };

        // Get the config directory for relative path resolution
        let config_dir = self
            .config_dir
            .as_ref()
            .ok_or("Cannot apply overrides without config directory")?;

        // Make source_file relative to config_dir
        let relative_source = source_file.strip_prefix(config_dir).unwrap_or(source_file);

        // Apply each matching override in order
        for override_val in overrides {
            if let Ok(override_config) = override_val.clone().try_into::<Override>() {
                if Self::matches_override(&override_config, relative_source)? {
                    // Apply this override by merging its config
                    let mut override_config_val = override_config.config;

                    // Remove the 'files' and 'exclude' fields that were captured
                    if let toml::Value::Table(ref mut map) = override_config_val {
                        map.remove("files");
                        map.remove("exclude");
                    }

                    Self::merge_tables(&mut self.raw, &override_config_val);
                }
            }
        }

        Ok(())
    }

    /// Check if a file matches an override's patterns
    fn matches_override(
        override_config: &Override,
        file_path: &Path,
    ) -> Result<bool, Box<dyn std::error::Error>> {
        // Build include globset
        let mut include_builder = GlobSetBuilder::new();
        for pattern in &override_config.files {
            include_builder.add(Glob::new(pattern)?);
        }
        let include_set = include_builder.build()?;

        // Build exclude globset
        let mut exclude_builder = GlobSetBuilder::new();
        for pattern in &override_config.exclude {
            exclude_builder.add(Glob::new(pattern)?);
        }
        let exclude_set = exclude_builder.build()?;

        // Check if file matches include and doesn't match exclude
        let included = include_set.is_match(file_path);
        let excluded = exclude_set.is_match(file_path);

        Ok(included && !excluded)
    }

    /// Get a configuration section deserialized to type T
    ///
    /// Extra fields in the TOML are tolerated and ignored.
    /// If the section doesn't exist, returns the default value for T.
    ///
    /// # Example
    /// ```no_run
    /// use rustcop::config::{Config, ImportsConfig};
    /// use std::path::Path;
    ///
    /// let config = Config::load(Path::new("rustcop.toml")).unwrap();
    /// let imports_config = config.get_config::<ImportsConfig>("imports").unwrap();
    /// ```
    pub fn get_config<'de, T>(&self, section: &str) -> Result<T, Box<dyn std::error::Error>>
    where
        T: Deserialize<'de> + Default,
    {
        if let Some(section_value) = self.raw.get(section) {
            let config: T = section_value.clone().try_into()?;
            Ok(config)
        } else {
            Ok(T::default())
        }
    }

    /// Get the raw TOML value for advanced use cases
    pub fn raw(&self) -> &toml::Value {
        &self.raw
    }

    /// Get a nested configuration section (e.g., "lints.disallow_super_imports")
    ///
    /// # Example
    /// ```no_run
    /// use rustcop::config::{Config, LintConfig};
    /// use std::path::Path;
    ///
    /// let config = Config::load(Path::new("rustcop.toml")).unwrap();
    /// let lint = config.get_nested_config::<LintConfig>(&["lints", "disallow_super_imports"]).unwrap();
    /// ```
    pub fn get_nested_config<'de, T>(&self, path: &[&str]) -> Result<T, Box<dyn std::error::Error>>
    where
        T: Deserialize<'de> + Default,
    {
        let mut current = &self.raw;
        for segment in path {
            match current.get(segment) {
                Some(value) => current = value,
                None => return Ok(T::default()),
            }
        }
        let config: T = current.clone().try_into()?;
        Ok(config)
    }

    /// Get whether warnings should be treated as errors.
    /// Returns the configured value, defaulting to false if not set.
    pub fn treat_warnings_as_errors(&self) -> bool {
        self.raw
            .get("treat_warnings_as_errors")
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
    }

    /// Get whether suppression directives must include justifications.
    /// Returns the configured value, defaulting to true if not set.
    pub fn require_suppression_justification(&self) -> bool {
        self.raw
            .get("require_suppression_justification")
            .and_then(|v| v.as_bool())
            .unwrap_or(true)
    }
}

/// Configuration for the imports section
#[derive(Debug, Deserialize, Clone)]
#[serde(default)]
pub struct ImportsConfig {
    pub group_imports: bool,
    pub preferred_import_order: Vec<String>,
    pub import_granularity: String,
    pub import_merge_behaviour: String,
    pub allowed_import_prefixes: Vec<String>,
}

impl Default for ImportsConfig {
    fn default() -> Self {
        ImportsConfig {
            group_imports: true,
            preferred_import_order: vec![
                "std".to_string(),
                "extern_crate".to_string(),
                "crate".to_string(),
                "self".to_string(),
                "super".to_string(),
            ],
            import_granularity: "crate".to_string(),
            import_merge_behaviour: "always".to_string(),
            allowed_import_prefixes: vec![
                "std".to_string(),
                "extern_crate".to_string(),
                "crate".to_string(),
                "self".to_string(),
            ],
        }
    }
}

/// Configuration for the modules section
#[derive(Debug, Deserialize, Clone)]
#[serde(default)]
pub struct ModulesConfig {
    pub severity: String,
    pub preferred_module_order: Vec<String>,
}

impl Default for ModulesConfig {
    fn default() -> Self {
        ModulesConfig {
            severity: "none".to_string(),
            preferred_module_order: vec![
                "local".to_string(),
                "crate".to_string(),
                "super".to_string(),
                "in_crate".to_string(),
            ],
        }
    }
}

/// Configuration for the exports section
#[derive(Debug, Deserialize, Clone)]
#[serde(default)]
pub struct ExportsConfig {
    pub severity: String,
    pub allowed_lib_exports: Vec<String>,
}

impl Default for ExportsConfig {
    fn default() -> Self {
        ExportsConfig {
            severity: "none".to_string(),
            allowed_lib_exports: Vec::new(),
        }
    }
}

/// Configuration for a lint rule
#[derive(Debug, Deserialize, Clone)]
#[serde(default)]
pub struct LintConfig {
    pub severity: String,
}

impl Default for LintConfig {
    fn default() -> Self {
        LintConfig {
            severity: "none".to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_config_with_extra_fields() {
        // TOML with extra fields that should be tolerated
        let toml_str = r#"
            [imports]
            group_imports = true
            import_granularity = "crate"
            some_future_field = "ignored"
            another_unknown_field = 42
        "#;

        let raw: toml::Value = toml::from_str(toml_str).unwrap();
        let config = Config {
            raw,
            config_dir: None,
        };

        // Should successfully deserialize despite extra fields
        let imports = config.get_config::<ImportsConfig>("imports").unwrap();
        assert!(imports.group_imports);
        assert_eq!(imports.import_granularity, "crate");
    }

    #[test]
    fn test_get_config_missing_section() {
        let config = Config::empty();

        // Should return default when section doesn't exist
        let imports = config.get_config::<ImportsConfig>("imports").unwrap();
        assert_eq!(imports.import_granularity, "crate");
    }

    #[test]
    fn test_get_config_partial_fields() {
        // TOML with only some fields specified
        let toml_str = r#"
            [imports]
            group_imports = false
        "#;

        let raw: toml::Value = toml::from_str(toml_str).unwrap();
        let config = Config {
            raw,
            config_dir: None,
        };

        let imports = config.get_config::<ImportsConfig>("imports").unwrap();
        assert!(!imports.group_imports);
        // Other fields should use defaults
        assert_eq!(imports.import_granularity, "crate");
    }

    #[test]
    fn test_get_nested_config() {
        let toml_str = r#"
            [lints.disallow_super_imports]
            severity = "error"
            
            [lints.another_rule]
            severity = "warning"
        "#;

        let raw: toml::Value = toml::from_str(toml_str).unwrap();
        let config = Config {
            raw,
            config_dir: None,
        };

        let lint = config
            .get_nested_config::<LintConfig>(&["lints", "disallow_super_imports"])
            .unwrap();
        assert_eq!(lint.severity, "error");

        let lint2 = config
            .get_nested_config::<LintConfig>(&["lints", "another_rule"])
            .unwrap();
        assert_eq!(lint2.severity, "warning");

        // Non-existent lint should return default
        let lint3 = config
            .get_nested_config::<LintConfig>(&["lints", "nonexistent"])
            .unwrap();
        assert_eq!(lint3.severity, "none");
    }

    #[test]
    fn test_merge_tables() {
        let base_toml = r#"
            [imports]
            group_imports = true
            import_granularity = "crate"
            
            [lints.rule1]
            severity = "warning"
        "#;

        let override_toml = r#"
            [imports]
            group_imports = false
            
            [lints.rule1]
            message = "Custom"
            
            [lints.rule2]
            severity = "error"
        "#;

        let mut base: toml::Value = toml::from_str(base_toml).unwrap();
        let override_val: toml::Value = toml::from_str(override_toml).unwrap();

        Config::merge_tables(&mut base, &override_val);

        let config = Config {
            raw: base,
            config_dir: None,
        };

        // Scalar should be replaced
        let imports = config.get_config::<ImportsConfig>("imports").unwrap();
        assert!(!imports.group_imports);
        // But unspecified values should remain
        assert_eq!(imports.import_granularity, "crate");

        // Tables should be merged recursively
        let rule1 = config
            .get_nested_config::<LintConfig>(&["lints", "rule1"])
            .unwrap();
        assert_eq!(rule1.severity, "warning"); // Original value preserved

        let rule2 = config
            .get_nested_config::<LintConfig>(&["lints", "rule2"])
            .unwrap();
        assert_eq!(rule2.severity, "error"); // New value added
    }

    #[test]
    fn test_version_validation() {
        let valid_toml = r#"
            version = 1
            [imports]
            group_imports = true
        "#;

        let raw: toml::Value = toml::from_str(valid_toml).unwrap();
        assert!(Config::validate_version(&raw).is_ok());

        let invalid_toml = r#"
            version = 2
            [imports]
            group_imports = true
        "#;

        let raw: toml::Value = toml::from_str(invalid_toml).unwrap();
        assert!(Config::validate_version(&raw).is_err());

        // Missing version should produce warning but not error
        let no_version_toml = r#"
            [imports]
            group_imports = true
        "#;

        let raw: toml::Value = toml::from_str(no_version_toml).unwrap();
        assert!(Config::validate_version(&raw).is_ok());
    }

    #[test]
    fn test_override_matching() {
        use std::path::Path;

        let override_config = Override {
            files: vec!["tests/**/*.rs".to_string(), "**/*_test.rs".to_string()],
            exclude: vec!["tests/ignored/**".to_string()],
            config: toml::Value::Table(Default::default()),
        };

        // Should match files in tests/
        assert!(Config::matches_override(&override_config, Path::new("tests/foo.rs")).unwrap());

        assert!(
            Config::matches_override(&override_config, Path::new("tests/subdir/bar.rs")).unwrap()
        );

        // Should match *_test.rs anywhere
        assert!(Config::matches_override(&override_config, Path::new("src/my_test.rs")).unwrap());

        // Should not match excluded paths
        assert!(
            !Config::matches_override(&override_config, Path::new("tests/ignored/foo.rs")).unwrap()
        );

        // Should not match non-matching paths
        assert!(!Config::matches_override(&override_config, Path::new("src/main.rs")).unwrap());
    }
}