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::fmt::Display;
use std::fs;
use std::io::{Read, Write};
use std::path::Path;

/// A no-op struct that allows you to easily manipulate files.
pub struct File;

impl File {
    /// Read a file at `path` into a String.
    pub fn read<P: AsRef<Path>>(path: P) -> Result<String, String> {
        let mut file = fs::OpenOptions::new().read(true).open(&path);

        match &mut file {
            Ok(f) => {
                let mut contents = String::from("");
                match f.read_to_string(&mut contents) {
                    Ok(_) => Ok(contents),
                    Err(e) => Err(format!(
                        "Could not read from file '{}' because {}",
                        match path.as_ref().to_str() {
                            Some(s) => s,
                            None => "",
                        },
                        e.to_string()
                    )),
                }
            }
            Err(e) => Err(format!(
                "Could not open file '{}' because {}",
                match path.as_ref().to_str() {
                    Some(s) => s,
                    None => "",
                },
                e.to_string()
            )),
        }
    }

    /// Write a string to file.
    /// This truncates a file (sets its size to zero to clear its contents),
    /// and then writes `contents` to the file.
    pub fn write<P: AsRef<Path>, S: Display>(path: P, contents: S) -> Result<(), String> {
        let mut file = fs::OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(&path);

        match &mut file {
            Ok(f) => match writeln!(f, "{}", contents) {
                Ok(_) => Ok(()),
                Err(e) => Err(format!(
                    "Could not open file '{}' because {}",
                    match path.as_ref().to_str() {
                        Some(s) => s,
                        None => "",
                    },
                    e.to_string()
                )),
            },
            Err(e) => Err(format!(
                "Could not open file '{}' because {}",
                match path.as_ref().to_str() {
                    Some(s) => s,
                    None => "",
                },
                e.to_string()
            )),
        }
    }

    /// This does the same as write, but does not wipe the file,
    /// and appends `contents` to the end of the file.
    pub fn append<P: AsRef<Path>, S: Display>(path: P, contents: S) -> Result<(), String> {
        let mut file = fs::OpenOptions::new().append(true).write(true).open(&path);

        match &mut file {
            Ok(f) => match writeln!(f, "{}", contents) {
                Ok(_) => Ok(()),
                Err(e) => Err(format!(
                    "Could not open file '{}' because {}",
                    match path.as_ref().to_str() {
                        Some(s) => s,
                        None => "",
                    },
                    e.to_string()
                )),
            },
            Err(e) => Err(format!(
                "Could not open file '{}' because {}",
                match path.as_ref().to_str() {
                    Some(s) => s,
                    None => "",
                },
                e.to_string()
            )),
        }
    }
}