Skip to content

Commit

Permalink
Improve regexs to match more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrodnm committed Oct 5, 2023
1 parent 1ac7758 commit 1fde93d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/organizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/organizer/photos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down Expand Up @@ -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))));
}
Expand Down
16 changes: 9 additions & 7 deletions src/organizer/videos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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))));
}
Expand All @@ -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!(
Expand Down

0 comments on commit 1fde93d

Please sign in to comment.