chaser 0.1.0

An automated file path synchronization tool that updates changed paths in configuration files in real time.
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
use anyhow::{Context, Result};
use serde_json::Value as JsonValue;
use serde_yaml_ng::Value as YamlValue;
use std::fs;
use std::path::{Path, PathBuf};
use toml::Value as TomlValue;

#[derive(Debug, Clone, PartialEq)]
pub enum TargetFileFormat {
    Json,
    Yaml,
    Toml,
    Csv,
}

impl TargetFileFormat {
    pub fn from_path(path: &Path) -> Result<Self> {
        match path.extension().and_then(|s| s.to_str()) {
            Some("json") => Ok(Self::Json),
            Some("yaml") | Some("yml") => Ok(Self::Yaml),
            Some("toml") => Ok(Self::Toml),
            Some("csv") => Ok(Self::Csv),
            _ => anyhow::bail!("Unsupported file format for: {:?}", path),
        }
    }
}

#[derive(Debug, Clone)]
pub struct PathEntry {
    pub path: String,
    pub exists: bool,
    pub last_known_path: Option<String>,
}

#[derive(Debug, Clone)]
pub struct TargetFile {
    pub path: PathBuf,
    pub format: TargetFileFormat,
    pub paths: Vec<PathEntry>,
}

impl TargetFile {
    pub fn new(path: PathBuf) -> Result<Self> {
        let format = TargetFileFormat::from_path(&path)?;
        let paths = Self::extract_paths(&path, &format)?;

        Ok(Self {
            path,
            format,
            paths,
        })
    }

    /// Extract all paths from the target file
    fn extract_paths(file_path: &Path, format: &TargetFileFormat) -> Result<Vec<PathEntry>> {
        if !file_path.exists() {
            return Ok(Vec::new());
        }

        let content = fs::read_to_string(file_path)
            .with_context(|| format!("Failed to read file: {:?}", file_path))?;

        match format {
            TargetFileFormat::Json => Self::extract_paths_from_json(&content),
            TargetFileFormat::Yaml => Self::extract_paths_from_yaml(&content),
            TargetFileFormat::Toml => Self::extract_paths_from_toml(&content),
            TargetFileFormat::Csv => Self::extract_paths_from_csv(&content),
        }
    }

    fn extract_paths_from_json(content: &str) -> Result<Vec<PathEntry>> {
        let value: JsonValue = serde_json::from_str(content)?;
        let mut paths = Vec::new();
        Self::collect_paths_from_json_value(&value, &mut paths);
        Ok(paths
            .into_iter()
            .map(|p| PathEntry {
                path: p.clone(),
                exists: Path::new(&p).exists(),
                last_known_path: None,
            })
            .collect())
    }

    fn collect_paths_from_json_value(value: &JsonValue, paths: &mut Vec<String>) {
        match value {
            JsonValue::String(s) => {
                if Self::looks_like_path(s) {
                    paths.push(s.clone());
                }
            }
            JsonValue::Array(arr) => {
                for item in arr {
                    Self::collect_paths_from_json_value(item, paths);
                }
            }
            JsonValue::Object(obj) => {
                for (_, v) in obj {
                    Self::collect_paths_from_json_value(v, paths);
                }
            }
            _ => {}
        }
    }

    fn extract_paths_from_yaml(content: &str) -> Result<Vec<PathEntry>> {
        let value: YamlValue = serde_yaml_ng::from_str(content)?;
        let mut paths = Vec::new();
        Self::collect_paths_from_yaml_value(&value, &mut paths);
        Ok(paths
            .into_iter()
            .map(|p| PathEntry {
                path: p.clone(),
                exists: Path::new(&p).exists(),
                last_known_path: None,
            })
            .collect())
    }

    fn collect_paths_from_yaml_value(value: &YamlValue, paths: &mut Vec<String>) {
        match value {
            YamlValue::String(s) => {
                if Self::looks_like_path(s) {
                    paths.push(s.clone());
                }
            }
            YamlValue::Sequence(seq) => {
                for item in seq {
                    Self::collect_paths_from_yaml_value(item, paths);
                }
            }
            YamlValue::Mapping(map) => {
                for (_, v) in map {
                    Self::collect_paths_from_yaml_value(v, paths);
                }
            }
            _ => {}
        }
    }

