mi-simple-io 0.1.0

Small synchronous IO utilities for common file and terminal workflows.
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
//! # mi-simple-io
//!
//! Small synchronous IO utilities for common file and terminal workflows.
//!
//! ```
//! use mi_simple_io::{read_file, remove, write_file};
//!
//! # fn main() -> std::io::Result<()> {
//! let file = std::env::temp_dir().join("mi_simple_io_doctest.txt");
//! write_file(&file, "hello")?;
//! assert_eq!(read_file(&file)?, "hello");
//! remove(&file)?;
//! # Ok(())
//! # }
//! ```

use std::fs::{self, File, OpenOptions};
use std::io::{self, BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

/// Read a UTF-8 text file into a string.
pub fn read_file<P: AsRef<Path>>(path: P) -> io::Result<String> {
    fs::read_to_string(path)
}

/// Read a file into bytes.
pub fn read_bytes<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
    fs::read(path)
}

/// Write text to a file, replacing existing content.
pub fn write_file<P: AsRef<Path>>(path: P, content: &str) -> io::Result<()> {
    fs::write(path, content)
}

/// Append text to a file, creating it when missing.
pub fn append_file<P: AsRef<Path>>(path: P, content: &str) -> io::Result<()> {
    let mut file = OpenOptions::new().create(true).append(true).open(path)?;
    file.write_all(content.as_bytes())
}

/// Write text when the file is missing, otherwise append it.
pub fn write_or_append<P: AsRef<Path>>(path: P, content: &str) -> io::Result<()> {
    if path.as_ref().exists() {
        append_file(path, content)
    } else {
        write_file(path, content)
    }
}

/// Return file size in bytes.
pub fn file_size<P: AsRef<Path>>(path: P) -> io::Result<u64> {
    Ok(fs::metadata(path)?.len())
}

/// Return whether a path exists.
pub fn exists<P: AsRef<Path>>(path: P) -> bool {
    path.as_ref().exists()
}

/// Return whether a path is a regular file.
pub fn is_file<P: AsRef<Path>>(path: P) -> bool {
    path.as_ref().is_file()
}

/// Return whether a path is a directory.
pub fn is_dir<P: AsRef<Path>>(path: P) -> bool {
    path.as_ref().is_dir()
}

/// Create a directory and any missing parents.
pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
    fs::create_dir_all(path)
}

/// Remove a file or directory tree.
pub fn remove<P: AsRef<Path>>(path: P) -> io::Result<()> {
    let path = path.as_ref();
    if path.is_dir() {
        fs::remove_dir_all(path)
    } else {
        fs::remove_file(path)
    }
}

/// Remove all directory contents while keeping the directory itself.
pub fn clear_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
    for entry in fs::read_dir(path)? {
        let entry = entry?;
        remove(entry.path())?;
    }
    Ok(())
}

/// Copy a file and return the number of bytes copied.
pub fn copy_file<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
    fs::copy(from, to)
}

/// Move or rename a file or directory.
pub fn move_file<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
    fs::rename(from, to)
}

/// Return the file stem as UTF-8 lossy text, or an empty string.
pub fn file_stem<P: AsRef<Path>>(path: P) -> String {
    path.as_ref()
        .file_stem()
        .unwrap_or_default()
        .to_string_lossy()
        .to_string()
}

/// Return the lowercase file extension as UTF-8 lossy text, or an empty string.
pub fn file_extension<P: AsRef<Path>>(path: P) -> String {
    path.as_ref()
        .extension()
        .unwrap_or_default()
        .to_string_lossy()
        .to_lowercase()
}

/// Return the current working directory.
pub fn current_dir() -> io::Result<PathBuf> {
    std::env::current_dir()
}

/// Return the system temporary directory.
pub fn temp_dir() -> PathBuf {
    std::env::temp_dir()
}

/// Read a file as a line iterator, preserving line read errors.
pub fn read_lines<P: AsRef<Path>>(path: P) -> io::Result<impl Iterator<Item = io::Result<String>>> {
    let file = File::open(path)?;
    Ok(BufReader::new(file).lines())
}

/// Read all lines from a file.
pub fn read_all_lines<P: AsRef<Path>>(path: P) -> io::Result<Vec<String>> {
    read_lines(path)?.collect()
}

/// Read the first `n` lines from a file.
pub fn read_first_lines<P: AsRef<Path>>(path: P, n: usize) -> io::Result<Vec<String>> {
    read_lines(path)?.take(n).collect()
}

/// Read the last `n` lines from a file.
pub fn read_last_lines<P: AsRef<Path>>(path: P, n: usize) -> io::Result<Vec<String>> {
    let lines = read_all_lines(path)?;
    let start = lines.len().saturating_sub(n);
    Ok(lines[start..].to_vec())
}

