-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduces functionality for optionally returning incomplete pixel data #252
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,6 @@ pub enum UnsupportedFeature { | |
} | ||
|
||
/// Errors that can occur while decoding a JPEG image. | ||
#[derive(Debug)] | ||
pub enum Error { | ||
/// The image is not formatted properly. The string contains detailed information about the | ||
/// error. | ||
|
@@ -41,6 +40,13 @@ pub enum Error { | |
Io(IoError), | ||
/// An internal error occurred while decoding the image. | ||
Internal(Box<dyn StdError + Send + Sync + 'static>), //TODO: not used, can be removed with the next version bump | ||
/// An error that occurred during the decode, but allows for incomplete image pixel data to be returned with `try_recover()` | ||
Recoverable { | ||
/// Error occurred while decoding the image | ||
err: Box<Self>, | ||
/// Incomplete pixel data of the image | ||
pixels: Vec<u8> | ||
} | ||
Comment on lines
+44
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the variant stored a Compare roughly design of |
||
} | ||
|
||
impl fmt::Display for Error { | ||
|
@@ -50,6 +56,19 @@ impl fmt::Display for Error { | |
Error::Unsupported(ref feat) => write!(f, "unsupported JPEG feature: {:?}", feat), | ||
Error::Io(ref err) => err.fmt(f), | ||
Error::Internal(ref err) => err.fmt(f), | ||
Error::Recoverable { ref err, .. } => err.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Debug for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::Format(err) => f.debug_tuple("Format").field(err).finish(), | ||
Self::Unsupported(err) => f.debug_tuple("Unsupported").field(err).finish(), | ||
Self::Io(err) => f.debug_tuple("Io").field(err).finish(), | ||
Self::Internal(err) => f.debug_tuple("Internal").field(err).finish(), | ||
Self::Recoverable { ref err, .. } => f.debug_tuple("Recoverable").field(err).finish(), | ||
} | ||
} | ||
} | ||
|
@@ -69,3 +88,20 @@ impl From<IoError> for Error { | |
Error::Io(err) | ||
} | ||
} | ||
|
||
/// A trait that, if supported, returns incomplete image pixel data | ||
pub trait TryRecover { | ||
/// Returns incomplete image pixel data if supported. Otherwise, simply passes through the original value. | ||
fn try_recover(self) -> Self; | ||
} | ||
|
||
impl TryRecover for Result<Vec<u8>> { | ||
fn try_recover(self) -> Self { | ||
self.or_else(|err| match err { | ||
Error::Recoverable { pixels, .. } => { | ||
Ok(pixels) | ||
}, | ||
_ => Err(err), | ||
}) | ||
Comment on lines
+98
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather provide this as an inherent method of
Each usage site can easily emulate the above trait by calling |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to ingore all IO errors? see
io::Error::kind()
andErrorKind::UnexpectedEof
, I would start with a small set.