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

use crate::icalwrap::IcalVCalendar;

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

  WalkDir::new(dir).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).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 write_cal(cal: &IcalVCalendar) -> io::Result<()> {
  match cal.get_path() {
    Some(path) => write_file(&path, &cal.to_string()),
    None => Err(io::Error::new(io::ErrorKind::Other, "calendar has no path")),
  }
}

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)
}

#[cfg(test)]
mod tests {
  use super::*;

  use crate::testutils::prepare_testdir;
  use assert_fs::prelude::*;

  #[test]
  fn test_append_file() {
    let testdir = prepare_testdir("testdir");
    let file = testdir.child("f");

    append_file(file.path(), "x\ny\n").unwrap();
    file.assert("x\ny\n");

    append_file(file.path(), "z\n").unwrap();
    file.assert("x\ny\nz\n");
  }

  #[test]
  fn test_write_file() {
    let testdir = prepare_testdir("testdir");
    let file = testdir.child("f");

    write_file(file.path(), "x\ny\n").unwrap();
    file.assert("x\ny\n");

    write_file(file.path(), "z\n").unwrap();
    file.assert("z\n");
  }
}