Skip to content

Commit

Permalink
exif: Implement Error for Error
Browse files Browse the repository at this point in the history
  • Loading branch information
sophie-h committed Jun 13, 2024
1 parent f5ebc71 commit 24eea4b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ once_cell = "1.19.0"
paste = "1.0.15"
serde = { version = "1.0.202", features = ["derive"] }
tracing = "0.1"
thiserror = "1.0.61"
1 change: 1 addition & 0 deletions gufo-exif/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ workspace = true
[dependencies]
gufo-common.workspace = true
tracing.workspace = true
thiserror.workspace = true

[dev-dependencies]
gufo-jpeg.workspace = true
33 changes: 30 additions & 3 deletions gufo-exif/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use gufo_common::utils::{
AdditionOverflowError, ConversionOverflowError, SubstractionOverflowError,
};
Expand All @@ -6,40 +8,65 @@ use crate::internal::{Ifd, TagIfd, Type};

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("Unkown byte order: {0:x?}")]
UnkownByteOrder([u8; 2]),
#[error("Wrong magic bytes: {0:x?}")]
MagicBytesWrong(u16),
Io(std::io::Error),
#[error("IO error: {0}")]
Io(Arc<std::io::Error>),
#[error("Tag not found: {0:?}")]
TagNotFound(TagIfd),
#[error("Ifd should terminate: {0:?}")]
IfdShouldTerminate(Ifd),
#[error("OffsetTooLarge")]
OffsetTooLarge,
#[error("LookupEof")]
LookupEof,
#[error("LookupEof")]
ByteOrderEof,
#[error("ByteOrderEof")]
MagicBytesEof,
#[error("MagicBytesEof")]
EntryEof,
#[error("EntryEof")]
NumerEntriesEof,
#[error("NumerEntriesEof")]
InvalidLookupOffset,
#[error("InvalidLookupOffset")]
DataSizeTooLarge,
#[error("DataSizeTooLarge")]
IfdNotFound,
#[error("IfdNotFound")]
WrongTypeGeneric,
#[error("WrongTypeGeneric")]
WrongType {
expected: (u32, Type),
actual: (u32, Type),
},
#[error("OffsetInvalid: {0}")]
OffsetInvalid(i64),
#[error("OffsetInsteadOfValue")]
OffsetInsteadOfValue,
#[error("ValueInsteadOfOffset")]
ValueInsteadOfOffset,
#[error("IncompatibleValue")]
IncompatibleValue,
#[error("AdditionOverflow")]
AdditionOverflow,
#[error("SubstractionOverflowError")]
SubstractionOverflowError,
#[error("ConversionOverflowError")]
ConversionOverflowError,
#[error("EntryNotFound")]
EntryNotFound,
}

impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
Self::Io(Arc::new(value))
}
}

Expand Down
1 change: 1 addition & 0 deletions gufo-xmp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ rust-version.workspace = true

[dependencies]
xml-rs = "0.8.19"
thiserror.workspace = true
21 changes: 7 additions & 14 deletions gufo-xmp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;
use std::fmt::Display;
use std::io::Cursor;
use std::sync::Arc;

use xml::name::OwnedName;
use xml::reader::XmlEvent;
Expand Down Expand Up @@ -39,23 +40,15 @@ pub struct Xmp {
entries: BTreeMap<Ref, String>,
}

#[derive(Debug)]
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("XmlReader: {0}")]
XmlReader(xml::reader::Error),
XmlWriter(xml::writer::Error),
#[error("XmlWriter: {0}")]
XmlWriter(Arc<xml::writer::Error>),
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::XmlReader(err) => write!(f, "XmlReader: {err}"),
Self::XmlWriter(err) => write!(f, "XmlWriter: {err}"),
}
}
}

impl std::error::Error for Error {}

impl From<xml::reader::Error> for Error {
fn from(value: xml::reader::Error) -> Self {
Self::XmlReader(value)
Expand All @@ -64,7 +57,7 @@ impl From<xml::reader::Error> for Error {

impl From<xml::writer::Error> for Error {
fn from(value: xml::writer::Error) -> Self {
Self::XmlWriter(value)
Self::XmlWriter(Arc::new(value))
}
}

Expand Down

0 comments on commit 24eea4b

Please sign in to comment.