From 1fde93d48d6ea0839c66a785cadbdc1755dc0251 Mon Sep 17 00:00:00 2001 From: Alejandro Do Nascimento Mora Date: Thu, 5 Oct 2023 10:07:37 +0200 Subject: [PATCH] Improve regexs to match more cases --- src/date.rs | 1 + src/directory.rs | 3 +-- src/fixtures/PXL_20200829_205420.TS.mp4 | 0 src/organizer.rs | 2 +- src/organizer/photos.rs | 4 ++-- src/organizer/videos.rs | 16 +++++++++------- 6 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 src/fixtures/PXL_20200829_205420.TS.mp4 diff --git a/src/date.rs b/src/date.rs index ead3f21..33b80e5 100644 --- a/src/date.rs +++ b/src/date.rs @@ -3,6 +3,7 @@ use color_eyre::eyre::{eyre, Result}; /// A simple date structure that only contains the year and month. /// The components can be returned as strings. In the case of the /// months they are returned as `MM - Month Name`. +/// [Self::get_month] #[derive(Debug)] pub struct Date { year: u16, diff --git a/src/directory.rs b/src/directory.rs index d5aeacb..8530209 100644 --- a/src/directory.rs +++ b/src/directory.rs @@ -26,8 +26,7 @@ impl Iterator for FilesIter { return Some(file); } - while !self.dirs.is_empty() { - let dir = self.dirs.pop().unwrap(); + while let Some(dir) = self.dirs.pop() { let dir_entries = match fs::read_dir(dir) { Ok(entries) => entries, _ => continue, diff --git a/src/fixtures/PXL_20200829_205420.TS.mp4 b/src/fixtures/PXL_20200829_205420.TS.mp4 new file mode 100644 index 0000000..e69de29 diff --git a/src/organizer.rs b/src/organizer.rs index f9a7117..5e7172d 100644 --- a/src/organizer.rs +++ b/src/organizer.rs @@ -73,7 +73,7 @@ impl Organizer { fn move_file(file: &Path, dst_dir: &Path) -> Result<()> { if !dst_dir.is_dir() { - fs::create_dir_all(&dst_dir).wrap_err("failed to create destination dir")?; + fs::create_dir_all(dst_dir).wrap_err("failed to create destination dir")?; } let file_name = match file.file_name() { diff --git a/src/organizer/photos.rs b/src/organizer/photos.rs index 5e25a86..eb5e73f 100644 --- a/src/organizer/photos.rs +++ b/src/organizer/photos.rs @@ -37,7 +37,7 @@ impl PhotoOrganizer { PhotoOrganizer { dst_dir, date_from_filename_regex: Regex::new( - r"^IMG[-_](\d{4})(\d{2})\d{2}[-_](WA)?\d+\.(jpeg|jpg|JPG)$", + r"^(?:IMG[-_])?(\d{4})(\d{2})\d{2}[-_](?:WA)?\d+\.(jpeg|jpg|JPG)$", ) .unwrap(), } @@ -142,7 +142,7 @@ mod tests { #[test] fn should_not_organize() { let organizer = PhotoOrganizer::new(PathBuf::new()); - let extensions = vec!["mp4", "doc", ""]; + let extensions = ["mp4", "doc", ""]; for extension in extensions.iter() { assert!(!organizer.should_organize(&PathBuf::from(format!("file.{}", extension)))); } diff --git a/src/organizer/videos.rs b/src/organizer/videos.rs index adf64eb..0b2fa6d 100644 --- a/src/organizer/videos.rs +++ b/src/organizer/videos.rs @@ -14,13 +14,15 @@ pub struct VideoOrganizer { } impl VideoOrganizer { - const SUPPORTED: [&'static str; 1] = ["mp4"]; + const SUPPORTED: [&'static str; 2] = ["mp4", "avi"]; pub fn new(dst_dir: PathBuf) -> VideoOrganizer { VideoOrganizer { dst_dir, - date_from_filename_regex: Regex::new(r"^(?:VID[-_])?(\d{4})(\d{2})\d{2}[_-].+\.mp4$") - .unwrap(), + date_from_filename_regex: Regex::new( + r"^(?:VID[-_]|PXL[-_])?(\d{4})(\d{2})\d{2}[_-].+\.mp4$", + ) + .unwrap(), } } @@ -91,7 +93,7 @@ mod tests { #[test] fn should_not_organize() { let organizer = VideoOrganizer::new(PathBuf::new()); - let extensions = vec!["jpg", "doc", ""]; + let extensions = ["jpg", "doc", ""]; for extension in extensions.iter() { assert!(!organizer.should_organize(&PathBuf::from(format!("file.{}", extension)))); } @@ -109,10 +111,10 @@ mod tests { .parent() .unwrap() .join("fixtures") - .join("20200829_205420.mp4"); + .join("PXL_20200829_205420.TS.mp4"); let sub_dir = src.path().join("sub_dir"); - fs::create_dir(&sub_dir).unwrap(); - fs::copy(&video, src.path().join("20200829_205420.mp4")).unwrap(); + fs::create_dir(sub_dir).unwrap(); + fs::copy(&video, src.path().join("PXL_20200829_205420.TS.mp4")).unwrap(); let video_organizer = VideoOrganizer::new(video_dst); assert_eq!(