Skip to content

Commit

Permalink
handle partial parsing errors (#3)
Browse files Browse the repository at this point in the history
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki authored Oct 1, 2024
1 parent 3b165b3 commit 77b814f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 70 deletions.
19 changes: 9 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ serde_yaml = "0.9"
regex = "1.10"
chrono = "0.4.34"
filetime = "0.2"
kamadak-exif = "0.5.5"
# kamadak-exif = "0.5.5"
exif = { git = "https://github.com/kamadak/exif-rs.git", rev = "8523b0f06f6fe4f8eb6022d6ae0922f5c1b3477c" }
little_exif = "0.4.0"
lazy_static = "1.5.0"
clap-verbosity-flag = "2.2.1"
Expand Down
116 changes: 57 additions & 59 deletions src/datetime_tag_parser.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,67 @@
use chrono::NaiveDate;
use exif::{DateTime, Error, In, Tag, Value};
use exif::{DateTime, In, Tag, Value};
use std::fs::File;
use std::path::Path;

pub fn captures(path: &Path) -> Result<Option<chrono::DateTime<chrono::Utc>>, String> {
let file = match File::open(path) {
Ok(f) => f,
Err(e) => return Err(e.to_string()),
};
pub fn captures(path: &Path) -> Result<Option<chrono::DateTime<chrono::Utc>>, exif::Error> {
let file = File::open(path)?;

let mut bufreader = std::io::BufReader::new(&file);
let exifreader = exif::Reader::new();
match exifreader.read_from_container(&mut bufreader) {
Ok(exif) => {
//for f in exif.fields() {
// println!(
// " {}/{}: {}",
// f.ifd_num.index(),
// f.tag,
// f.display_value().with_unit(&exif)
// );
// println!(" {:?}", f.value);
//}
match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) {
Some(field) => match field.value {
Value::Ascii(ref vec) if !vec.is_empty() => {
match DateTime::from_ascii(&vec[0]) {
Ok(datetime) => Ok(Some(
NaiveDate::from_ymd_opt(
i32::from(datetime.year),
u32::from(datetime.month),
u32::from(datetime.day),
)
.unwrap()
.and_hms_milli_opt(
u32::from(datetime.hour),
u32::from(datetime.minute),
u32::from(datetime.second),
0,
)
.unwrap()
.and_utc(),
)),
Err(e) => Err(e.to_string()),
}
}
_ => Ok(None),
},
None => Ok(None),
}
let result = exif::Reader::new()
.continue_on_error(true)
.read_from_container(&mut bufreader);

let exif = if let Err(exif::Error::PartialResult(partial)) = result {
let (exif, _) = partial.into_inner();
exif
} else {
result?
};

//if let Some(field) = exif.get_field(Tag::DateTime, In::PRIMARY) {
// match field.value {
// Value::Ascii(ref vec) if !vec.is_empty() => {
// if let Ok(datetime) = DateTime::from_ascii(&vec[0]) {
// println!("Year of DateTime is {}.", datetime.year);
// }
// }
// _ => {}
// }
//}
}
Err(e) => match e {
Error::NotFound(_) => Ok(None),
_ => Err(e.to_string()),
//for f in exif.fields() {
// println!(
// " {}/{}: {}",
// f.ifd_num.index(),
// f.tag,
// f.display_value().with_unit(&exif)
// );
// println!(" {:?}", f.value);
//}

match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) {
Some(field) => match field.value {
Value::Ascii(ref vec) if !vec.is_empty() => {
let datetime = DateTime::from_ascii(&vec[0])?;
Ok(Some(
NaiveDate::from_ymd_opt(
i32::from(datetime.year),
u32::from(datetime.month),
u32::from(datetime.day),
)
.unwrap()
.and_hms_milli_opt(
u32::from(datetime.hour),
u32::from(datetime.minute),
u32::from(datetime.second),
0,
)
.unwrap()
.and_utc(),
))
}
_ => Ok(None),
},
None => Ok(None),
}

//if let Some(field) = exif.get_field(Tag::DateTime, In::PRIMARY) {
// match field.value {
// Value::Ascii(ref vec) if !vec.is_empty() => {
// if let Ok(datetime) = DateTime::from_ascii(&vec[0]) {
// println!("Year of DateTime is {}.", datetime.year);
// }
// }
// _ => {}
// }
//}
}

0 comments on commit 77b814f

Please sign in to comment.