superviseur_util/
lib.rs

1use anyhow::Error;
2use std::{
3    fs::File,
4    io::{BufRead, BufReader},
5    path::PathBuf,
6};
7
8pub fn convert_dir_path_to_absolute_path(dir: &str, current_dir: &str) -> Result<String, Error> {
9    let current_dir = PathBuf::from(current_dir);
10    if dir == "." || dir == "" || dir == "./" {
11        return Ok(current_dir.to_str().unwrap().to_string());
12    }
13    if dir.starts_with("./") {
14        return Ok(current_dir.join(&dir[2..]).to_str().unwrap().to_string());
15    }
16    if current_dir.join(dir).is_dir() {
17        return Ok(current_dir.join(dir).to_str().unwrap().to_string());
18    }
19    Err(Error::msg("Invalid directory"))
20}
21
22pub fn read_lines(path: &str) -> Result<Vec<String>, Error> {
23    let file = File::open(path).map_err(|e| Error::msg(e.to_string()))?;
24
25    let reader = BufReader::new(file);
26
27    let mut lines = vec![];
28    for line in reader.lines() {
29        let line = line.map_err(|e| Error::msg(e.to_string()))?;
30        lines.push(line);
31    }
32    Ok(lines)
33}