Skip to content

Commit

Permalink
🚧(main): refactor decode and get info result
Browse files Browse the repository at this point in the history
  • Loading branch information
W-Mai committed Mar 6, 2024
1 parent 27f6ba2 commit 9e15ac4
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,31 @@ use icu_lib::{endecoder, EncoderParams};
use std::fs;
use std::path::Path;

fn decode_with(data: Vec<u8>, input_format: ImageFormatCategory) -> MiData {
fn decode_with(
data: Vec<u8>,
input_format: ImageFormatCategory,
) -> Result<MiData, Box<dyn std::error::Error>> {
match input_format {
ImageFormatCategory::Auto => {
let ed = find_endecoder(&data);
ed.unwrap().decode(data)
Ok(ed.ok_or("No endecoder found")?.decode(data))
}
ImageFormatCategory::Common => MiData::decode_from(&common::AutoDectect {}, data),
ImageFormatCategory::LVGL_V9 => MiData::decode_from(&lvgl_v9::LVGL {}, data),
ImageFormatCategory::Common => Ok(MiData::decode_from(&common::AutoDectect {}, data)),
ImageFormatCategory::LVGL_V9 => Ok(MiData::decode_from(&lvgl_v9::LVGL {}, data)),
}
}

fn get_info_with(data: Vec<u8>, input_format: ImageFormatCategory) -> endecoder::ImageInfo {
fn get_info_with(
data: Vec<u8>,
input_format: ImageFormatCategory,
) -> Result<endecoder::ImageInfo, Box<dyn std::error::Error>> {
match input_format {
ImageFormatCategory::Auto => {
let ed = find_endecoder(&data);
ed.unwrap().info(&data)
Ok(ed.ok_or("No endecoder found")?.info(&data))
}
ImageFormatCategory::Common => common::AutoDectect {}.info(&data),
ImageFormatCategory::LVGL_V9 => lvgl_v9::LVGL {}.info(&data),
ImageFormatCategory::Common => Ok(common::AutoDectect {}.info(&data)),
ImageFormatCategory::LVGL_V9 => Ok(lvgl_v9::LVGL {}.info(&data)),
}
}

Expand Down Expand Up @@ -58,7 +64,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

let data = fs::read(file).expect("Unable to read file");
let info = get_info_with(data, *input_format);
let info = get_info_with(data, *input_format)?;

println!("{:#?}", info);
}
Expand All @@ -72,7 +78,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let data = fs::read(file).expect("Unable to read file");
let mid = decode_with(data, *input_format);

show_image(mid);
show_image(mid?);
}
SubCommands::Convert {
input_files,
Expand Down Expand Up @@ -165,7 +171,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

let data = fs::read(&file_path).expect("Unable to read file");
let mid = decode_with(data, *input_format);
let mid = decode_with(data, *input_format)?;

let ed = output_format.get_endecoder();
let params = EncoderParams::new()
Expand Down

0 comments on commit 9e15ac4

Please sign in to comment.