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")]
50pub struct InputPath {
52 input_path: String,
53}
54impl InputPath {
55 pub fn new(s: String) -> InputPath {
58 InputPath { input_path: s }
59 }
60}
61#[derive(Serialize, JsonSchema)]
62#[serde(crate = "rocket::serde")]
63pub struct QuietPaths {
66 paths_list: Vec<String>,
67}
68
69impl QuietPaths {
70 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 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 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 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 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 pub fn packed(&self) -> Self {
111 QuietPaths::from_paths(basic_listing(self.to_paths(), true).get_paths())
112 }
113 pub fn from_string(s: String) -> Self {
116 QuietPaths {
117 paths_list: vec![s],
118 }
119 }
120}