Skip to content

Commit

Permalink
add packed option
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleailes committed Jul 18, 2023
1 parent 4d4a0e8 commit ea1031b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
38 changes: 25 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use framels::{basic_listing, paths::Paths};
use glob::glob;
use jwalk::WalkDir;
use rocket::serde::{json::Json, Deserialize, Serialize};
Expand All @@ -9,7 +10,7 @@ use std::fs;
/// subfolders
/// The result is sorted to be consistant trough the os
pub fn get_walk(input_path: &str) -> Vec<String> {
WalkDir::new(input_path)
WalkDir::new(input_path)
.sort(true)
.into_iter()
.filter_map(|e| e.ok())
Expand Down Expand Up @@ -77,29 +78,40 @@ impl InputPath {
}
#[derive(Serialize, JsonSchema)]
#[serde(crate = "rocket::serde")]
/// Paths is a simple struct to represent the output of the crate by
/// QuietPaths is a simple struct to represent the output of the crate by
/// getting a vector of strings
pub struct Paths {
pub struct QuietPaths {
paths_list: Vec<String>,
}

impl Paths {
/// Create a Paths from a get_list_tdir function
pub fn from_listdir(input_path: Json<InputPath>) -> Paths {
Paths {
impl QuietPaths {
/// Create a QuietPaths from a get_list_tdir function
pub fn from_listdir(input_path: Json<InputPath>) -> QuietPaths {
QuietPaths {
paths_list: get_list_dir(input_path.input_path.as_str()),
}
}
/// Create a Paths from a get_glob function
pub fn from_glob(input_path: Json<InputPath>) -> Paths {
Paths {
/// Create a QuietPaths from a get_glob function
pub fn from_glob(input_path: Json<InputPath>) -> QuietPaths {
QuietPaths {
paths_list: get_glob(input_path.input_path.as_str()),
}
}
/// Create a Paths from a get_walk function
pub fn from_walk(input_path: Json<InputPath>) -> Paths {
Paths {
/// Create a QuietPaths from a get_walk function
pub fn from_walk(input_path: Json<InputPath>) -> QuietPaths {
QuietPaths {
paths_list: get_walk(input_path.input_path.as_str()),
}
}
pub fn to_paths(&self) -> Paths {
Paths::new(self.paths_list.clone())
}
pub fn from_paths(paths: Paths) -> Self {
QuietPaths {
paths_list: paths.to_vec(),
}
}
pub fn packed(&self) -> Self {
QuietPaths::from_paths(basic_listing(self.to_paths()).get_paths())
}
}
57 changes: 47 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//!
#[macro_use]
extern crate rocket;
use quiet_stroll::{InputPath, Paths};
use quiet_stroll::{InputPath, QuietPaths};
use rocket::http::Status;
use rocket::response::{content, status};
use rocket::serde::json::Json;
Expand All @@ -36,7 +36,7 @@ fn coffee() -> status::Custom<content::RawJson<&'static str>> {
status::Custom(Status::ImATeapot, content::RawJson("{ \"hi\": \"world\" }"))
}
#[openapi(tag = "FileSystem")]
#[post("/walk", format = "application/json", data = "<input_path>")]
#[post("/walk?<packed>", format = "application/json", data = "<input_path>")]
/// # walk
///
/// ## Description
Expand All @@ -46,11 +46,26 @@ fn coffee() -> status::Custom<content::RawJson<&'static str>> {
/// ## Tips
///
/// It is recommanded to use path with slash `/` instead of backslash `\`
fn fwalk(input_path: Json<InputPath>) -> Json<Paths> {
Json(Paths::from_walk(input_path))
///
/// ## Parameters
///
/// ### packed
///
/// You can use a filter `packed=true` or `packed=true` to pack frame sequences
fn fwalk(input_path: Json<InputPath>, packed: Option<bool>) -> Json<QuietPaths> {
let input_path: QuietPaths = QuietPaths::from_walk(input_path);
if packed.unwrap_or(false) {
Json(input_path.packed())
} else {
Json(input_path)
}
}
#[openapi(tag = "FileSystem")]
#[post("/listdir", format = "application/json", data = "<input_path>")]
#[post(
"/listdir?<packed>",
format = "application/json",
data = "<input_path>"
)]
/// # listdir
///
/// ## Description
Expand All @@ -60,11 +75,22 @@ fn fwalk(input_path: Json<InputPath>) -> Json<Paths> {
/// ## Tips
///
/// It is recommanded to use path with slash `/` instead of backslash `\`
fn flistdir(input_path: Json<InputPath>) -> Json<Paths> {
Json(Paths::from_listdir(input_path))
///
/// ## Parameters
///
/// ### packed
///
/// You can use a filter `packed=true` or `packed=true` to pack frame sequences
fn flistdir(input_path: Json<InputPath>, packed: Option<bool>) -> Json<QuietPaths> {
let input_path: QuietPaths = QuietPaths::from_listdir(input_path);
if packed.unwrap_or(false) {
Json(input_path.packed())
} else {
Json(input_path)
}
}
#[openapi(tag = "FileSystem")]
#[post("/glob", format = "application/json", data = "<input_path>")]
#[post("/glob?<packed>", format = "application/json", data = "<input_path>")]
/// # glob
///
/// ## Description
Expand All @@ -74,8 +100,19 @@ fn flistdir(input_path: Json<InputPath>) -> Json<Paths> {
/// ## Tips
///
/// It is recommanded to use path with slash `/` instead of backslash `\`
fn fglob(input_path: Json<InputPath>) -> Json<Paths> {
Json(Paths::from_glob(input_path))
///
/// ## Parameters
///
/// ### packed
///
/// You can use a filter `packed=true` or `packed=true` to pack frame sequences
fn fglob(input_path: Json<InputPath>, packed: Option<bool>) -> Json<QuietPaths> {
let input_path: QuietPaths = QuietPaths::from_glob(input_path);
if packed.unwrap_or(false) {
Json(input_path.packed())
} else {
Json(input_path)
}
}

#[launch]
Expand Down

0 comments on commit ea1031b

Please sign in to comment.