smop 0.2.1

Batteries-included scripting utilities for Rust
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! File system utilities.
//!
//! Convenient functions for common file operations like reading,
//! writing, and JSON/TOML serialization.

use std::fs::{self, OpenOptions};
use std::io::Write as IoWrite;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use serde::{Serialize, de::DeserializeOwned};

// Re-export TempDir for convenience
pub use tempfile::TempDir;

/// Reads a file's entire contents as a string.
///
/// # Errors
///
/// Returns an error if the file doesn't exist or can't be read.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// let content = fs::read_string("config.txt")?;
/// println!("{}", content);
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn read_string<P: AsRef<Path>>(path: P) -> Result<String> {
    let path = path.as_ref();
    fs::read_to_string(path).with_context(|| format!("Failed to read file: {}", path.display()))
}

/// Writes a string to a file, creating it if it doesn't exist.
///
/// Parent directories are NOT created automatically.
///
/// # Errors
///
/// Returns an error if the file can't be written.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// fs::write_string("output.txt", "Hello, world!")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn write_string<P: AsRef<Path>, C: AsRef<str>>(path: P, content: C) -> Result<()> {
    let path = path.as_ref();
    fs::write(path, content.as_ref())
        .with_context(|| format!("Failed to write file: {}", path.display()))
}

/// Reads and deserializes JSON from a file.
///
/// # Errors
///
/// Returns an error if the file doesn't exist or contains invalid JSON.
///
/// # Examples
///
/// ```no_run
/// use serde::Deserialize;
/// use smop::fs;
///
/// #[derive(Deserialize)]
/// struct Config {
///     name: String,
/// }
///
/// let config: Config = fs::read_json("config.json")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn read_json<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> {
    let path = path.as_ref();
    let content = read_string(path)?;
    serde_json::from_str(&content)
        .with_context(|| format!("Failed to parse JSON from: {}", path.display()))
}

/// Serializes and writes JSON to a file with pretty printing.
///
/// # Errors
///
/// Returns an error if the file can't be written.
///
/// # Examples
///
/// ```no_run
/// use serde::Serialize;
/// use smop::fs;
///
/// #[derive(Serialize)]
/// struct Config {
///     name: String,
/// }
///
/// let config = Config { name: "example".into() };
/// fs::write_json("config.json", &config)?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn write_json<T: Serialize, P: AsRef<Path>>(path: P, value: &T) -> Result<()> {
    let path = path.as_ref();
    let content = serde_json::to_string_pretty(value)
        .with_context(|| format!("Failed to serialize JSON for: {}", path.display()))?;
    write_string(path, content)
}

/// Reads a file and returns its lines as a vector.
///
/// Empty lines are preserved. Line endings are stripped.
///
/// # Errors
///
/// Returns an error if the file doesn't exist or can't be read.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// let lines = fs::read_lines("data.txt")?;
/// for line in lines {
///     println!("{}", line);
/// }
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn read_lines<P: AsRef<Path>>(path: P) -> Result<Vec<String>> {
    let content = read_string(path)?;
    Ok(content.lines().map(String::from).collect())
}

/// Appends content to a file, creating it if it doesn't exist.
///
/// # Errors
///
/// Returns an error if the file can't be written.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// fs::append("log.txt", "New log entry\n")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn append<P: AsRef<Path>, C: AsRef<str>>(path: P, content: C) -> Result<()> {
    let path = path.as_ref();
    let mut file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .with_context(|| format!("Failed to open file for append: {}", path.display()))?;

    file.write_all(content.as_ref().as_bytes())
        .with_context(|| format!("Failed to append to file: {}", path.display()))
}

/// Matches files using glob patterns and returns sorted paths.
///
/// # Errors
///
/// Returns an error if the pattern is invalid or filesystem access fails.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// let rs_files = fs::glob("src/**/*.rs")?;
/// for file in rs_files {
///     println!("{}", file.display());
/// }
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn glob<P: AsRef<str>>(pattern: P) -> Result<Vec<PathBuf>> {
    let pattern = pattern.as_ref();
    let mut paths: Vec<PathBuf> = glob::glob(pattern)
        .with_context(|| format!("Failed to parse glob pattern: {pattern}"))?
        .collect::<Result<Vec<_>, _>>()
        .with_context(|| format!("Failed to match glob pattern: {pattern}"))?;

    paths.sort();
    Ok(paths)
}

