ce/
lib.rs

1use std::{
2    fs::File,
3    io::{self, Read, Write},
4    path::Path,
5};
6
7pub fn cat(path: &Path) -> io::Result<String> {
8    let mut file = File::open(path)?;
9    let mut string = String::new();
10    match file.read_to_string(&mut string) {
11        Ok(_) => Ok(string),
12        Err(e) => Err(e),
13    }
14}
15
16pub fn echo(s: &str, path: &Path) -> io::Result<()> {
17    let mut file = File::create(path)?;
18
19    file.write_all(s.as_bytes())
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn echo_hello_to_txt() {
28        let _r = echo("hello world", &Path::new("./hello.txt"));
29        cat_hello();
30    }
31
32    #[test]
33    fn cat_hello() {
34        cat(&Path::new("./hello.txt")).expect("cannot cat this file");
35    }
36}