Skip to content

Commit

Permalink
Add a new struct FormatTemplate to propose a easy use as lib
Browse files Browse the repository at this point in the history
Add documentation of struct `FormatTemplate`
  • Loading branch information
Serhat Baykal committed Nov 12, 2024
1 parent d569562 commit 55dbfec
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
11 changes: 6 additions & 5 deletions benches/mega.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use framels::{
basic_listing, extended_listing, parse_dir,
paths::{Paths, PathsPacked},
FormatTemplate,
};

fn generate_paths(n: u64) -> Paths {
Expand All @@ -18,19 +19,19 @@ fn generate_paths(n: u64) -> Paths {
fn parse_and_run() {
let source = "./samples/big".to_string();
let paths: Paths = parse_dir(&source);
let _results: PathsPacked = basic_listing(paths, false);
let _results: PathsPacked = basic_listing(paths, false, FormatTemplate::default().format);
}

fn small_parse_and_run() {
let source = "./samples/big".to_string();
let paths: Paths = parse_dir(&source);
let _results: PathsPacked = basic_listing(paths, false);
let _results: PathsPacked = basic_listing(paths, false, FormatTemplate::default().format);
}

fn exr_reading() {
let source = "./samples/big/".to_string();
let paths: Paths = parse_dir(&source);
let _results: PathsPacked = extended_listing(source, paths, false);
let _results: PathsPacked = extended_listing(source, paths, false, FormatTemplate::default().format);
}

fn criterion_benchmark(c: &mut Criterion) {
Expand All @@ -39,10 +40,10 @@ fn criterion_benchmark(c: &mut Criterion) {
for i in [1u64, 10u64, 100u64, 1000u64, 10000u64].iter() {
let data_set = generate_paths(*i);
group.bench_with_input(BenchmarkId::new("Mono", i), i, |b, _i| {
b.iter(|| basic_listing(data_set.clone(), false))
b.iter(|| basic_listing(data_set.clone(), false, FormatTemplate::default().format))
});
group.bench_with_input(BenchmarkId::new("Multi", i), i, |b, _i| {
b.iter(|| basic_listing(data_set.clone(), true))
b.iter(|| basic_listing(data_set.clone(), true, FormatTemplate::default().format))
});
}
group.finish();
Expand Down
74 changes: 61 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,31 @@
//! similar to `rvls -l`
//! It take a `Vec<String>` of entries as an input
//! - Pack the frames
//! - Print the metada if the sequence is an exr sequence
//! - Print the metadata if the sequence is an exr sequence
//! - Return a Vector of path packed
//!
//! ### Formating the output
//!
//! The output can be formated using the [FormatTemplate] struct.
//!
//! Three formats are available, with `toto.160.jpg` as an example:
//! - default : `{name}{sep}{padding}.{ext}@{first_frame}-{last_frame}` => `toto.***.jpg@158-179`
//! - buf : `{name}{sep}[{first_frame}:{last_frame}].{ext}` => `toto.[158:179].jpg`
//! - nuke : `{name}{sep}.{ext} {first_frame}-{last_frame}`=> `toto.jpg 158-179`
//!
//!
//! ### Example
//!
//! ```rust
//! use framels::{basic_listing, extended_listing, parse_dir, paths::{Paths,Join}, recursive_dir};
//! use framels::{basic_listing, extended_listing, parse_dir, paths::{Paths,Join}, recursive_dir, FormatTemplate};
//!
//! fn main() {
//! // Perform directory listing
//! let in_paths: Paths = parse_dir("./samples/small");
//!
//! // Generate results based on arguments
//! let results: String = basic_listing(in_paths, false,
//! "{name}{sep}{padding}.{ext}@{first_frame}-{last_frame}".to_string()).get_paths().join("\n");
//! FormatTemplate::default().format).get_paths().join("\n");
//!
//! println!("{}", results)
//! }
Expand Down Expand Up @@ -343,8 +353,47 @@ fn group_continuity(data: &[isize]) -> Vec<Vec<isize>> {
result.iter().map(|x| x.to_vec()).collect()
}

/// ## Basic listing of the library
/// ### Description
/// Structure to store output string format templates used in the library.
/// These templates are strings that can be used with the [strfmt()] crate to
/// model the output.
pub struct FormatTemplate {
pub format: &'static str,
}

impl Default for FormatTemplate {
/// Default format template
///
/// `toto.160.jpg` => `toto.***.jpg@158-179`
fn default() -> Self{
FormatTemplate {
format: "{name}{sep}{padding}.{ext}@{first_frame}-{last_frame}"
}
}
}

impl FormatTemplate {
/// Buff format template
///
/// `toto.160.jpg` => `toto.[158:179].jpg`
pub fn buf_format () -> Self {
FormatTemplate {
format: "{name}{sep}[{first_frame}:{last_frame}].{ext}"
}
}
/// Nuke format template
///
/// `toto.160.jpg` => `toto.jpg 158-179`
pub fn nuke_format() -> Self {
FormatTemplate {
format: "{name}{sep}.{ext} {first_frame}-{last_frame}"
}
}
}

/// # basic_listing
///
///
/// ## Description
///
/// This function is the main function of the library it use a list of
/// filename as in input and pack the frame sequences using a new filename
Expand Down Expand Up @@ -372,26 +421,26 @@ fn group_continuity(data: &[isize]) -> Vec<Vec<isize>> {
/// ### Example as a library
///
/// ```rust
/// use framels::{basic_listing, parse_dir, paths::{Paths,Join}};
/// use framels::{basic_listing, parse_dir, paths::{Paths,Join}, FormatTemplate};
///
/// fn main() {
/// // Perform directory listing
/// let in_paths: Paths = parse_dir("./samples/small");
///
/// // Generate results based on arguments
/// let results: String = basic_listing(in_paths, false, "{name}{sep}{padding}.{ext}@{first_frame}-{last_frame}".to_string()).get_paths().join("\n");
/// let results: String = basic_listing(in_paths, false, FormatTemplate::default().format).get_paths().join("\n");
///
/// println!("{}", results)
/// }
/// ```
pub fn basic_listing(frames: Paths, multithreaded: bool, format: String) -> PathsPacked {
pub fn basic_listing(frames: Paths, multithreaded: bool, format: &str) -> PathsPacked {
let frames_dict = parse_result(frames, multithreaded);
let mut frames_list: Vec<String> = frames_dict
.into_par_iter()
.map(|(key, value)| {
match value {
None => key,
Some(f) => match strfmt(&format, &f.to_hashmap()) {
Some(f) => match strfmt(format, &f.to_hashmap()) {
Ok(s) => s,
Err(e) => {
eprint!("Error formatting string: {}", e);
Expand Down Expand Up @@ -435,7 +484,7 @@ fn get_exr_metada(root_path: &String, path: &String) -> String {
///
/// It take a `Vec<String>` of entries as an input
/// - Pack the frames
/// - Print the metada if the sequence is an exr sequence
/// - Print the metadata if the sequence is an exr sequence
/// - Return a Vector of path packed
///
/// ## Example
Expand All @@ -448,7 +497,7 @@ fn get_exr_metada(root_path: &String, path: &String) -> String {
/// ```bash
/// ./samples/small/foo.exr Not an exr
/// ```
pub fn extended_listing(root_path: String, frames: Paths, multithreaded: bool) -> PathsPacked {
pub fn extended_listing(root_path: String, frames: Paths, multithreaded: bool, format: &str) -> PathsPacked {
let frames_dict = parse_result(frames, multithreaded);
let mut out_frames: PathsPacked = PathsPacked::new_empty();
for (key, value) in frames_dict {
Expand All @@ -466,10 +515,9 @@ pub fn extended_listing(root_path: String, frames: Paths, multithreaded: bool) -
sep = f.sep,
ext = f.ext
);
dbg!(&new_path);
out_frames.push_metadata(get_exr_metada(&root_path, &new_path));
out_frames.push_paths(PathBuf::from(
strfmt("{name}@{first_frame}-{last_frame}", &f.to_hashmap()).unwrap(),
strfmt(&format, &f.to_hashmap()).unwrap(),
));
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use framels::{
basic_listing, extended_listing, parse_dir,
paths::{Join, Paths},
recursive_dir,
FormatTemplate,
};
mod tree;
use tree::run_tree;
Expand Down Expand Up @@ -57,10 +58,9 @@ fn main() {
};

let format = match args.format.as_str() {
"default" => "{name}{sep}{padding}.{ext}@{first_frame}-{last_frame}".to_string(),
"nuke" => "{name}{sep}.{ext} {first_frame}-{last_frame}".to_string(),
"buf" => "{name}{sep}[{first_frame}:{last_frame}].{ext}".to_string(),
&_ => todo!(),
"nuke" => FormatTemplate::nuke_format().format,
"buf" => FormatTemplate::buf_format().format,
_ => FormatTemplate::default().format,
};

// Choose listing function based on arguments
Expand All @@ -73,6 +73,7 @@ fn main() {
},
in_paths,
args.multithread,
format
)
} else {
basic_listing(in_paths, args.multithread, format)
Expand Down

0 comments on commit 55dbfec

Please sign in to comment.