scsys_util/
fs.rs

1/*
2    Appellation: fs <module>
3    Contrib: @FL03
4*/
5
6/// This function converts the file found at path (fp) into a [Vec<String>]
7pub fn file_to_vec(fp: String) -> std::io::Result<Vec<String>> {
8    use std::io::BufRead;
9    // open the file
10    let file_in = std::fs::File::open(fp)?;
11    // initialize a new buffered reader
12    let file_reader = std::io::BufReader::new(file_in);
13    // initialize a new buffer to hold the lines
14    let mut buffer = Vec::new();
15    // read the file line by line and collect into a Vec<String>
16    // filtering out any errors in the process
17    file_reader.lines().for_each(|line| match line {
18        Ok(l) => buffer.push(l),
19        Err(e) => eprintln!("Error reading line: {}", e),
20    });
21    // collect the lines into a Vec<String>
22    Ok(buffer)
23}