Skip to main content

task_commander/
loader.rs

1//! # Task definitions loader
2
3use super::errors::{Result, err_definitions_not_found};
4use std::fs::{read_dir, read_to_string};
5
6/// Returns the content of the task definitions file.
7///
8/// Error handling is intentionally minimal. File access and filesystem errors
9/// are not reported precisely because such failures are expected to be rare.
10/// This function will just panic if the task definitions file is not accessible.
11///
12/// # Errors
13///
14/// (tbd)
15///
16/// # Panics
17///
18/// TODO Get rid of panics
19pub fn load_definitions() -> Result<String> {
20    let mut files = vec![];
21    let paths = read_dir(".").expect("Failed to access current directory");
22    for entry in paths.flatten() {
23        let file_type = entry.file_type().expect("Failed to get file type");
24        if file_type.is_file() {
25            let file_name = entry.file_name().to_string_lossy().to_string();
26            if file_name.ends_with(".taskcmd") {
27                files.push(file_name);
28            }
29        }
30    }
31    files.sort();
32    files.reverse();
33    files.first().map_or_else(
34        || Err(err_definitions_not_found()),
35        |file_name| Ok(read_to_string(file_name).expect("Failed to read task definitions file")),
36    )
37}