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
use std::fs::{self, File};
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::Path;

use anyhow::Result;
use std::io;

#[allow(dead_code)]
pub fn mk_dir(path: impl AsRef<Path>) -> Result<()> {
    fs::create_dir_all(path)?;
    Ok(())
}

#[allow(dead_code)]
pub fn read_file(path: impl AsRef<Path>) -> Result<Vec<u8>> {
    let file = File::open(&path)?;
    let mut reader = BufReader::new(file);
    let mut buf = Vec::new();
    reader.read_to_end(&mut buf)?;
    Ok(buf)
}

#[allow(dead_code)]
pub fn write_file(path: impl AsRef<Path>, contents: &[u8]) -> Result<()> {
    let file = File::create(path)?;
    let mut buf = BufWriter::new(file);
    buf.write_all(contents)?;
    Ok(())
}

pub fn read_from_stdin() -> Result<String> {
    let mut buf = String::new();
    let stdin = io::stdin();
    let mut handle = stdin.lock();
    handle.read_to_string(&mut buf)?;
    Ok(buf)
}

#[cfg(test)]
mod tests {

    use crate::fs::*;
    use std::str;
    use tempfile::tempdir;

    #[test]
    fn mk_dir_one_layer_ok() -> Result<()> {
        let tmp_dir = tempdir()?;
        let path = tmp_dir.path().join("test");
        let actual = mk_dir(&path);
        assert!(actual.is_ok());
        assert!(path.exists());
        Ok(())
    }

    #[test]
    fn mk_dir_deep_layer_ok() -> Result<()> {
        let tmp_dir = tempdir()?;
        let path = tmp_dir.path().join("test/test/test/test");
        let actual = mk_dir(&path);
        assert!(actual.is_ok());
        assert!(path.exists());
        Ok(())
    }

    #[test]
    fn read_file_temp_ok() -> Result<()> {
        let expect = "hello";
        let file = "read_test.txt";

        let tmp_dir = tempdir()?;
        let tmp_file = tmp_dir.path().join(file);
        write_file(&tmp_file, expect.as_bytes()).unwrap();

        let actual = read_file(&tmp_file);
        assert!(actual.is_ok());
        assert_eq!(expect, str::from_utf8(&actual.unwrap()).unwrap());
        Ok(())
    }

    #[test]
    fn read_file_not_file_ng() -> Result<()> {
        let file = "not_found.txt";
        let tmp_dir = tempdir()?;
        let tmp_file = tmp_dir.path().join(file);
        let actual = read_file(&tmp_file);
        assert!(actual.is_err());
        Ok(())
    }

    #[test]
    fn write_file_ok() -> Result<()> {
        let file = "write_test.txt";
        let tmp_dir = tempdir()?;
        let tmp_file = tmp_dir.path().join(file);
        let actual = write_file(&tmp_file, b"write");
        assert!(actual.is_ok());
        Ok(())
    }
}