gen_file/
file.rs

1use std::io;
2use std::io::Write;
3use std::fs;
4use std::path::PathBuf;
5
6pub fn write(line_set: Vec<String>, path: &PathBuf) -> io::Result<()> {
7    let mut file = fs::OpenOptions::new().write(true).create(true)
8        .truncate(true).open(path)
9        .map_err(|e| io::Error::new(e.kind(), format!("Failed to open {}: {}", path.display(), e)))?;
10
11    for line in line_set {
12        writeln!(file, "{}", line.trim_end())
13            .map_err(|e| io::Error::new(e.kind(), format!("Failed to write to {}: {}", path.display(), e)))?;
14    }
15
16    Ok(())
17}
18
19pub fn create(content: String, path: &PathBuf) -> io::Result<()> {
20    let mut file = fs::OpenOptions::new().write(true).create(true)
21        .truncate(true).open(path)
22        .map_err(|e| io::Error::new(e.kind(), format!("Failed to create {}: {}", path.display(), e)))?;
23
24    write!(file, "{}", content)
25        .map_err(|e| io::Error::new(e.kind(), format!("Failed to write to {}: {}", path.display(), e)))
26}
27
28pub fn delete(path: &PathBuf) -> io::Result<()> {
29    fs::remove_file(path)
30        .map_err(|e| io::Error::new(e.kind(), format!("Failed to delete {}: {}", path.display(), e)))
31}
32
33pub fn read(path: &PathBuf) -> io::Result<String> {
34    fs::read_to_string(path)
35        .map_err(|e| io::Error::new(e.kind(), format!("Failed to read {}: {}", path.display(), e)))
36}
37
38#[cfg(test)]
39#[path = "../tests/unit_tests/file.rs"]
40pub mod test;