From 92ce0e4a8990373542ffcfb7dc3a0b488e2020ab Mon Sep 17 00:00:00 2001 From: marmal95 Date: Sat, 14 Sep 2024 17:47:20 +0200 Subject: [PATCH] Replace rest Results with anyhow --- src/coder/decoder/decode.rs | 2 +- src/coder/decoder/header_decoder.rs | 37 ++++++++++++++++++----------- src/coder/encoder/alpha_encoder.rs | 24 +++++++++++-------- src/coder/encoder/encode.rs | 10 ++++---- src/coder/encoder/header_encoder.rs | 33 ++++++++++++++----------- src/coder/encoder/rgb_encoder.rs | 12 ++++++---- src/main.rs | 6 ++--- 7 files changed, 73 insertions(+), 51 deletions(-) diff --git a/src/coder/decoder/decode.rs b/src/coder/decoder/decode.rs index 68ccd27..5890776 100644 --- a/src/coder/decoder/decode.rs +++ b/src/coder/decoder/decode.rs @@ -8,7 +8,7 @@ pub trait Decode { .decode_length() .ok_or(self.not_available("filename length"))?; - let file_name = self + let file_name: Vec = self .decode_data(file_name_length) .ok_or(self.not_available("filename"))?; let file_name = String::from_utf8(file_name)?; diff --git a/src/coder/decoder/header_decoder.rs b/src/coder/decoder/header_decoder.rs index 2af65d8..8cbf418 100644 --- a/src/coder/decoder/header_decoder.rs +++ b/src/coder/decoder/header_decoder.rs @@ -1,9 +1,11 @@ +use anyhow::{anyhow, Result}; + use crate::coder::{ error::HeaderDecodeError, header::{AlgHeader, AlphaHeader, Header, RgbHeader, ALPHA_MODE, RGB_MODE}, }; -pub fn decode(buffer: &[u8]) -> Result { +pub fn decode(buffer: &[u8]) -> Result
{ let mut iter = buffer.iter().skip(3).step_by(4); let mode = *iter.next().ok_or(HeaderDecodeError( "Not enough data to decode mode.".to_string(), @@ -13,14 +15,16 @@ pub fn decode(buffer: &[u8]) -> Result { Ok(Header::new(mode, alg_header)) } -fn decode_alg_header<'a, I>(mode: u8, iter: &mut I) -> Result +fn decode_alg_header<'a, I>(mode: u8, iter: &mut I) -> Result where I: Iterator, { match mode { ALPHA_MODE => Ok(AlgHeader::Alpha(decode_alpha())), RGB_MODE => Ok(AlgHeader::Rgb(decode_rgb(iter)?)), - _ => Err(HeaderDecodeError("Unknown mode in header.".to_string())), + _ => Err(anyhow!(HeaderDecodeError( + "Unknown mode in header.".to_string() + ))), } } @@ -28,7 +32,7 @@ fn decode_alpha() -> AlphaHeader { AlphaHeader {} } -fn decode_rgb<'a, I>(iter: &mut I) -> Result +fn decode_rgb<'a, I>(iter: &mut I) -> Result where I: Iterator, { @@ -73,10 +77,11 @@ mod tests { let buffer = Vec::new(); let decoded = super::decode(&buffer); assert_eq!( - decoded, - Err(HeaderDecodeError( - "Not enough data to decode mode.".to_string() - )) + decoded + .unwrap_err() + .downcast::() + .unwrap(), + HeaderDecodeError("Not enough data to decode mode.".to_string()) ); } @@ -85,10 +90,11 @@ mod tests { let buffer = vec![0, 0, 0, RGB_MODE]; let decoded = super::decode(&buffer); assert_eq!( - decoded, - Err(HeaderDecodeError( - "Not enough data to decode bits per channel.".to_string() - )) + decoded + .unwrap_err() + .downcast::() + .unwrap(), + HeaderDecodeError("Not enough data to decode bits per channel.".to_string()) ); } @@ -101,8 +107,11 @@ mod tests { let decoded = super::decode(&buffer); assert_eq!( - decoded, - Err(HeaderDecodeError("Unknown mode in header.".to_string())) + decoded + .unwrap_err() + .downcast::() + .unwrap(), + HeaderDecodeError("Unknown mode in header.".to_string()) ); } } diff --git a/src/coder/encoder/alpha_encoder.rs b/src/coder/encoder/alpha_encoder.rs index 8081a33..c81936c 100644 --- a/src/coder/encoder/alpha_encoder.rs +++ b/src/coder/encoder/alpha_encoder.rs @@ -75,11 +75,13 @@ mod tests { let mut buffer = vec![0; min_required_buffer - 1]; let encoder = create_encoder(&mut buffer, data.to_vec(), file_name.to_string()); assert_eq!( - encoder.encode(), - Err(EncodeError( - "Too much data to encode in the image.".to_string() - )) - ) + encoder + .encode() + .unwrap_err() + .downcast::() + .unwrap(), + EncodeError("Too much data to encode in the image.".to_string()) + ); } #[test] @@ -93,11 +95,13 @@ mod tests { let mut buffer = vec![0; min_bytes_required - 1]; let encoder = create_encoder(&mut buffer, data.to_vec(), file_name.to_string()); assert_eq!( - encoder.encode(), - Err(EncodeError( - "Too much data to encode in the image.".to_string() - )) - ) + encoder + .encode() + .unwrap_err() + .downcast::() + .unwrap(), + EncodeError("Too much data to encode in the image.".to_string()) + ); } let mut buffer = vec![0; min_bytes_required]; diff --git a/src/coder/encoder/encode.rs b/src/coder/encoder/encode.rs index 968e209..f871165 100644 --- a/src/coder/encoder/encode.rs +++ b/src/coder/encoder/encode.rs @@ -1,7 +1,9 @@ +use anyhow::{anyhow, Result}; + use crate::coder::error::EncodeError; pub trait Encode { - fn encode(mut self: Box) -> Result<(), EncodeError> { + fn encode(mut self: Box) -> Result<()> { self.validate()?; let file_name = self.file_name_bytes(); @@ -15,11 +17,11 @@ pub trait Encode { Ok(()) } - fn validate(&self) -> Result<(), EncodeError> { + fn validate(&self) -> Result<()> { if self.bytes_to_encode() > self.max_bytes_to_encode() { - return Err(EncodeError( + return Err(anyhow!(EncodeError( "Too much data to encode in the image.".to_string(), - )); + ))); } Ok(()) } diff --git a/src/coder/encoder/header_encoder.rs b/src/coder/encoder/header_encoder.rs index b6c55dd..f5405d3 100644 --- a/src/coder/encoder/header_encoder.rs +++ b/src/coder/encoder/header_encoder.rs @@ -1,9 +1,11 @@ +use anyhow::Result; + use crate::coder::{ error::HeaderEncodeError, header::{AlgHeader, Header, RgbHeader}, }; -pub fn encode(header: Header, buffer: &mut [u8]) -> Result<(), HeaderEncodeError> { +pub fn encode(header: Header, buffer: &mut [u8]) -> Result<()> { let mut iter = buffer.iter_mut().skip(3).step_by(4); let mode_byte = iter.next().ok_or(HeaderEncodeError( "Not enough to encode header mode.".to_string(), @@ -18,14 +20,14 @@ pub fn encode(header: Header, buffer: &mut [u8]) -> Result<(), HeaderEncodeError Ok(()) } -fn encode_alpha<'a, I>(_iter: &mut I) -> Result<(), HeaderEncodeError> +fn encode_alpha<'a, I>(_iter: &mut I) -> Result<()> where I: Iterator, { Ok(()) } -fn encode_rgb<'a, I>(iter: &mut I, header: &RgbHeader) -> Result<(), HeaderEncodeError> +fn encode_rgb<'a, I>(iter: &mut I, header: &RgbHeader) -> Result<()> where I: Iterator, { @@ -38,7 +40,6 @@ where #[cfg(test)] mod tests { - use crate::coder::{ error::HeaderEncodeError, header::{Header, ALPHA_MODE, RGB_MODE}, @@ -48,7 +49,7 @@ mod tests { fn encode_alpha() { let header = Header::new_alpha(); let mut buffer = vec![0; 10]; - assert_eq!(Ok(()), super::encode(header, &mut buffer)); + assert!(super::encode(header, &mut buffer).is_ok()); assert_eq!(buffer, vec![0, 0, 0, ALPHA_MODE, 0, 0, 0, 0, 0, 0]); } @@ -57,7 +58,7 @@ mod tests { let bits_per_channel = 4; let header = Header::new_rgb(bits_per_channel); let mut buffer = vec![0; 10]; - assert_eq!(Ok(()), super::encode(header, &mut buffer)); + assert!(super::encode(header, &mut buffer).is_ok()); assert_eq!( buffer, vec![0, 0, 0, RGB_MODE, 0, 0, 0, bits_per_channel, 0, 0] @@ -69,11 +70,13 @@ mod tests { let header = Header::new_alpha(); let mut buffer = vec![0; 1]; let encoded = super::encode(header, &mut buffer); + assert_eq!( - encoded, - Err(HeaderEncodeError( - "Not enough to encode header mode.".to_string() - )) + encoded + .unwrap_err() + .downcast::() + .unwrap(), + HeaderEncodeError("Not enough to encode header mode.".to_string()) ); } @@ -83,11 +86,13 @@ mod tests { let header = Header::new_rgb(bits_per_channel); let mut buffer = vec![0; 4]; let encoded = super::encode(header, &mut buffer); + assert_eq!( - encoded, - Err(HeaderEncodeError( - "Not enough to encode header bits per channel.".to_string() - )) + encoded + .unwrap_err() + .downcast::() + .unwrap(), + HeaderEncodeError("Not enough to encode header bits per channel.".to_string()) ); } } diff --git a/src/coder/encoder/rgb_encoder.rs b/src/coder/encoder/rgb_encoder.rs index ae7d18b..d635e7f 100644 --- a/src/coder/encoder/rgb_encoder.rs +++ b/src/coder/encoder/rgb_encoder.rs @@ -82,11 +82,13 @@ mod tests { file_name.to_string(), ); assert_eq!( - encoder.encode(), - Err(EncodeError( - "Too much data to encode in the image.".to_string() - )) - ) + encoder + .encode() + .unwrap_err() + .downcast::() + .unwrap(), + EncodeError("Too much data to encode in the image.".to_string()) + ); } #[test] diff --git a/src/main.rs b/src/main.rs index 1f7be29..2fbcba6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use anyhow::Result; use image::{io::Reader as ImageReader, GenericImageView, RgbaImage}; use mythic_telegram::{ - coder, + coder::{decoder, encoder}, config::{self, Config, DecodeConfig, EncodeConfig, Mode}, file, }; @@ -14,7 +14,7 @@ fn encode(config: &EncodeConfig) -> Result<()> { let image = ImageReader::open(image_path)?.decode()?; let (image_width, image_height) = image.dimensions(); - let encoded_data = coder::encoder::encode( + let encoded_data = encoder::encode( &config.algorithm, image.to_rgba8().into_vec(), file::read_bytes(&config.secret_file)?, @@ -32,7 +32,7 @@ fn decode(config: &DecodeConfig) -> Result<()> { let image = ImageReader::open(image_path)?.decode()?; let image_data = image.to_rgba8().into_vec(); - let (file_name, decoded_data) = coder::decoder::decode(image_data)?; + let (file_name, decoded_data) = decoder::decode(image_data)?; let secret_file_path = image_path.with_file_name(file_name); file::write_bytes(&secret_file_path, &decoded_data) }