quiet_stroll/
lib.rs

1use framels::{basic_listing, paths::Paths};
2use glob::glob;
3use glob::PatternError;
4use jwalk::rayon::prelude::IntoParallelRefIterator;
5use jwalk::rayon::prelude::ParallelIterator;
6use jwalk::WalkDir;
7use rocket::serde::{json::Json, Deserialize, Serialize};
8use rocket_okapi::okapi::schemars;
9use rocket_okapi::okapi::schemars::JsonSchema;
10use std::fs;
11use std::path::PathBuf;
12
13fn common_file_operation<F>(input_path: &str, file_op: F) -> Vec<String>
14where
15    F: Fn(fs::DirEntry) -> String,
16{
17    fs::read_dir(input_path)
18        .unwrap()
19        .filter_map(|entry| entry.ok())
20        .map(file_op)
21        .collect()
22}
23
24pub fn get_walk(input_path: &str) -> Vec<String> {
25    WalkDir::new(input_path)
26        .sort(true)
27        .into_iter()
28        .filter_map(|entry| entry.ok())
29        .map(|entry| entry.path().display().to_string())
30        .collect()
31}
32
33pub fn get_list_dir(input_path: &str) -> Vec<String> {
34    let mut dir_list =
35        common_file_operation(input_path, |entry| entry.path().display().to_string());
36    dir_list.sort();
37    dir_list
38}
39
40pub fn get_glob(input_path: &str) -> Result<Vec<String>, PatternError> {
41    let paths = glob(input_path)?;
42    Ok(paths
43        .filter_map(|entry| entry.ok())
44        .map(|entry| entry.display().to_string())
45        .collect())
46}
47
48#[derive(Deserialize, Serialize, JsonSchema)]
49#[serde(crate = "rocket::serde")]
50/// InputPath is a simple struct to represent an input_path
51pub struct InputPath {
52    input_path: String,
53}
54impl InputPath {
55    /// This function create a new InputPath based on a String
56    /// Mainly create for testing purpose
57    pub fn new(s: String) -> InputPath {
58        InputPath { input_path: s }
59    }
60}
61#[derive(Serialize, JsonSchema)]
62#[serde(crate = "rocket::serde")]
63/// QuietPaths is a simple struct to represent the output of the crate by
64/// getting a vector of strings
65pub struct QuietPaths {
66    paths_list: Vec<String>,
67}
68
69impl QuietPaths {
70    /// Create a QuietPaths from a get_list_tdir function
71    pub fn from_listdir(input_path: Json<InputPath>) -> Self {
72        QuietPaths {
73            paths_list: get_list_dir(input_path.input_path.as_str()),
74        }
75    }
76    /// Create a QuietPaths from a get_glob function can return a error if
77    /// if the pattern is not supported by glob lib like double wild card in
78    /// the middle of a path
79    pub fn from_glob(input_path: Json<InputPath>) -> Result<Self, PatternError> {
80        Ok(QuietPaths {
81            paths_list: get_glob(input_path.input_path.as_str())?,
82        })
83    }
84    /// Create a QuietPaths from a get_walk function
85    pub fn from_walk(input_path: Json<InputPath>) -> Self {
86        QuietPaths {
87            paths_list: get_walk(input_path.input_path.as_str()),
88        }
89    }
90    /// Create a QuietPaths from a get_walk function
91    pub fn to_paths(&self) -> Paths {
92        let data: Vec<PathBuf> = self
93            .paths_list
94            .par_iter()
95            .map(|x| PathBuf::from(x))
96            .collect::<Vec<PathBuf>>();
97        Paths::from(data)
98    }
99    /// Create a QuietPaths from a get_walk function
100    pub fn from_paths(paths: Paths) -> Self {
101        let paths_list: Vec<String> = paths
102            .par_iter()
103            .map(|f| f.to_string_lossy().into_owned())
104            .collect::<Vec<String>>();
105        QuietPaths {
106            paths_list: paths_list,
107        }
108    }
109    /// Pack a quiet path to a basic listing using framels lib
110    pub fn packed(&self) -> Self {
111        QuietPaths::from_paths(basic_listing(self.to_paths(), true).get_paths())
112    }
113    /// create a QuietPaths from a string
114    /// mainly for testing purpose
115    pub fn from_string(s: String) -> Self {
116        QuietPaths {
117            paths_list: vec![s],
118        }
119    }
120}