/// Write text via a sibling temporary file before replacing the destination.
pub fn safe_write<P: AsRef<Path>>(path: P, content: &str) -> io::Result<()> {
    let path = path.as_ref();
    let temp_path = path.with_extension("tmp");

    if let Err(error) = write_file(&temp_path, content) {
        let _ = fs::remove_file(&temp_path);
        return Err(error);
    }

    if let Err(error) = fs::rename(&temp_path, path) {
        let _ = fs::remove_file(&temp_path);
        return Err(error);
    }

    Ok(())
}

/// Basic text file statistics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileStats {
    pub size: u64,
    pub lines: usize,
    pub words: usize,
    pub chars: usize,
}

/// Read a text file and return basic statistics.
pub fn file_stats<P: AsRef<Path>>(path: P) -> io::Result<FileStats> {
    let content = read_file(path)?;
    Ok(FileStats {
        size: content.len() as u64,
        lines: content.lines().count(),
        words: content.split_whitespace().count(),
        chars: content.chars().count(),
    })
}

/// Temporary file removed when dropped.
#[derive(Debug)]
pub struct TempFile {
    path: PathBuf,
}

impl TempFile {
    /// Create a new empty temporary file with a prefix.
    pub fn new(prefix: &str) -> io::Result<Self> {
        for attempt in 0..100 {
            let unique = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("system time before unix epoch")
                .as_nanos();
            let path = temp_dir().join(format!(
                "{}_{}_{}_{}",
                prefix,
                std::process::id(),
                unique,
                attempt
            ));

            match OpenOptions::new().write(true).create_new(true).open(&path) {
                Ok(_) => return Ok(Self { path }),
                Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
                Err(error) => return Err(error),
            }
        }

        Err(io::Error::new(
            io::ErrorKind::AlreadyExists,
            "could not create a unique temporary file",
        ))
    }

    /// Create a temporary file with content.
    pub fn with_content(prefix: &str, content: &str) -> io::Result<Self> {
        let temp = Self::new(prefix)?;
        write_file(&temp.path, content)?;
        Ok(temp)
    }

    /// Return the temporary file path.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Read the temporary file as text.
    pub fn read(&self) -> io::Result<String> {
        read_file(&self.path)
    }
}

impl Drop for TempFile {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.path);
    }
}

/// Read a trimmed line from standard input after printing a prompt.
pub fn read_input(prompt: &str) -> io::Result<String> {
    print!("{prompt}");
    io::stdout().flush()?;

    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    Ok(input.trim().to_string())
}

/// Parse confirmation text using a default for empty or unknown input.
pub fn parse_confirmation(input: &str, default: bool) -> bool {
    match input.trim().to_lowercase().as_str() {
        "y" | "yes" => true,
        "n" | "no" => false,
        _ => default,
    }
}

/// Ask a yes/no question and return the selected answer.
pub fn confirm(prompt: &str, default: bool) -> bool {
    let default_text = if default { "Y/n" } else { "y/N" };
    let input = read_input(&format!("{prompt} [{default_text}]: ")).unwrap_or_default();
    parse_confirmation(&input, default)
}

