Skip to content

Commit

Permalink
🚧(icu_lib): refactor find endecoder function
Browse files Browse the repository at this point in the history
  • Loading branch information
W-Mai committed Mar 6, 2024
1 parent ca8d950 commit 27f6ba2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
6 changes: 3 additions & 3 deletions icu_lib/src/endecoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait EnDecoder {
fn info(&self, data: &[u8]) -> ImageInfo;
}

pub fn get_info(data: &[u8]) -> ImageInfo {
pub fn find_endecoder(data: &[u8]) -> Option<&'static dyn EnDecoder> {
let eds = vec![
&endecoder::common::AutoDectect {} as &dyn EnDecoder,
&endecoder::lvgl_v9::LVGL {} as &dyn EnDecoder,
Expand All @@ -31,9 +31,9 @@ pub fn get_info(data: &[u8]) -> ImageInfo {
for ed in eds {
let can_decode = ed.can_decode(data);
if can_decode {
return ed.info(data);
return Some(ed);
}
}

panic!("No decoder found for this data")
None
}
16 changes: 0 additions & 16 deletions icu_lib/src/midata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,3 @@ impl MiData {
ed.encode(self, encoder_params)
}
}

pub fn decode_from(data: Vec<u8>) -> MiData {
let eds = vec![
&endecoder::common::AutoDectect {} as &dyn EnDecoder,
&endecoder::lvgl_v9::LVGL {} as &dyn EnDecoder,
];

for ed in eds {
let can_decode = ed.can_decode(&data);
if can_decode {
return ed.decode(data);
}
}

panic!("No decoder found for this data")
}
14 changes: 10 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ use crate::arguments::{
parse_args, ImageFormatCategory, ImageFormats, OutputFileFormatCategory, SubCommands,
};
use crate::image_shower::show_image;
use icu_lib::endecoder::{common, get_info, lvgl_v9, EnDecoder};
use icu_lib::midata::{decode_from, MiData};
use icu_lib::endecoder::{common, find_endecoder, lvgl_v9, EnDecoder};
use icu_lib::midata::MiData;
use icu_lib::{endecoder, EncoderParams};
use std::fs;
use std::path::Path;

fn decode_with(data: Vec<u8>, input_format: ImageFormatCategory) -> MiData {
match input_format {
ImageFormatCategory::Auto => decode_from(data),
ImageFormatCategory::Auto => {
let ed = find_endecoder(&data);
ed.unwrap().decode(data)
}
ImageFormatCategory::Common => MiData::decode_from(&common::AutoDectect {}, data),
ImageFormatCategory::LVGL_V9 => MiData::decode_from(&lvgl_v9::LVGL {}, data),
}
}

fn get_info_with(data: Vec<u8>, input_format: ImageFormatCategory) -> endecoder::ImageInfo {
match input_format {
ImageFormatCategory::Auto => get_info(&data),
ImageFormatCategory::Auto => {
let ed = find_endecoder(&data);
ed.unwrap().info(&data)
}
ImageFormatCategory::Common => common::AutoDectect {}.info(&data),
ImageFormatCategory::LVGL_V9 => lvgl_v9::LVGL {}.info(&data),
}
Expand Down

0 comments on commit 27f6ba2

Please sign in to comment.