Skip to main content

ryo_mutations/idiom/
organize_imports.rs

1//! OrganizeImportsMutation: Sort and group use statements idiomatically
2//!
3//! Groups:
4//! 1. `std` / `core` / `alloc` (standard library)
5//! 2. External crates
6//! 3. `crate::` / `super::` / `self::` (local)
7//!
8//! Within each group, sorted alphabetically.
9
10use crate::Mutation;
11
12/// Organize and sort use statements
13#[derive(Debug, Clone, Default)]
14pub struct OrganizeImportsMutation {
15    /// Remove duplicate imports
16    pub deduplicate: bool,
17    /// Merge imports with same prefix into groups
18    pub merge_groups: bool,
19}
20
21impl OrganizeImportsMutation {
22    pub fn new() -> Self {
23        Self {
24            deduplicate: true,
25            merge_groups: true,
26        }
27    }
28
29    pub fn with_deduplicate(mut self, deduplicate: bool) -> Self {
30        self.deduplicate = deduplicate;
31        self
32    }
33
34    pub fn with_merge_groups(mut self, merge: bool) -> Self {
35        self.merge_groups = merge;
36        self
37    }
38}
39
40impl Mutation for OrganizeImportsMutation {
41    fn describe(&self) -> String {
42        "Organize imports: sort and group use statements".to_string()
43    }
44
45    fn mutation_type(&self) -> &'static str {
46        "OrganizeImports"
47    }
48
49    fn box_clone(&self) -> Box<dyn Mutation> {
50        Box::new(self.clone())
51    }
52}