    fn extract_paths_from_toml(content: &str) -> Result<Vec<PathEntry>> {
        let value: TomlValue = toml::from_str(content)?;
        let mut paths = Vec::new();
        Self::collect_paths_from_toml_value(&value, &mut paths);
        Ok(paths
            .into_iter()
            .map(|p| PathEntry {
                path: p.clone(),
                exists: Path::new(&p).exists(),
                last_known_path: None,
            })
            .collect())
    }

    fn collect_paths_from_toml_value(value: &TomlValue, paths: &mut Vec<String>) {
        match value {
            TomlValue::String(s) => {
                if Self::looks_like_path(s) {
                    paths.push(s.clone());
                }
            }
            TomlValue::Array(arr) => {
                for item in arr {
                    Self::collect_paths_from_toml_value(item, paths);
                }
            }
            TomlValue::Table(table) => {
                for (_, v) in table {
                    Self::collect_paths_from_toml_value(v, paths);
                }
            }
            _ => {}
        }
    }

    fn extract_paths_from_csv(content: &str) -> Result<Vec<PathEntry>> {
        let mut reader = csv::Reader::from_reader(content.as_bytes());
        let mut paths = Vec::new();

        for result in reader.records() {
            let record = result?;
            for field in record.iter() {
                if Self::looks_like_path(field) {
                    paths.push(field.to_string());
                }
            }
        }

        Ok(paths
            .into_iter()
            .map(|p| PathEntry {
                path: p.clone(),
                exists: Path::new(&p).exists(),
                last_known_path: None,
            })
            .collect())
    }

    /// Check if a string looks like a file/directory path
    fn looks_like_path(s: &str) -> bool {
        if s.is_empty() {
            return false;
        }

        // Check for common path patterns
        s.contains('/')
            || s.contains('\\')
            || s.starts_with("./")
            || s.starts_with("../")
            || s.starts_with("~/")
            || s.starts_with('/')
            || (cfg!(windows) && s.len() > 2 && s.chars().nth(1) == Some(':'))
    }

    /// Update a path in the target file
    pub fn update_path(&mut self, old_path: &str, new_path: &str) -> Result<()> {
        // Update internal path tracking
        for entry in &mut self.paths {
            if entry.path == old_path {
                entry.last_known_path = Some(entry.path.clone());
                entry.path = new_path.to_string();
                entry.exists = Path::new(new_path).exists();
            }
        }

        // Update the actual file content
        self.update_file_content(old_path, new_path)
    }

    fn update_file_content(&self, old_path: &str, new_path: &str) -> Result<()> {
        if !self.path.exists() {
            return Ok(());
        }

        let content = fs::read_to_string(&self.path)?;

        let updated_content = match self.format {
            TargetFileFormat::Json => self.update_json_content(&content, old_path, new_path)?,
            TargetFileFormat::Yaml => self.update_yaml_content(&content, old_path, new_path)?,
            TargetFileFormat::Toml => self.update_toml_content(&content, old_path, new_path)?,
            TargetFileFormat::Csv => self.update_csv_content(&content, old_path, new_path)?,
        };

        fs::write(&self.path, updated_content)?;
        Ok(())
    }

    fn update_json_content(&self, content: &str, old_path: &str, new_path: &str) -> Result<String> {
        let mut value: JsonValue = serde_json::from_str(content)?;
        Self::update_json_value(&mut value, old_path, new_path);
        Ok(serde_json::to_string_pretty(&value)?)
    }

    fn update_json_value(value: &mut JsonValue, old_path: &str, new_path: &str) {
        match value {
            JsonValue::String(s) => {
                if s == old_path {
                    *s = new_path.to_string();
                }
            }
            JsonValue::Array(arr) => {
                for item in arr {
                    Self::update_json_value(item, old_path, new_path);
                }
            }
            JsonValue::Object(obj) => {
                for (_, v) in obj {
                    Self::update_json_value(v, old_path, new_path);
                }
            }
            _ => {}
        }
    }

    fn update_yaml_content(&self, content: &str, old_path: &str, new_path: &str) -> Result<String> {
        let mut value: YamlValue = serde_yaml_ng::from_str(content)?;
        Self::update_yaml_value(&mut value, old_path, new_path);
        Ok(serde_yaml_ng::to_string(&value)?)
    }

