diff options
Diffstat (limited to 'src/jpeg/segment.rs')
-rw-r--r-- | src/jpeg/segment.rs | 42 |
1 files changed, 10 insertions, 32 deletions
diff --git a/src/jpeg/segment.rs b/src/jpeg/segment.rs index b20ba3b..cd1a651 100644 --- a/src/jpeg/segment.rs +++ b/src/jpeg/segment.rs @@ -1,9 +1,8 @@ use std::{mem::size_of, usize}; use img_parts::jpeg::{markers, Jpeg, JpegSegment}; -use thiserror::Error; -use crate::codec::Codec; +use crate::{codec::Codec, CodecError}; /// Codec for storing payload data in JPEG comment (COM) segments. Can store an arbitrary amount of /// data, as long as the number of comment segments does not exceed u64::MAX. @@ -14,21 +13,13 @@ pub struct JpegSegmentCodec { } impl Codec for JpegSegmentCodec { - type Carrier = Vec<u8>; - type Payload = Vec<u8>; - type Output = Self::Carrier; - type Error = JpegSegmentError; - - fn encode<C, P>(&self, carrier: C, payload: P) -> Result<Self::Output, Self::Error> - where - C: Into<Self::Carrier>, - P: Into<Self::Payload>, + fn encode(&self, carrier: &[u8], payload: &[u8]) -> Result<Vec<u8>, CodecError> { - let mut jpeg = match Jpeg::from_bytes(carrier.into().into()) { - Ok(image) => image, - Err(err) => return Err(JpegSegmentError::ParseFailed { inner: err }) + let mut jpeg = match Jpeg::from_bytes(carrier.to_vec().into()) { + Ok(v) => v, + Err(e) => return Err(CodecError::DependencyError(e.to_string())) }; - let mut payload_bytes: Self::Carrier = payload.into(); + let mut payload_bytes = payload.to_vec(); let segment_count = ((payload_bytes.len() + size_of::<u64>()) as u64).div_ceil((u16::MAX as usize - size_of::<u16>()) as u64); payload_bytes.splice(0..0, segment_count.to_le_bytes()); for (index, payload_chunk) in payload_bytes.chunks(u16::MAX as usize - size_of::<u16>()).enumerate() { @@ -38,13 +29,11 @@ impl Codec for JpegSegmentCodec { Ok(jpeg.encoder().bytes().to_vec()) } - fn decode<E>(&self, encoded: E) -> Result<(Self::Carrier, Self::Payload), Self::Error> - where - E: Into<Self::Output>, + fn decode(&self, encoded: &[u8]) -> Result<(Vec<u8>, Vec<u8>), CodecError> { - let mut jpeg = match Jpeg::from_bytes(encoded.into().into()) { - Ok(image) => image, - Err(err) => return Err(JpegSegmentError::ParseFailed { inner: err }) + let mut jpeg = match Jpeg::from_bytes(encoded.to_vec().into()) { + Ok(v) => v, + Err(e) => return Err(CodecError::DependencyError(e.to_string())) }; let segment = jpeg.segments_mut().remove(self.start_index); let segment_bytes = segment.contents(); @@ -68,14 +57,3 @@ impl Default for JpegSegmentCodec { } } } - -/// Errors thrown by the JPEG segment codec. -#[derive(Error, Debug)] -pub enum JpegSegmentError { - /// Parsing JPEG data failed. - #[error("Failed to parse JPEG data: {inner:?}")] - ParseFailed { - /// Error thrown by parser. - inner: img_parts::Error, - } -} |