summaryrefslogtreecommitdiffstats
path: root/src/utils/fileutil.rs
blob: aac3ab6e1ff3d70f2ac0b488d66d885e92709956 (plain)
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
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::{fs, io};

pub fn file_iter(dir: &Path) -> impl Iterator<Item = PathBuf> {
    use walkdir::WalkDir;

    WalkDir::new(dir)
        .follow_links(true)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .map(|entry| entry.into_path())
}

pub fn dir_iter(dir: &Path) -> impl Iterator<Item = PathBuf> {
    use walkdir::WalkDir;

    let dir = dir.to_path_buf();
    WalkDir::new(&dir)
        .follow_links(true)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_dir())
        .filter(move |f| f.path() != dir)
        .map(|entry| entry.into_path())
}

pub fn write_file(filepath: &Path, contents: &str) -> io::Result<()> {
    let mut file = fs::File::create(filepath)?;
    file.write_all(contents.as_bytes())
}

pub fn append_file(filepath: &Path, contents: &str) -> io::Result<()> {
    let mut file = OpenOptions::new()
        .append(true)
        .create(true)
        .open(filepath)?;
    file.write_all(contents.as_bytes())
}

pub fn read_lines_from_file(
    filepath: &Path,
) -> io::Result<impl DoubleEndedIterator<Item = String>> {
    let f = fs::File::open(filepath)?;
    let f = BufReader::new(f);
    let lines: Result<Vec<String>, io::Error> = f.lines().collect();
    lines.map(|result| result.into_iter())
}

pub fn read_file_to_string(path: &Path) -> io::Result<String> {
    let mut file = fs::File::open(&path)?;
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;
    Ok(contents)
}