/// Reads and deserializes TOML from a file.
///
/// # Errors
///
/// Returns an error if the file doesn't exist or contains invalid TOML.
///
/// # Examples
///
/// ```no_run
/// use serde::Deserialize;
/// use smop::fs;
///
/// #[derive(Deserialize)]
/// struct Config {
///     name: String,
/// }
///
/// let config: Config = fs::read_toml("config.toml")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn read_toml<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> {
    let path = path.as_ref();
    let content = read_string(path)?;
    toml::from_str(&content)
        .with_context(|| format!("Failed to parse TOML from: {}", path.display()))
}

/// Serializes and writes TOML to a file with pretty printing.
///
/// # Errors
///
/// Returns an error if the file can't be written.
///
/// # Examples
///
/// ```no_run
/// use serde::Serialize;
/// use smop::fs;
///
/// #[derive(Serialize)]
/// struct Config {
///     name: String,
/// }
///
/// let config = Config { name: "example".into() };
/// fs::write_toml("config.toml", &config)?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn write_toml<T: Serialize, P: AsRef<Path>>(path: P, value: &T) -> Result<()> {
    let path = path.as_ref();
    let content = toml::to_string_pretty(value)
        .with_context(|| format!("Failed to serialize TOML for: {}", path.display()))?;
    write_string(path, content)
}

/// Copies a file or directory from one location to another.
///
/// # Errors
///
/// Returns an error if the source doesn't exist or the operation fails.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// fs::copy("source.txt", "dest.txt")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()> {
    let from = from.as_ref();
    let to = to.as_ref();
    fs::copy(from, to)
        .with_context(|| format!("Failed to copy {} to {}", from.display(), to.display()))?;
    Ok(())
}

/// Renames a file or directory.
///
/// # Errors
///
/// Returns an error if the source doesn't exist or the operation fails.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// fs::rename("old.txt", "new.txt")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()> {
    let from = from.as_ref();
    let to = to.as_ref();
    fs::rename(from, to)
        .with_context(|| format!("Failed to rename {} to {}", from.display(), to.display()))
}

/// Removes a file or directory (recursively if directory).
///
/// # Errors
///
/// Returns an error if the path doesn't exist or can't be removed.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// fs::remove("file.txt")?;
/// fs::remove("directory")?;  // Removes recursively
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn remove<P: AsRef<Path>>(path: P) -> Result<()> {
    let path = path.as_ref();

    // Try as file first, then as directory
    if let Err(e) = fs::remove_file(path) {
        if path.is_dir() {
            fs::remove_dir_all(path)
                .with_context(|| format!("Failed to remove directory: {}", path.display()))?;
        } else {
            return Err(e).with_context(|| format!("Failed to remove file: {}", path.display()));
        }
    }
    Ok(())
}

/// Creates a temporary file and returns a handle and path.
///
/// The file will be deleted when the handle is dropped unless persisted.
///
/// # Errors
///
/// Returns an error if the temporary file can't be created.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// let (mut file, path) = fs::temp_file()?;
/// use std::io::Write;
/// writeln!(file, "temporary data")?;
/// println!("Temp file at: {}", path.display());
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn temp_file() -> Result<(std::fs::File, PathBuf)> {
    let file = tempfile::NamedTempFile::new().context("Failed to create temporary file")?;
    let path = file.path().to_path_buf();
    let file = file.into_file();
    Ok((file, path))
}

/// Creates a temporary directory that will be auto-cleaned on drop.
///
/// # Errors
///
/// Returns an error if the temporary directory can't be created.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// let temp = fs::temp_dir()?;
/// let file_path = temp.path().join("test.txt");
/// fs::write_string(&file_path, "data")?;
/// // Directory automatically cleaned up when `temp` goes out of scope
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn temp_dir() -> Result<TempDir> {
    TempDir::new().context("Failed to create temporary directory")
}

// ============================================================================
// CSV Functions (feature-gated)
// ============================================================================

/// Reads and deserializes CSV from a file.
///
/// Each row is deserialized into the specified type. The first row is treated
/// as headers and used for field matching.
///
/// This function is only available with the `csv` feature.
///
/// # Errors
///
/// Returns an error if the file doesn't exist or contains invalid CSV.
///
/// # Examples
///
/// ```no_run
/// use serde::Deserialize;
/// use smop::fs;
///
/// #[derive(Deserialize)]
/// struct Record {
///     name: String,
///     age: u32,
/// }
///
/// let records: Vec<Record> = fs::read_csv("data.csv")?;
/// for record in records {
///     println!("{}: {}", record.name, record.age);
/// }
/// # Ok::<(), anyhow::Error>(())
/// ```
#[cfg(feature = "csv")]
pub fn read_csv<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<Vec<T>> {
    let path = path.as_ref();
    let mut reader = csv::Reader::from_path(path)
        .with_context(|| format!("Failed to open CSV file: {}", path.display()))?;

    let mut records = Vec::new();
    for result in reader.deserialize() {
        let record: T = result
            .with_context(|| format!("Failed to parse CSV record from: {}", path.display()))?;
        records.push(record);
    }
    Ok(records)
}