/// Read a password from the terminal without echoing input.
#[cfg(feature = "password")]
pub fn read_password(prompt: &str) -> io::Result<String> {
    print!("{prompt}");
    io::stdout().flush()?;
    rpassword::read_password()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::io;
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn test_dir(name: &str) -> io::Result<PathBuf> {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time before unix epoch")
            .as_nanos();
        let path = std::env::temp_dir().join(format!(
            "mi_simple_io_{}_{}_{}",
            name,
            std::process::id(),
            unique
        ));
        fs::create_dir_all(&path)?;
        Ok(path)
    }

    #[test]
    fn write_read_append_and_write_or_append_text() -> io::Result<()> {
        let dir = test_dir("text")?;
        let file = dir.join("notes.txt");

        write_file(&file, "hello")?;
        assert_eq!(read_file(&file)?, "hello");

        append_file(&file, " world")?;
        assert_eq!(read_file(&file)?, "hello world");

        let new_file = dir.join("new.txt");
        write_or_append(&new_file, "first")?;
        write_or_append(&new_file, " second")?;
        assert_eq!(read_file(&new_file)?, "first second");

        fs::remove_dir_all(dir)?;
        Ok(())
    }

    #[test]
    fn reads_bytes_and_reports_file_size() -> io::Result<()> {
        let dir = test_dir("bytes")?;
        let file = dir.join("data.bin");

        fs::write(&file, [0_u8, 1, 2, 3])?;

        assert_eq!(read_bytes(&file)?, vec![0, 1, 2, 3]);
        assert_eq!(file_size(&file)?, 4);

        fs::remove_dir_all(dir)?;
        Ok(())
    }

    #[test]
    fn checks_file_and_directory_state() -> io::Result<()> {
        let dir = test_dir("state")?;
        let file = dir.join("state.txt");

        assert!(exists(&dir));
        assert!(is_dir(&dir));
        assert!(!is_file(&dir));

        write_file(&file, "state")?;
        assert!(exists(&file));
        assert!(is_file(&file));
        assert!(!is_dir(&file));

        fs::remove_dir_all(dir)?;
        Ok(())
    }

    #[test]
    fn creates_clears_and_removes_directories() -> io::Result<()> {
        let dir = test_dir("dirs")?;
        let nested = dir.join("a/b");
        let file = nested.join("file.txt");

        create_dir(&nested)?;
        write_file(&file, "content")?;
        assert!(is_file(&file));

        clear_dir(dir.join("a"))?;
        assert!(is_dir(dir.join("a")));
        assert!(!exists(&file));

        remove(dir.join("a"))?;
        assert!(!exists(dir.join("a")));

        fs::remove_dir_all(dir)?;
        Ok(())
    }

    #[test]
    fn copies_moves_and_extracts_path_parts() -> io::Result<()> {
        let dir = test_dir("copy_move")?;
        let source = dir.join("Report.TXT");
        let copied = dir.join("copied.txt");
        let moved = dir.join("moved.md");

        write_file(&source, "copy me")?;
        assert_eq!(copy_file(&source, &copied)?, 7);
        assert_eq!(read_file(&copied)?, "copy me");

        move_file(&copied, &moved)?;
        assert!(!exists(&copied));
        assert_eq!(read_file(&moved)?, "copy me");

        assert_eq!(file_stem(&source), "Report");
        assert_eq!(file_extension(&source), "txt");
        assert!(current_dir()?.is_dir());
        assert!(temp_dir().is_dir());

        fs::remove_dir_all(dir)?;
        Ok(())
    }

    #[test]
    fn reads_lines_and_line_ranges() -> io::Result<()> {
        let dir = test_dir("lines")?;
        let file = dir.join("lines.txt");
        write_file(&file, "one\ntwo\nthree\nfour\n")?;

        let lines = read_all_lines(&file)?;
        assert_eq!(lines, vec!["one", "two", "three", "four"]);

        let streamed = read_lines(&file)?.collect::<io::Result<Vec<_>>>()?;
        assert_eq!(streamed, vec!["one", "two", "three", "four"]);

        assert_eq!(read_first_lines(&file, 2)?, vec!["one", "two"]);
        assert_eq!(read_last_lines(&file, 2)?, vec!["three", "four"]);
        assert_eq!(
            read_last_lines(&file, 10)?,
            vec!["one", "two", "three", "four"]
        );

        fs::remove_dir_all(dir)?;
        Ok(())
    }

    #[test]
    fn safely_writes_and_reports_file_stats() -> io::Result<()> {
        let dir = test_dir("safe_stats")?;
        let file = dir.join("stats.txt");

        safe_write(&file, "hello rust\nsecond line")?;
        assert_eq!(read_file(&file)?, "hello rust\nsecond line");
        assert!(!exists(file.with_extension("tmp")));

        let stats = file_stats(&file)?;
        assert_eq!(stats.size, 22);
        assert_eq!(stats.lines, 2);
        assert_eq!(stats.words, 4);
        assert_eq!(stats.chars, 22);

        fs::remove_dir_all(dir)?;
        Ok(())
    }

    #[test]
    fn temp_file_writes_reads_and_removes_on_drop() -> io::Result<()> {
        let path;
        {
            let temp = TempFile::with_content("unit", "temporary data")?;
            path = temp.path().to_path_buf();
            assert!(is_file(&path));
            assert_eq!(temp.read()?, "temporary data");
        }
        assert!(!exists(path));
        Ok(())
    }

    #[test]
    fn confirm_defaults_match_empty_or_unknown_answers() {
        assert_eq!(parse_confirmation("", true), true);
        assert_eq!(parse_confirmation("", false), false);
        assert_eq!(parse_confirmation("maybe", true), true);
        assert_eq!(parse_confirmation("maybe", false), false);
        assert_eq!(parse_confirmation("yes", false), true);
        assert_eq!(parse_confirmation("Y", false), true);
        assert_eq!(parse_confirmation("no", true), false);
        assert_eq!(parse_confirmation("N", true), false);
    }
}