    fn update_yaml_value(value: &mut YamlValue, old_path: &str, new_path: &str) {
        match value {
            YamlValue::String(s) => {
                if s == old_path {
                    *s = new_path.to_string();
                }
            }
            YamlValue::Sequence(seq) => {
                for item in seq {
                    Self::update_yaml_value(item, old_path, new_path);
                }
            }
            YamlValue::Mapping(map) => {
                for (_, v) in map {
                    Self::update_yaml_value(v, old_path, new_path);
                }
            }
            _ => {}
        }
    }

    fn update_toml_content(&self, content: &str, old_path: &str, new_path: &str) -> Result<String> {
        let mut value: TomlValue = toml::from_str(content)?;
        Self::update_toml_value(&mut value, old_path, new_path);
        Ok(toml::to_string_pretty(&value)?)
    }

    fn update_toml_value(value: &mut TomlValue, old_path: &str, new_path: &str) {
        match value {
            TomlValue::String(s) => {
                if s == old_path {
                    *s = new_path.to_string();
                }
            }
            TomlValue::Array(arr) => {
                for item in arr {
                    Self::update_toml_value(item, old_path, new_path);
                }
            }
            TomlValue::Table(table) => {
                for (_, v) in table {
                    Self::update_toml_value(v, old_path, new_path);
                }
            }
            _ => {}
        }
    }

    fn update_csv_content(&self, content: &str, old_path: &str, new_path: &str) -> Result<String> {
        let lines: Vec<&str> = content.lines().collect();
        if lines.is_empty() {
            return Ok(content.to_string());
        }

        let mut updated_lines = Vec::new();
        updated_lines.push(lines[0].to_string()); // Keep header

        for line in &lines[1..] {
            if line.starts_with(old_path) {
                // Replace the path at the beginning of the line
                let remaining = &line[old_path.len()..];
                updated_lines.push(format!("{}{}", new_path, remaining));
            } else {
                updated_lines.push(line.to_string());
            }
        }

        Ok(updated_lines.join("\n") + "\n")
    }

    /// Mark a path as deleted (but keep tracking it)
    pub fn mark_path_deleted(&mut self, path: &str) -> Result<()> {
        for entry in &mut self.paths {
            if entry.path == path {
                entry.exists = false;
            }
        }
        Ok(())
    }

