Skip to content

Commit

Permalink
Anyhow introduced / Decouple Image from Lib interface / Integration T…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
marmal95 committed Sep 14, 2024
1 parent a957705 commit 887c4fb
Show file tree
Hide file tree
Showing 14 changed files with 386 additions and 93 deletions.
144 changes: 142 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ edition = "2021"

[dependencies]
image = "0.25.1"
clap = { version = "4.0", features = ["derive"] }
clap = { version = "4.0", features = ["derive"] }
anyhow = "1.0.87"

assert_cmd = "2.0.16"
predicates = "3.1.2"
11 changes: 4 additions & 7 deletions src/coder/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ mod decode;
mod header_decoder;
mod rgb_decoder;

use std::error::Error;

use image::RgbaImage;
use anyhow::Result;

use self::{alpha_decoder::AlphaDecoder, decode::Decode, rgb_decoder::RgbDecoder};

use super::header::{AlgHeader, Header};

pub fn decode(image: RgbaImage) -> Result<(String, Vec<u8>), Box<dyn Error>> {
let mut buffer = image.into_raw();
let header = header_decoder::decode(&buffer)?;
let buffer = buffer.split_off(header.size() * 4);
pub fn decode(mut image_buffer: Vec<u8>) -> Result<(String, Vec<u8>)> {
let header = header_decoder::decode(&image_buffer)?;
let buffer = image_buffer.split_off(header.size() * 4);

let decoder = create_decoder(&header, &buffer);
let decoded = decoder.decode()?;
Expand Down
22 changes: 8 additions & 14 deletions src/coder/decoder/alpha_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ mod tests {
let decoder = create_decoder(&buffer);
let decoded = decoder.decode();
assert_eq!(
decoded,
Err(DecodeError(
"Not enough data to decode filename length".to_string()
))
decoded.unwrap_err().downcast::<DecodeError>().unwrap(),
DecodeError("Not enough data to decode filename length".to_string())
);
}

Expand All @@ -60,10 +58,8 @@ mod tests {
let decoder = create_decoder(&buffer);
let decoded = decoder.decode();
assert_eq!(
decoded,
Err(DecodeError(
"Not enough data to decode filename".to_string()
))
decoded.unwrap_err().downcast::<DecodeError>().unwrap(),
DecodeError("Not enough data to decode filename".to_string())
);
}

Expand All @@ -82,10 +78,8 @@ mod tests {
let decoder = create_decoder(&buffer);
let decoded = decoder.decode();
assert_eq!(
decoded,
Err(DecodeError(
"Not enough data to decode data length".to_string()
))
decoded.unwrap_err().downcast::<DecodeError>().unwrap(),
DecodeError("Not enough data to decode data length".to_string())
);
}

Expand Down Expand Up @@ -128,8 +122,8 @@ mod tests {
let decoder = create_decoder(&buffer);
let decoded = decoder.decode();
assert_eq!(
decoded,
Err(DecodeError("Not enough data to decode data".to_string()))
decoded.unwrap_err().downcast::<DecodeError>().unwrap(),
DecodeError("Not enough data to decode data".to_string())
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/coder/decoder/decode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Result;

use crate::coder::error::DecodeError;

pub trait Decode {
fn decode(mut self: Box<Self>) -> Result<(String, Vec<u8>), DecodeError> {
fn decode(mut self: Box<Self>) -> Result<(String, Vec<u8>)> {
let file_name_length = self
.decode_length()
.ok_or(self.not_available("filename length"))?;
Expand Down
22 changes: 8 additions & 14 deletions src/coder/decoder/rgb_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ mod tests {
let decoded = decoder.decode();

assert_eq!(
decoded,
Err(DecodeError(
"Not enough data to decode filename length".to_string()
))
decoded.unwrap_err().downcast::<DecodeError>().unwrap(),
DecodeError("Not enough data to decode filename length".to_string())
);
}

Expand All @@ -79,10 +77,8 @@ mod tests {
let decoder = create_decoder(&buffer, bits_per_channel);
let decoded = decoder.decode();
assert_eq!(
decoded,
Err(DecodeError(
"Not enough data to decode filename".to_string()
))
decoded.unwrap_err().downcast::<DecodeError>().unwrap(),
DecodeError("Not enough data to decode filename".to_string())
);
}

Expand All @@ -102,10 +98,8 @@ mod tests {
let decoder = create_decoder(&buffer, bits_per_channel);
let decoded = decoder.decode();
assert_eq!(
decoded,
Err(DecodeError(
"Not enough data to decode data length".to_string()
))
decoded.unwrap_err().downcast::<DecodeError>().unwrap(),
DecodeError("Not enough data to decode data length".to_string())
);
}

Expand Down Expand Up @@ -143,8 +137,8 @@ mod tests {
let decoder = create_decoder(&buffer, bits_per_channel);
let decoded = decoder.decode();
assert_eq!(
decoded,
Err(DecodeError("Not enough data to decode data".to_string()))
decoded.unwrap_err().downcast::<DecodeError>().unwrap(),
DecodeError("Not enough data to decode data".to_string())
);
}

Expand Down
Loading

0 comments on commit 887c4fb

Please sign in to comment.