Skip to content

Commit

Permalink
Replace rest Results with anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
marmal95 committed Sep 14, 2024
1 parent 887c4fb commit 92ce0e4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/coder/decoder/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub trait Decode {
.decode_length()
.ok_or(self.not_available("filename length"))?;

let file_name = self
let file_name: Vec<u8> = self
.decode_data(file_name_length)
.ok_or(self.not_available("filename"))?;
let file_name = String::from_utf8(file_name)?;
Expand Down
37 changes: 23 additions & 14 deletions src/coder/decoder/header_decoder.rs
Original file line number Diff line number Diff line change
@@ -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<Header, HeaderDecodeError> {
pub fn decode(buffer: &[u8]) -> Result<Header> {
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(),
Expand All @@ -13,22 +15,24 @@ pub fn decode(buffer: &[u8]) -> Result<Header, HeaderDecodeError> {
Ok(Header::new(mode, alg_header))
}

fn decode_alg_header<'a, I>(mode: u8, iter: &mut I) -> Result<AlgHeader, HeaderDecodeError>
fn decode_alg_header<'a, I>(mode: u8, iter: &mut I) -> Result<AlgHeader>
where
I: Iterator<Item = &'a u8>,
{
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()
))),
}
}

fn decode_alpha() -> AlphaHeader {
AlphaHeader {}
}

fn decode_rgb<'a, I>(iter: &mut I) -> Result<RgbHeader, HeaderDecodeError>
fn decode_rgb<'a, I>(iter: &mut I) -> Result<RgbHeader>
where
I: Iterator<Item = &'a u8>,
{
Expand Down Expand Up @@ -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::<HeaderDecodeError>()
.unwrap(),
HeaderDecodeError("Not enough data to decode mode.".to_string())
);
}

Expand All @@ -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::<HeaderDecodeError>()
.unwrap(),
HeaderDecodeError("Not enough data to decode bits per channel.".to_string())
);
}

Expand All @@ -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::<HeaderDecodeError>()
.unwrap(),
HeaderDecodeError("Unknown mode in header.".to_string())
);
}
}
24 changes: 14 additions & 10 deletions src/coder/encoder/alpha_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<EncodeError>()
.unwrap(),
EncodeError("Too much data to encode in the image.".to_string())
);
}

#[test]
Expand All @@ -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::<EncodeError>()
.unwrap(),
EncodeError("Too much data to encode in the image.".to_string())
);
}

let mut buffer = vec![0; min_bytes_required];
Expand Down
10 changes: 6 additions & 4 deletions src/coder/encoder/encode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::{anyhow, Result};

use crate::coder::error::EncodeError;

pub trait Encode {
fn encode(mut self: Box<Self>) -> Result<(), EncodeError> {
fn encode(mut self: Box<Self>) -> Result<()> {
self.validate()?;

let file_name = self.file_name_bytes();
Expand All @@ -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(())
}
Expand Down
33 changes: 19 additions & 14 deletions src/coder/encoder/header_encoder.rs
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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<Item = &'a mut u8>,
{
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<Item = &'a mut u8>,
{
Expand All @@ -38,7 +40,6 @@ where

#[cfg(test)]
mod tests {

use crate::coder::{
error::HeaderEncodeError,
header::{Header, ALPHA_MODE, RGB_MODE},
Expand All @@ -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]);
}

Expand All @@ -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]
Expand All @@ -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::<HeaderEncodeError>()
.unwrap(),
HeaderEncodeError("Not enough to encode header mode.".to_string())
);
}

Expand All @@ -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::<HeaderEncodeError>()
.unwrap(),
HeaderEncodeError("Not enough to encode header bits per channel.".to_string())
);
}
}
12 changes: 7 additions & 5 deletions src/coder/encoder/rgb_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<EncodeError>()
.unwrap(),
EncodeError("Too much data to encode in the image.".to_string())
);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand All @@ -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)?,
Expand All @@ -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)
}
Expand Down

0 comments on commit 92ce0e4

Please sign in to comment.