Skip to content

Commit

Permalink
Write recursive directory search in Rust
Browse files Browse the repository at this point in the history
Implement runtime comparison for `walkdir`
Simplify code and update docstrings
  • Loading branch information
dormant-user committed Feb 17, 2024
1 parent 5e0b6e8 commit c4b6aff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
24 changes: 12 additions & 12 deletions src/routes/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,20 @@ pub async fn stream(config: web::Data<Arc<squire::settings::Config>>,
// Load the default response body and re-construct with subtitles if present
let mut response_body = landing.render(context!(
video_title => &filepath, path => render_path,
previous => &iter.previous,
next => &iter.next,
previous_title => &iter.previous,
next_title => &iter.next,
previous => &rust_iter.previous,
next => &rust_iter.next,
previous_title => &rust_iter.previous,
next_title => &rust_iter.next,
)).unwrap();
let subtitle = subtitles(__target, &filepath);
if subtitle.vtt.exists() {
let sfx_file = format!("/track?file={}", url_encode(&subtitle.vtt_file));
response_body = landing.render(context!(
video_title => &filepath, path => render_path,
previous => &iter.previous,
next => &iter.next,
previous_title => &iter.previous,
next_title => &iter.next,
previous => &rust_iter.previous,
next => &rust_iter.next,
previous_title => &rust_iter.previous,
next_title => &rust_iter.next,
track => sfx_file
)).unwrap();
} else if subtitle.srt.exists() {
Expand All @@ -192,10 +192,10 @@ pub async fn stream(config: web::Data<Arc<squire::settings::Config>>,
let sfx_file = format!("/track?file={}", url_encode(&subtitle.vtt_file));
response_body = landing.render(context!(
video_title => &filepath, path => render_path,
previous => &iter.previous,
next => &iter.next,
previous_title => &iter.previous,
next_title => &iter.next,
previous => &rust_iter.previous,
next => &rust_iter.next,
previous_title => &rust_iter.previous,
next_title => &rust_iter.next,
track => sfx_file
)).unwrap();
}
Expand Down
35 changes: 25 additions & 10 deletions src/squire/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ fn natural_sort_key(filename: &str) -> Vec<std::result::Result<i32, String>> {
}


/// Retrieves content information for all streams.
///
/// # Arguments
///
/// * `config` - Configuration data for the application.
///
/// # Returns
///
/// A `ContentPayload` struct representing the content of all streams.
pub fn get_all_stream_content(config: &Config) -> ContentPayload {
let mut payload = ContentPayload::default();

Expand All @@ -76,16 +85,21 @@ pub fn get_all_stream_content(config: &Config) -> ContentPayload {
let components: &Vec<_> = &path.components().collect();
if components.len() == 1 {
let mut entry_map = HashMap::new();
entry_map.insert("path".to_string(), format!("stream/{}", &file_name));
entry_map.insert("name".to_string(), file_name.to_string());
entry_map.insert("path".to_string(), format!("stream/{}", file_name));
payload.files.push(entry_map);
} else {
let filename = &path.file_name().unwrap().to_str().unwrap();
let appender = &path.to_string_lossy().replace(filename, "");
let valuated = appender.strip_suffix("/").unwrap();
/*
path.components(): returns an iterator over the components of the path
.rev(): reverses the order of the iterator
.skip(1): skips the first (originally last) component of the reversed path
*/
let skimmed: String = path.components().rev().skip(1)
.collect::<Vec<_>>().iter().rev()
.collect::<PathBuf>().to_string_lossy().to_string();
let mut entry_map = HashMap::new();
entry_map.insert("name".to_string(), valuated.to_string());
entry_map.insert("path".to_string(), format!("stream/{}", valuated));
entry_map.insert("path".to_string(), format!("stream/{}", &skimmed));
entry_map.insert("name".to_string(), skimmed);
if payload.directories.contains(&entry_map) { continue; }
payload.directories.push(entry_map);
}
Expand All @@ -110,7 +124,7 @@ pub fn get_all_stream_content(config: &Config) -> ContentPayload {
/// # Returns
///
/// A `ContentPayload` struct representing the content of the specified directory.
pub fn get_dir_stream_content(parent: &str, subdir: &str, file_formats: &[String]) -> ContentPayload {
pub fn get_dir_stream_content(parent: &str, subdir: &str, file_formats: &Vec<String>) -> ContentPayload {
let mut files = Vec::new();
for entry in fs::read_dir(parent).unwrap().flatten() {
let file_name = entry.file_name().into_string().unwrap();
Expand Down Expand Up @@ -140,16 +154,17 @@ pub struct Iter {
pub next: Option<String>,
}

/// Retrieves iterator information from Python based on the provided arguments.
/// Retrieves the previous and/or next file to the currently streaming file.
///
/// # Arguments
///
/// * `args` - A tuple containing a stream identifier and references to two strings.
/// * `filepath` - File that is requested for streaming.
/// * `file_formats` - Vector of file formats (as String) that are allowed.
///
/// # Returns
///
/// An `Iter` struct representing the iterator information.
pub fn get_iter(filepath: &PathBuf, file_formats: &[String]) -> Iter {
pub fn get_iter(filepath: &Path, file_formats: &[String]) -> Iter {
let parent = filepath.parent().unwrap();
let mut dir_content: Vec<String> = fs::read_dir(parent)
.ok().unwrap()
Expand Down

0 comments on commit c4b6aff

Please sign in to comment.