    /// Mark a path as restored
    pub fn mark_path_restored(&mut self, path: &str) -> Result<()> {
        for entry in &mut self.paths {
            if entry.path == path {
                entry.exists = true;
            }
        }
        Ok(())
    }
}

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

    #[test]
    fn test_target_file_format_detection() {
        assert_eq!(
            TargetFileFormat::from_path(Path::new("test.json")).unwrap(),
            TargetFileFormat::Json
        );
        assert_eq!(
            TargetFileFormat::from_path(Path::new("test.yaml")).unwrap(),
            TargetFileFormat::Yaml
        );
        assert_eq!(
            TargetFileFormat::from_path(Path::new("test.yml")).unwrap(),
            TargetFileFormat::Yaml
        );
        assert_eq!(
            TargetFileFormat::from_path(Path::new("test.toml")).unwrap(),
            TargetFileFormat::Toml
        );
        assert_eq!(
            TargetFileFormat::from_path(Path::new("test.csv")).unwrap(),
            TargetFileFormat::Csv
        );
        assert!(TargetFileFormat::from_path(Path::new("test.txt")).is_err()); // Unsupported format
    }

    #[test]
    fn test_looks_like_path() {
        assert!(TargetFile::looks_like_path("./test_files/test.txt"));
        assert!(TargetFile::looks_like_path("/home/user/test.txt"));
        assert!(TargetFile::looks_like_path("../relative/path"));
        assert!(TargetFile::looks_like_path("simple/path"));
        assert!(TargetFile::looks_like_path("C:\\Windows\\System32"));
        assert!(!TargetFile::looks_like_path("not a path"));
        assert!(!TargetFile::looks_like_path("123456"));
        assert!(!TargetFile::looks_like_path("config_option"));
    }

    #[test]
    fn test_extract_paths_from_json() {
        let json_content = r#"[
            "./test_files/file1.txt",
            "./test_files/dir",
            "not a path",
            "/absolute/path"
        ]"#;

        let paths = TargetFile::extract_paths_from_json(json_content).unwrap();
        assert_eq!(paths.len(), 3);
        assert!(paths.iter().any(|p| p.path == "./test_files/file1.txt"));
        assert!(paths.iter().any(|p| p.path == "./test_files/dir"));
        assert!(paths.iter().any(|p| p.path == "/absolute/path"));
    }

    #[test]
    fn test_extract_paths_from_yaml() {
        let yaml_content = r#"
paths:
  - "./test_files/file1.txt"
  - "./test_files/dir"
  - "/absolute/path"
other_field: "value"
"#;

        let paths = TargetFile::extract_paths_from_yaml(yaml_content).unwrap();
        assert_eq!(paths.len(), 3);
        assert!(paths.iter().any(|p| p.path == "./test_files/file1.txt"));
        assert!(paths.iter().any(|p| p.path == "./test_files/dir"));
        assert!(paths.iter().any(|p| p.path == "/absolute/path"));
    }

    #[test]
    fn test_extract_paths_from_toml() {
        let toml_content = r#"
paths = ["./test_files/file1.txt", "./test_files/dir", "/absolute/path"]
other_field = "value"
"#;

        let paths = TargetFile::extract_paths_from_toml(toml_content).unwrap();
        assert_eq!(paths.len(), 3);
        assert!(paths.iter().any(|p| p.path == "./test_files/file1.txt"));
        assert!(paths.iter().any(|p| p.path == "./test_files/dir"));
        assert!(paths.iter().any(|p| p.path == "/absolute/path"));
    }

    #[test]
    fn test_extract_paths_from_csv() {
        let csv_content = r#"path,type,description
./test_files/file1.txt,file,Test file
./test_files/dir,directory,Test directory
/absolute/path,file,Absolute path
"#;

        let paths = TargetFile::extract_paths_from_csv(csv_content).unwrap();
        assert_eq!(paths.len(), 3);
        assert!(paths.iter().any(|p| p.path == "./test_files/file1.txt"));
        assert!(paths.iter().any(|p| p.path == "./test_files/dir"));
        assert!(paths.iter().any(|p| p.path == "/absolute/path"));
    }

    #[test]
    fn test_json_file_path_update() {
        let temp_dir = TempDir::new().unwrap();
        let json_file = temp_dir.path().join("test.json");

        let initial_content = r#"["./test_files/old_path", "./test_files/keep_path"]"#;
        fs::write(&json_file, initial_content).unwrap();

        let mut target_file = TargetFile::new(json_file.clone()).unwrap();
        target_file
            .update_path("./test_files/old_path", "./test_files/new_path")
            .unwrap();

        let updated_content = fs::read_to_string(&json_file).unwrap();
        assert!(updated_content.contains("./test_files/new_path"));
        assert!(updated_content.contains("./test_files/keep_path"));
        assert!(!updated_content.contains("./test_files/old_path"));
    }

    #[test]
    fn test_yaml_file_path_update() {
        let temp_dir = TempDir::new().unwrap();
        let yaml_file = temp_dir.path().join("test.yaml");

        let initial_content = r#"paths:
  - "./test_files/old_path"
  - "./test_files/keep_path"
other_field: "value"
"#;
        fs::write(&yaml_file, initial_content).unwrap();

        let mut target_file = TargetFile::new(yaml_file.clone()).unwrap();
        target_file
            .update_path("./test_files/old_path", "./test_files/new_path")
            .unwrap();

        let updated_content = fs::read_to_string(&yaml_file).unwrap();

        assert!(updated_content.contains("./test_files/new_path"));
        assert!(updated_content.contains("./test_files/keep_path"));
        assert!(!updated_content.contains("./test_files/old_path"));
        assert!(updated_content.contains("other_field")); // YAML formatting might change quotes
    }

    #[test]
    fn test_toml_file_path_update() {
        let temp_dir = TempDir::new().unwrap();
        let toml_file = temp_dir.path().join("test.toml");

        let initial_content = r#"paths = ["./test_files/old_path", "./test_files/keep_path"]
other_field = "value"
"#;
        fs::write(&toml_file, initial_content).unwrap();

        let mut target_file = TargetFile::new(toml_file.clone()).unwrap();
        target_file
            .update_path("./test_files/old_path", "./test_files/new_path")
            .unwrap();

        let updated_content = fs::read_to_string(&toml_file).unwrap();
        assert!(updated_content.contains("./test_files/new_path"));
        assert!(updated_content.contains("./test_files/keep_path"));
        assert!(!updated_content.contains("./test_files/old_path"));
        assert!(updated_content.contains("other_field = \"value\""));
    }

    #[test]
    fn test_csv_file_path_update() {
        let temp_dir = TempDir::new().unwrap();
        let csv_file = temp_dir.path().join("test.csv");

        let initial_content = r#"path,type,description
./test_files/old_path,file,Old file
./test_files/keep_path,directory,Keep this
"#;
        fs::write(&csv_file, initial_content).unwrap();

        let mut target_file = TargetFile::new(csv_file.clone()).unwrap();
        target_file
            .update_path("./test_files/old_path", "./test_files/new_path")
            .unwrap();

        let updated_content = fs::read_to_string(&csv_file).unwrap();
        assert!(updated_content.contains("./test_files/new_path"));
        assert!(updated_content.contains("./test_files/keep_path"));
        assert!(!updated_content.contains("./test_files/old_path"));
        assert!(updated_content.contains("path,type,description"));
    }

    #[test]
    fn test_complex_path_scenarios() {
        let temp_dir = TempDir::new().unwrap();

        // Test with paths that are substrings of each other
        let json_file = temp_dir.path().join("test.json");
        let initial_content =
            r#"["./test_files/path", "./test_files/path_extended", "./test_files/other"]"#;
        fs::write(&json_file, initial_content).unwrap();

        let mut target_file = TargetFile::new(json_file.clone()).unwrap();
        target_file
            .update_path("./test_files/path", "./test_files/renamed")
            .unwrap();

        let updated_content = fs::read_to_string(&json_file).unwrap();
        assert!(updated_content.contains("./test_files/renamed"));
        assert!(updated_content.contains("./test_files/path_extended")); // Should not be changed
        assert!(updated_content.contains("./test_files/other"));
        assert!(!updated_content.contains("\"./test_files/path\"")); // Exact match should be gone
    }

    #[test]
    fn test_mixed_file_formats() {
        let temp_dir = TempDir::new().unwrap();

        // Create files in different formats with same paths
        let json_file = temp_dir.path().join("test.json");
        let yaml_file = temp_dir.path().join("test.yaml");
        let toml_file = temp_dir.path().join("test.toml");
        let csv_file = temp_dir.path().join("test.csv");

        fs::write(&json_file, r#"["./test_files/shared_path"]"#).unwrap();
        fs::write(&yaml_file, "paths:\n  - \"./test_files/shared_path\"").unwrap();
        fs::write(&toml_file, "paths = [\"./test_files/shared_path\"]").unwrap();
        fs::write(&csv_file, "path,type\n./test_files/shared_path,file").unwrap();

        let mut json_target = TargetFile::new(json_file.clone()).unwrap();
        let mut yaml_target = TargetFile::new(yaml_file.clone()).unwrap();
        let mut toml_target = TargetFile::new(toml_file.clone()).unwrap();
        let mut csv_target = TargetFile::new(csv_file.clone()).unwrap();

        // Update all files
        json_target
            .update_path("./test_files/shared_path", "./test_files/updated_path")
            .unwrap();
        yaml_target
            .update_path("./test_files/shared_path", "./test_files/updated_path")
            .unwrap();
        toml_target
            .update_path("./test_files/shared_path", "./test_files/updated_path")
            .unwrap();
        csv_target
            .update_path("./test_files/shared_path", "./test_files/updated_path")
            .unwrap();

        // Verify all formats were updated
        let json_content = fs::read_to_string(&json_file).unwrap();
        let yaml_content = fs::read_to_string(&yaml_file).unwrap();
        let toml_content = fs::read_to_string(&toml_file).unwrap();
        let csv_content = fs::read_to_string(&csv_file).unwrap();

        assert!(json_content.contains("./test_files/updated_path"));
        assert!(yaml_content.contains("./test_files/updated_path"));
        assert!(toml_content.contains("./test_files/updated_path"));
        assert!(csv_content.contains("./test_files/updated_path"));

        assert!(!json_content.contains("./test_files/shared_path"));
        assert!(!yaml_content.contains("./test_files/shared_path"));
        assert!(!toml_content.contains("./test_files/shared_path"));
        assert!(!csv_content.contains("./test_files/shared_path"));
    }
}