summaryrefslogtreecommitdiff
path: root/src/picture.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/picture.rs')
-rw-r--r--src/picture.rs75
1 files changed, 41 insertions, 34 deletions
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<String>,
+ taken: Option<String>,
hash: String,
pub path: PathBuf,
pub file_name: String,
+
+ #[serde(skip)]
+ pub taken_chrono: Option<NaiveDateTime>,
}
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<io::Error> for LoadError {
- fn from(error: io::Error) -> Self {
- Self::Io(error)
- }
-}
-
-impl From<exif::Error> for LoadError {
- fn from(error: exif::Error) -> Self {
- Self::ExifParser(error)
- }
-}
-
-impl From<io::Error> for ConversionError {
- fn from(error: io::Error) -> Self {
- Self::Io(error)
- }
-}
-
-impl From<ImageError> for ConversionError {
- fn from(error: ImageError) -> Self {
- Self::ImageError(error)
- }
-}