/// Serializes and writes records to a CSV file.
///
/// The first row will contain headers derived from the struct field names.
///
/// This function is only available with the `csv` feature.
///
/// # Errors
///
/// Returns an error if the file can't be written.
///
/// # Examples
///
/// ```no_run
/// use serde::Serialize;
/// use smop::fs;
///
/// #[derive(Serialize)]
/// struct Record {
///     name: String,
///     age: u32,
/// }
///
/// let records = vec![
///     Record { name: "Alice".into(), age: 30 },
///     Record { name: "Bob".into(), age: 25 },
/// ];
/// fs::write_csv("output.csv", &records)?;
/// # Ok::<(), anyhow::Error>(())
/// ```
#[cfg(feature = "csv")]
pub fn write_csv<T: Serialize, P: AsRef<Path>>(path: P, records: &[T]) -> Result<()> {
    let path = path.as_ref();
    let mut writer = csv::Writer::from_path(path)
        .with_context(|| format!("Failed to create CSV file: {}", path.display()))?;

    for record in records {
        writer
            .serialize(record)
            .with_context(|| format!("Failed to write CSV record to: {}", path.display()))?;
    }
    writer
        .flush()
        .with_context(|| format!("Failed to flush CSV file: {}", path.display()))?;
    Ok(())
}

