rskiller 0.2.1

Find and clean Rust project build artifacts and caches with parallel processing
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
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::fs;

use crate::cli::{SortBy, Color};

/// Main configuration structure that mirrors CLI options
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    #[serde(default)]
    pub search: SearchConfig,
    #[serde(default)]
    pub output: OutputConfig,
    #[serde(default)]
    pub behavior: BehaviorConfig,
    #[serde(default)]
    pub safety: SafetyConfig,
    #[serde(default)]
    pub performance: PerformanceConfig,
    #[serde(default)]
    pub filters: FiltersConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchConfig {
    pub directory: Option<PathBuf>,
    pub full: Option<bool>,
    pub target: Option<String>,
    pub exclude: Option<Vec<String>>,
    pub exclude_hidden: Option<bool>,
    pub include_cargo_cache: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputConfig {
    pub sort: Option<SortBy>,
    pub gb: Option<bool>,
    pub color: Option<Color>,
    pub hide_errors: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BehaviorConfig {
    pub dry_run: Option<bool>,
    pub delete_all: Option<bool>,
    pub list_only: Option<bool>,
    pub no_check_update: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyConfig {
    pub confirm_large_deletions: Option<bool>,
    pub large_deletion_threshold: Option<String>,
    pub backup_before_delete: Option<bool>,
    pub max_concurrent_deletions: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    pub enable_parallel_scanning: Option<bool>,
    pub max_parallel_threads: Option<u32>,
    pub chunk_size: Option<u32>,
    pub enable_parallel_cleanup: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FiltersConfig {
    pub min_age_days: Option<u32>,
    pub max_age_days: Option<u32>,
    pub min_size: Option<String>,
    pub ignore_active_projects: Option<bool>,
    pub active_threshold_hours: Option<u32>,
}

impl Default for SearchConfig {
    fn default() -> Self {
        Self {
            directory: None,
            full: None,
            target: None,
            exclude: None,
            exclude_hidden: None,
            include_cargo_cache: None,
        }
    }
}

impl Default for OutputConfig {
    fn default() -> Self {
        Self {
            sort: None,
            gb: None,
            color: None,
            hide_errors: None,
        }
    }
}

impl Default for BehaviorConfig {
    fn default() -> Self {
        Self {
            dry_run: None,
            delete_all: None,
            list_only: None,
            no_check_update: None,
        }
    }
}

impl Default for SafetyConfig {
    fn default() -> Self {
        Self {
            confirm_large_deletions: None,
            large_deletion_threshold: None,
            backup_before_delete: None,
            max_concurrent_deletions: None,
        }
    }
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            enable_parallel_scanning: None,
            max_parallel_threads: None,
            chunk_size: None,
            enable_parallel_cleanup: None,
        }
    }
}

impl Default for FiltersConfig {
    fn default() -> Self {
        Self {
            min_age_days: None,
            max_age_days: None,
            min_size: None,
            ignore_active_projects: None,
            active_threshold_hours: None,
        }
    }
}

/// Configuration loader that handles multiple file formats and locations
pub struct ConfigLoader {
    pub search_paths: Vec<PathBuf>,
}

impl ConfigLoader {
    /// Create a new config loader with default search paths
    pub fn new() -> Self {
        let mut search_paths = Vec::new();
        
        // Current directory configs (highest priority)
        search_paths.push(PathBuf::from("./rskill.toml"));
        search_paths.push(PathBuf::from("./.rskillrc"));
        
        // User config directory
        if let Some(config_dir) = dirs::config_dir() {
            search_paths.push(config_dir.join("rskiller").join("rskill.toml"));
        }
        
        // User home directory root
        if let Some(home_dir) = home::home_dir() {
            search_paths.push(home_dir.join("rskill.toml"));
            search_paths.push(home_dir.join(".rskillrc"));
        }
        
        Self { search_paths }
    }
    
    /// Create a config loader with custom search paths
    pub fn with_paths(paths: Vec<PathBuf>) -> Self {
        Self { search_paths: paths }
    }
    
    /// Load configuration from the first available config file
    pub fn load_config(&self) -> Result<Config> {
        for path in &self.search_paths {
            if path.exists() {
                return self.load_from_file(path)
                    .with_context(|| format!("Failed to load config from {}", path.display()));
            }
        }
        
        // No config file found, return default
        Ok(Config::default())
    }
    
    /// Load configuration from a specific file
    pub fn load_from_file(&self, path: &Path) -> Result<Config> {
        let content = fs::read_to_string(path)
            .with_context(|| format!("Failed to read config file: {}", path.display()))?;
        
        match path.extension().and_then(|ext| ext.to_str()) {
            Some("toml") => {
                toml::from_str(&content)
                    .with_context(|| format!("Failed to parse TOML config: {}", path.display()))
            }
            _ => {
                // Assume JSON for .rskillrc and unknown extensions
                serde_json::from_str(&content)
                    .with_context(|| format!("Failed to parse JSON config: {}", path.display()))
            }
        }
    }
    
    /// Load and merge multiple configuration files
    pub fn load_merged_config(&self) -> Result<Config> {
        let mut final_config = Config::default();
        
        // Load configs in reverse priority order (lowest to highest)
        for path in self.search_paths.iter().rev() {
            if path.exists() {
                let config = self.load_from_file(path)?;
                final_config = self.merge_configs(final_config, config);
            }
        }
        
        Ok(final_config)
    }
    
    /// Merge two configurations, with the second one overriding the first
    pub fn merge_configs(&self, base: Config, override_config: Config) -> Config {
        Config {
            search: SearchConfig {
                directory: override_config.search.directory.or(base.search.directory),
                full: override_config.search.full.or(base.search.full),
                target: override_config.search.target.or(base.search.target),
                exclude: override_config.search.exclude.or(base.search.exclude),
                exclude_hidden: override_config.search.exclude_hidden.or(base.search.exclude_hidden),
                include_cargo_cache: override_config.search.include_cargo_cache.or(base.search.include_cargo_cache),
            },
            output: OutputConfig {
                sort: override_config.output.sort.or(base.output.sort),
                gb: override_config.output.gb.or(base.output.gb),
                color: override_config.output.color.or(base.output.color),
                hide_errors: override_config.output.hide_errors.or(base.output.hide_errors),
            },
            behavior: BehaviorConfig {
                dry_run: override_config.behavior.dry_run.or(base.behavior.dry_run),
                delete_all: override_config.behavior.delete_all.or(base.behavior.delete_all),
                list_only: override_config.behavior.list_only.or(base.behavior.list_only),
                no_check_update: override_config.behavior.no_check_update.or(base.behavior.no_check_update),
            },
            safety: SafetyConfig {
                confirm_large_deletions: override_config.safety.confirm_large_deletions.or(base.safety.confirm_large_deletions),
                large_deletion_threshold: override_config.safety.large_deletion_threshold.or(base.safety.large_deletion_threshold),
                backup_before_delete: override_config.safety.backup_before_delete.or(base.safety.backup_before_delete),
                max_concurrent_deletions: override_config.safety.max_concurrent_deletions.or(base.safety.max_concurrent_deletions),
            },
            performance: PerformanceConfig {
                enable_parallel_scanning: override_config.performance.enable_parallel_scanning.or(base.performance.enable_parallel_scanning),
                max_parallel_threads: override_config.performance.max_parallel_threads.or(base.performance.max_parallel_threads),
                chunk_size: override_config.performance.chunk_size.or(base.performance.chunk_size),
                enable_parallel_cleanup: override_config.performance.enable_parallel_cleanup.or(base.performance.enable_parallel_cleanup),
            },
            filters: FiltersConfig {
                min_age_days: override_config.filters.min_age_days.or(base.filters.min_age_days),
                max_age_days: override_config.filters.max_age_days.or(base.filters.max_age_days),
                min_size: override_config.filters.min_size.or(base.filters.min_size),
                ignore_active_projects: override_config.filters.ignore_active_projects.or(base.filters.ignore_active_projects),
                active_threshold_hours: override_config.filters.active_threshold_hours.or(base.filters.active_threshold_hours),
            },
        }
    }
    
    /// Generate a sample configuration file
    pub fn generate_sample_config(format: ConfigFormat) -> Result<String> {
        let sample_config = Config {
            search: SearchConfig {
                directory: Some(PathBuf::from("~/")),
                full: Some(false),
                target: Some("target".to_string()),
                exclude: Some(vec!["node_modules".to_string(), "vendor".to_string(), ".git".to_string()]),
                exclude_hidden: Some(true),
                include_cargo_cache: Some(false),
            },
            output: OutputConfig {
                sort: Some(SortBy::Size),
                gb: Some(false),
                color: Some(Color::Blue),
                hide_errors: Some(false),
            },
            behavior: BehaviorConfig {
                dry_run: Some(false),
                delete_all: Some(false),
                list_only: Some(false),
                no_check_update: Some(false),
            },
            safety: SafetyConfig {
                confirm_large_deletions: Some(true),
                large_deletion_threshold: Some("1GB".to_string()),
                backup_before_delete: Some(false),
                max_concurrent_deletions: Some(3),
            },
            performance: PerformanceConfig {
                enable_parallel_scanning: Some(true),
                max_parallel_threads: Some(0), // 0 = auto-detect
                chunk_size: Some(1000),
                enable_parallel_cleanup: Some(true),
            },
            filters: FiltersConfig {
                min_age_days: Some(0),
                max_age_days: Some(365),
                min_size: Some("1MB".to_string()),
                ignore_active_projects: Some(true),
                active_threshold_hours: Some(24),
            },
        };
        
        match format {
            ConfigFormat::Toml => {
                toml::to_string_pretty(&sample_config)
                    .context("Failed to serialize sample TOML config")
            }
            ConfigFormat::Json => {
                serde_json::to_string_pretty(&sample_config)
                    .context("Failed to serialize sample JSON config")
            }
        }
    }
}

impl Default for ConfigLoader {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone, Copy)]
pub enum ConfigFormat {
    Toml,
    Json,
}

impl std::str::FromStr for ConfigFormat {
    type Err = anyhow::Error;
    
    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "toml" => Ok(ConfigFormat::Toml),
            "json" => Ok(ConfigFormat::Json),
            _ => Err(anyhow::anyhow!("Invalid config format. Supported formats: toml, json")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;
    use std::fs::write;

    #[test]
    fn test_load_toml_config() {
        let temp_dir = tempdir().unwrap();
        let config_path = temp_dir.path().join("rskill.toml");
        
        let toml_content = r#"
        [search]
        directory = "/test/path"
        full = true
        target = "build"
        
        [output]
        sort = "Path"
        gb = true
        "#;
        
        write(&config_path, toml_content).unwrap();
        
        let loader = ConfigLoader::with_paths(vec![config_path]);
        let config = loader.load_config().unwrap();
        
        assert_eq!(config.search.directory, Some(PathBuf::from("/test/path")));
        assert_eq!(config.search.full, Some(true));
        assert_eq!(config.search.target, Some("build".to_string()));
        assert_eq!(config.output.sort, Some(SortBy::Path));
        assert_eq!(config.output.gb, Some(true));
    }
    
    #[test]
    fn test_load_json_config() {
        let temp_dir = tempdir().unwrap();
        let config_path = temp_dir.path().join(".rskillrc");
        
        let json_content = r#"
        {
            "search": {
                "directory": "/test/path",
                "full": false,
                "exclude": ["node_modules", "target"]
            },
            "output": {
                "color": "Red"
            }
        }
        "#;
        
        write(&config_path, json_content).unwrap();
        
        let loader = ConfigLoader::with_paths(vec![config_path]);
        let config = loader.load_config().unwrap();
        
        assert_eq!(config.search.directory, Some(PathBuf::from("/test/path")));
        assert_eq!(config.search.full, Some(false));
        assert_eq!(config.search.exclude, Some(vec!["node_modules".to_string(), "target".to_string()]));
        assert_eq!(config.output.color, Some(Color::Red));
    }
    
    #[test]
    fn test_config_merging() {
        let loader = ConfigLoader::new();
        
        let base_config = Config {
            search: SearchConfig {
                directory: Some(PathBuf::from("/base")),
                full: Some(false),
                target: Some("target".to_string()),
                exclude: None,
                exclude_hidden: None,
                include_cargo_cache: None,
            },
            output: OutputConfig {
                sort: Some(SortBy::Size),
                gb: Some(false),
                color: Some(Color::Blue),
                hide_errors: None,
            },
            ..Default::default()
        };
        
        let override_config = Config {
            search: SearchConfig {
                directory: Some(PathBuf::from("/override")),
                full: Some(true),
                target: None, // Should keep base value
                exclude: Some(vec!["test".to_string()]),
                exclude_hidden: None,
                include_cargo_cache: None,
            },
            output: OutputConfig {
                sort: None, // Should keep base value
                gb: Some(true),
                color: None, // Should keep base value
                hide_errors: Some(true),
            },
            ..Default::default()
        };
        
        let merged = loader.merge_configs(base_config, override_config);
        
        // Overridden values
        assert_eq!(merged.search.directory, Some(PathBuf::from("/override")));
        assert_eq!(merged.search.full, Some(true));
        assert_eq!(merged.search.exclude, Some(vec!["test".to_string()]));
        assert_eq!(merged.output.gb, Some(true));
        assert_eq!(merged.output.hide_errors, Some(true));
        
        // Preserved base values
        assert_eq!(merged.search.target, Some("target".to_string()));
        assert_eq!(merged.output.sort, Some(SortBy::Size));
        assert_eq!(merged.output.color, Some(Color::Blue));
    }
    
    #[test]
    fn test_default_config() {
        let loader = ConfigLoader::with_paths(vec![]);
        let config = loader.load_config().unwrap();
        
        // All fields should be None/default
        assert_eq!(config.search.directory, None);
        assert_eq!(config.search.full, None);
        assert_eq!(config.output.sort, None);
    }
    
    #[test]
    fn test_sample_config_generation() {
        let toml_config = ConfigLoader::generate_sample_config(ConfigFormat::Toml).unwrap();
        let json_config = ConfigLoader::generate_sample_config(ConfigFormat::Json).unwrap();
        
        assert!(toml_config.contains("[search]"));
        assert!(toml_config.contains("directory = \"~/\""));
        
        assert!(json_config.contains("\"search\""));
        assert!(json_config.contains("\"directory\": \"~/\""));
    }
}