scsys_core/utils/
std.rs

1/*
2    Appellation: std_utils <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5#![cfg(feature = "std")]
6
7pub use self::fs::*;
8
9/// Fetch the project root unless specified otherwise with a CARGO_MANIFEST_DIR env variable
10pub fn project_root() -> std::path::PathBuf {
11    std::path::Path::new(&env!("CARGO_MANIFEST_DIR"))
12        .ancestors()
13        .nth(1)
14        .unwrap()
15        .to_path_buf()
16}
17
18mod fs {
19    use std::fs::File;
20    use std::io::{self, BufRead, BufReader};
21    /// A generic function wrapper extending glob::glob
22    pub fn collect_files_as<T>(f: &dyn Fn(std::path::PathBuf) -> T, pat: &str) -> Vec<T> {
23        let mut files = Vec::<T>::new();
24        for path in glob::glob(pat).expect("Failed to read glob pattern...") {
25            if let Ok(r) = path {
26                files.push(f(r))
27            }
28            continue;
29        }
30        files
31    }
32
33    /// This function converts the file found at path (fp) into a [Vec<String>]
34    pub fn file_to_vec(fp: String) -> Result<Vec<String>, io::Error> {
35        let file_in = File::open(fp)?;
36        let file_reader = BufReader::new(file_in);
37        Ok(file_reader.lines().filter_map(Result::ok).collect())
38    }
39}