/// Reads CSV as raw string rows without type deserialization.
///
/// Returns each row as a vector of strings. The first row (headers) is included.
///
/// This function is only available with the `csv` feature.
///
/// # Errors
///
/// Returns an error if the file doesn't exist or can't be parsed.
///
/// # Examples
///
/// ```no_run
/// use smop::fs;
///
/// let rows = fs::read_csv_rows("data.csv")?;
/// for row in rows {
///     println!("{:?}", row);
/// }
/// # Ok::<(), anyhow::Error>(())
/// ```
#[cfg(feature = "csv")]
pub fn read_csv_rows<P: AsRef<Path>>(path: P) -> Result<Vec<Vec<String>>> {
    let path = path.as_ref();
    let mut reader = csv::ReaderBuilder::new()
        .has_headers(false)
        .from_path(path)
        .with_context(|| format!("Failed to open CSV file: {}", path.display()))?;

    let mut rows = Vec::new();
    for result in reader.records() {
        let record =
            result.with_context(|| format!("Failed to parse CSV row from: {}", path.display()))?;
        rows.push(record.iter().map(String::from).collect());
    }
    Ok(rows)
}

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

    fn setup() -> TempDir {
        TempDir::new().unwrap()
    }

    #[test]
    fn read_string_reads_file_contents() {
        let dir = setup();
        let path = dir.path().join("test.txt");
        std::fs::write(&path, "Hello, world!").unwrap();

        let content = read_string(&path).unwrap();
        assert_eq!(content, "Hello, world!");
    }

    #[test]
    fn read_string_fails_on_missing_file() {
        let result = read_string("/nonexistent/path/file.txt");
        assert!(result.is_err());
    }

    #[test]
    fn write_string_creates_file() {
        let dir = setup();
        let path = dir.path().join("new.txt");

        write_string(&path, "New content").unwrap();

        assert!(path.exists());
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "New content");
    }

    #[test]
    fn write_string_overwrites_existing() {
        let dir = setup();
        let path = dir.path().join("existing.txt");
        std::fs::write(&path, "Old").unwrap();

        write_string(&path, "New").unwrap();

        assert_eq!(std::fs::read_to_string(&path).unwrap(), "New");
    }

    #[test]
    fn read_json_deserializes_correctly() {
        let dir = setup();
        let path = dir.path().join("data.json");
        std::fs::write(&path, r#"{"name": "test", "value": 42}"#).unwrap();

        #[derive(serde::Deserialize, PartialEq, Debug)]
        struct Data {
            name: String,
            value: i32,
        }

        let data: Data = read_json(&path).unwrap();
        assert_eq!(data.name, "test");
        assert_eq!(data.value, 42);
    }

    #[test]
    fn read_json_fails_on_invalid_json() {
        let dir = setup();
        let path = dir.path().join("bad.json");
        std::fs::write(&path, "not valid json").unwrap();

        let result: Result<serde_json::Value> = read_json(&path);
        assert!(result.is_err());
    }

    #[test]
    fn write_json_serializes_with_pretty_print() {
        let dir = setup();
        let path = dir.path().join("out.json");

        #[derive(serde::Serialize)]
        struct Data {
            name: String,
        }

        let data = Data {
            name: "test".to_string(),
        };
        write_json(&path, &data).unwrap();

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains('\n'), "Should be pretty-printed");
        assert!(content.contains("\"name\": \"test\""));
    }

    #[test]
    fn read_lines_splits_on_newlines() {
        let dir = setup();
        let path = dir.path().join("lines.txt");
        std::fs::write(&path, "line1\nline2\nline3").unwrap();

        let lines = read_lines(&path).unwrap();
        assert_eq!(lines, vec!["line1", "line2", "line3"]);
    }

    #[test]
    fn read_lines_preserves_empty_lines() {
        let dir = setup();
        let path = dir.path().join("empty.txt");
        std::fs::write(&path, "line1\n\nline3").unwrap();

        let lines = read_lines(&path).unwrap();
        assert_eq!(lines, vec!["line1", "", "line3"]);
    }

    #[test]
    fn append_creates_file_if_missing() {
        let dir = setup();
        let path = dir.path().join("append.txt");

        append(&path, "first").unwrap();

        assert!(path.exists());
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "first");
    }

    #[test]
    fn append_adds_to_existing_file() {
        let dir = setup();
        let path = dir.path().join("append2.txt");
        std::fs::write(&path, "existing\n").unwrap();

        append(&path, "new\n").unwrap();

        assert_eq!(std::fs::read_to_string(&path).unwrap(), "existing\nnew\n");
    }

    // CSV tests (feature-gated)

    #[cfg(feature = "csv")]
    #[test]
    fn read_csv_deserializes_records() {
        let dir = setup();
        let path = dir.path().join("data.csv");
        std::fs::write(&path, "name,age\nAlice,30\nBob,25").unwrap();

        #[derive(serde::Deserialize, PartialEq, Debug)]
        struct Record {
            name: String,
            age: u32,
        }

        let records: Vec<Record> = read_csv(&path).unwrap();
        assert_eq!(records.len(), 2);
        assert_eq!(records[0].name, "Alice");
        assert_eq!(records[0].age, 30);
        assert_eq!(records[1].name, "Bob");
        assert_eq!(records[1].age, 25);
    }

    #[cfg(feature = "csv")]
    #[test]
    fn read_csv_fails_on_missing_file() {
        let result: Result<Vec<serde_json::Value>> = read_csv("/nonexistent/path/file.csv");
        assert!(result.is_err());
    }

    #[cfg(feature = "csv")]
    #[test]
    fn write_csv_creates_file_with_headers() {
        let dir = setup();
        let path = dir.path().join("output.csv");

        #[derive(serde::Serialize)]
        struct Record {
            name: String,
            score: i32,
        }

        let records = vec![
            Record {
                name: "Alice".to_string(),
                score: 95,
            },
            Record {
                name: "Bob".to_string(),
                score: 87,
            },
        ];
        write_csv(&path, &records).unwrap();

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains("name,score"));
        assert!(content.contains("Alice,95"));
        assert!(content.contains("Bob,87"));
    }

    #[cfg(feature = "csv")]
    #[test]
    fn read_csv_rows_returns_all_rows() {
        let dir = setup();
        let path = dir.path().join("raw.csv");
        std::fs::write(&path, "a,b,c\n1,2,3\n4,5,6").unwrap();

        let rows = read_csv_rows(&path).unwrap();
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[0], vec!["a", "b", "c"]);
        assert_eq!(rows[1], vec!["1", "2", "3"]);
        assert_eq!(rows[2], vec!["4", "5", "6"]);
    }

    #[cfg(feature = "csv")]
    #[test]
    fn read_csv_roundtrip() {
        let dir = setup();
        let path = dir.path().join("roundtrip.csv");

        #[derive(serde::Serialize, serde::Deserialize, PartialEq, Debug)]
        struct Record {
            id: u32,
            value: String,
        }

        let original = vec![
            Record {
                id: 1,
                value: "first".to_string(),
            },
            Record {
                id: 2,
                value: "second".to_string(),
            },
        ];

        write_csv(&path, &original).unwrap();
        let loaded: Vec<Record> = read_csv(&path).unwrap();

        assert_eq!(original, loaded);
    }

    #[test]
    fn glob_matches_files() {
        let dir = setup();
        std::fs::write(dir.path().join("test1.txt"), "a").unwrap();
        std::fs::write(dir.path().join("test2.txt"), "b").unwrap();
        std::fs::write(dir.path().join("other.rs"), "c").unwrap();

        let pattern = format!("{}/**/*.txt", dir.path().display());
        let matches = glob(&pattern).unwrap();

        assert_eq!(matches.len(), 2);
        assert!(matches.iter().all(|p| p.extension().unwrap() == "txt"));
    }

    #[test]
    fn glob_returns_sorted_results() {
        let dir = setup();
        std::fs::write(dir.path().join("c.txt"), "").unwrap();
        std::fs::write(dir.path().join("a.txt"), "").unwrap();
        std::fs::write(dir.path().join("b.txt"), "").unwrap();

        let pattern = format!("{}/*.txt", dir.path().display());
        let matches = glob(&pattern).unwrap();

        let names: Vec<_> = matches
            .iter()
            .filter_map(|p| p.file_name()?.to_str())
            .collect();

        assert_eq!(names, vec!["a.txt", "b.txt", "c.txt"]);
    }

    #[test]
    fn read_toml_deserializes_correctly() {
        let dir = setup();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "name = \"test\"\nvalue = 42").unwrap();

        #[derive(serde::Deserialize, PartialEq, Debug)]
        struct Config {
            name: String,
            value: i32,
        }

        let config: Config = read_toml(&path).unwrap();
        assert_eq!(config.name, "test");
        assert_eq!(config.value, 42);
    }

    #[test]
    fn write_toml_serializes_correctly() {
        let dir = setup();
        let path = dir.path().join("output.toml");

        #[derive(serde::Serialize)]
        struct Config {
            name: String,
            value: i32,
        }

        let config = Config {
            name: "test".to_string(),
            value: 42,
        };
        write_toml(&path, &config).unwrap();

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains("name = \"test\""));
        assert!(content.contains("value = 42"));
    }

    #[test]
    fn toml_roundtrip() {
        let dir = setup();
        let path = dir.path().join("roundtrip.toml");

        #[derive(serde::Serialize, serde::Deserialize, PartialEq, Debug)]
        struct Config {
            name: String,
            count: u32,
        }

        let original = Config {
            name: "test".to_string(),
            count: 123,
        };

        write_toml(&path, &original).unwrap();
        let loaded: Config = read_toml(&path).unwrap();

        assert_eq!(original, loaded);
    }

    #[test]
    fn copy_file_works() {
        let dir = setup();
        let src = dir.path().join("source.txt");
        let dst = dir.path().join("dest.txt");

        std::fs::write(&src, "content").unwrap();
        copy(&src, &dst).unwrap();

        assert!(dst.exists());
        assert_eq!(std::fs::read_to_string(&dst).unwrap(), "content");
    }

    #[test]
    fn rename_file_works() {
        let dir = setup();
        let old = dir.path().join("old.txt");
        let new = dir.path().join("new.txt");

        std::fs::write(&old, "content").unwrap();
        rename(&old, &new).unwrap();

        assert!(!old.exists());
        assert!(new.exists());
        assert_eq!(std::fs::read_to_string(&new).unwrap(), "content");
    }

    #[test]
    fn remove_file_works() {
        let dir = setup();
        let path = dir.path().join("remove_me.txt");

        std::fs::write(&path, "content").unwrap();
        assert!(path.exists());

        remove(&path).unwrap();
        assert!(!path.exists());
    }

    #[test]
    fn remove_directory_works() {
        let dir = setup();
        let subdir = dir.path().join("subdir");
        std::fs::create_dir(&subdir).unwrap();
        std::fs::write(subdir.join("file.txt"), "content").unwrap();

        remove(&subdir).unwrap();
        assert!(!subdir.exists());
    }

    #[test]
    fn temp_file_creates_file() {
        let (file, path) = temp_file().unwrap();
        drop(file);
        // Note: Path may or may not exist after drop depending on tempfile behavior
        // The important thing is we got a valid path
        assert!(!path.as_os_str().is_empty());
    }

    #[test]
    fn temp_dir_creates_directory() {
        let temp = temp_dir().unwrap();
        assert!(temp.path().exists());
        assert!(temp.path().is_dir());

        // Write a file into it
        let file_path = temp.path().join("test.txt");
        std::fs::write(&file_path, "data").unwrap();
        assert!(file_path.exists());

        // When temp drops, directory should be cleaned up
    }
}