From f0648bcc4e7a6b7ce3f59013f25413a667dc507a Mon Sep 17 00:00:00 2001 From: Julian T Date: Tue, 27 Jul 2021 17:46:28 +0200 Subject: Sort by timestamps and switch to thiserror crate --- src/picture.rs | 75 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 34 deletions(-) (limited to 'src/picture.rs') diff --git a/src/picture.rs b/src/picture.rs index 3f78f90..d72fa84 100644 --- a/src/picture.rs +++ b/src/picture.rs @@ -6,28 +6,54 @@ use image::io::Reader as ImageReader; use image::error::ImageError; use image::imageops; use serde::Serialize; +use chrono::naive::NaiveDateTime; use crate::context::Context; -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum LoadError { - PathError, - Io(io::Error), - ExifParser(exif::Error) + #[error("not a valid path: `{0}`")] + PathError(String), + #[error("loading configuration file")] + Io { + #[from] + source: io::Error, + }, + #[error("parsing exif data")] + ExifParser { + #[from] + source: exif::Error, + }, + #[error("parsing taken datetime")] + ExifTimestamp { + #[from] + source: chrono::ParseError, + }, } -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum ConversionError { - Io(io::Error), - ImageError(ImageError), + #[error("reading picture from file")] + Io { + #[from] + source: io::Error, + }, + #[error("loading image")] + Image { + #[from] + source: ImageError, + }, } #[derive(Debug, Serialize)] pub struct Picture { - pub taken: Option, + taken: Option, hash: String, pub path: PathBuf, pub file_name: String, + + #[serde(skip)] + pub taken_chrono: Option, } pub struct Converter<'a> { @@ -67,16 +93,21 @@ impl Picture { } }; + let taken = taken.map(|taken| + NaiveDateTime::parse_from_str(&taken, "%Y-%m-%d %H:%M:%S") + ).transpose()?; + // Move back to start of file for hashing reader.seek(io::SeekFrom::Start(0))?; Ok(Picture { - taken, + taken_chrono: taken, + taken: taken.map(|taken| taken.format("%Y-%m-%d").to_string()), hash: hash_reader(&mut reader)?, path: path.to_path_buf(), file_name: match path.file_name() { Some(fname) => Ok(fname.to_string_lossy().to_string()), - None => Err(LoadError::PathError), + None => Err(LoadError::PathError(path.to_string_lossy().to_string())), }?, }) } @@ -130,27 +161,3 @@ impl Converter<'_> { } } } - -impl From for LoadError { - fn from(error: io::Error) -> Self { - Self::Io(error) - } -} - -impl From for LoadError { - fn from(error: exif::Error) -> Self { - Self::ExifParser(error) - } -} - -impl From for ConversionError { - fn from(error: io::Error) -> Self { - Self::Io(error) - } -} - -impl From for ConversionError { - fn from(error: ImageError) -> Self { - Self::ImageError(error) - } -} -- cgit v1.2.3