Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Sep 25, 2024
1 parent afe9f69 commit ea38665
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
30 changes: 19 additions & 11 deletions src/date_parser.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use chrono::{DateTime, Utc};
use chrono::{DateTime, NaiveDate, Utc};
use lazy_static::lazy_static;
use regex::Regex;
use std::str::FromStr;

// https://github.com/waltzofpearls/dateparser/blob/main/dateparser/src/lib.rs
// https://github.com/waltzofpearls/dateparser/blob/main/dateparser/src/datetime.rs#L26

pub fn parse(input: &str) -> Result<DateTime<Utc>, String> {
pub fn parse(input: &str) -> Option<DateTime<Utc>> {
img_yyyymmdd(input)
.or_else(|| yyyymmdd(input))
.unwrap_or_else(|| Err(format!("{} did not match any formats.", input)))
}

fn img_yyyymmdd(input: &str) -> Option<Result<DateTime<Utc>, String>> {
fn img_yyyymmdd(input: &str) -> Option<DateTime<Utc>> {
lazy_static! {
static ref RE: Regex =
Regex::new(r"^IMG-(?P<yyyy>[0-9]{4})(?P<mm>[0-9]{2})(?P<dd>[0-9]{2})$",).unwrap();
Regex::new(r"^(IMG[-_])*(?P<yyyy>[0-9]{4})(?P<mm>[0-9]{2})(?P<dd>[0-9]{2}).*$",)
.unwrap();
}

if !RE.is_match(input) {
Expand All @@ -24,14 +24,22 @@ fn img_yyyymmdd(input: &str) -> Option<Result<DateTime<Utc>, String>> {
if let Some(caps) = RE.captures(input) {
if let Some(matched_yyyy) = caps.name("yyyy") {
if let Some(matched_mm) = caps.name("mm") {
if let Some(matched_dd) = caps.name("dd") {}
if let Some(matched_dd) = caps.name("dd") {
return Some(
NaiveDate::from_ymd_opt(
FromStr::from_str(matched_yyyy.as_str()).unwrap(),
FromStr::from_str(matched_mm.as_str()).unwrap(),
FromStr::from_str(matched_dd.as_str()).unwrap(),
)
.unwrap()
.and_hms_milli_opt(0, 0, 0, 0)
.unwrap()
.and_utc(),
);
}
}
}
}

None
}

fn yyyymmdd(input: &str) -> Option<Result<DateTime<Utc>, String>> {
None
}
7 changes: 5 additions & 2 deletions src/datetime_tag_parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use exif::{DateTime, In, Tag, Value};
use exif::{DateTime, Error, In, Tag, Value};
use std::fs::File;
use std::path::Path;

Expand Down Expand Up @@ -62,6 +62,9 @@ pub fn captures(path: &Path) -> Result<Option<chrono::DateTime<chrono::Utc>>, St
// }
//}
}
Err(e) => Err(e.to_string()),
Err(e) => match e {
Error::NotFound(_) => Ok(None),
_ => Err(e.to_string()),
},
}
}
5 changes: 2 additions & 3 deletions src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl FromIterator<DirEntry> for Stats {
None => {
s.num_files_missing_datetime_tag += 1;
match crate::date_parser::parse(i.file_name().to_str().unwrap()) {
Ok(tag) => match datetime_tag_writer::write_tag(i.path(), tag) {
Some(tag) => match datetime_tag_writer::write_tag(i.path(), tag) {
Ok(_) => s.num_files_successfully_tagged += 1,
Err(e) => {
println!("[ERROR] tagging metadata: {e:?}");
Expand All @@ -72,8 +72,7 @@ impl FromIterator<DirEntry> for Stats {
continue;
}
},
Err(e) => {
println!("[ERROR] parsing filename: {e:?}");
None => {
s.num_files_failed_filename_parsing += 1;
s.filenames_name_unparseable
.push(i.path().display().to_string());
Expand Down

0 comments on commit ea38665

Please sign in to comment.