From 3cbb97e5b309e1011415ac58ce7cca02ba4b403b Mon Sep 17 00:00:00 2001 From: famfo Date: Tue, 25 Jan 2022 12:37:15 +0100 Subject: [PATCH] Moved color type to different function --- src/main.rs | 20 +++++++++++++------- src/nummap.rs | 5 +++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6cd07ef..8aa98dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -145,6 +145,17 @@ fn unfuck_cif_number_fast(number: &str) -> Result { Ok(number) } +fn unfuck_color_type(color_type: &str) -> Result { + assert_trailing_leading(color_type)?; + let mut buf = ArrayString::<18>::new(); + let split = color_type.split_ascii_whitespace().take(8).collect::>(); + + join_to_arraystring(&split, &mut buf)?; + let color_type = *(nummap::COLOR_TYPE_NUMMAP.get(&buf).ok_or_else(|| InvalidSizeError(color_type.to_string()))?); + + Ok(color_type) +} + fn decoder(inputfile: &str, outputfile: &str) -> Result<()> { let infile = File::open(inputfile)?; let outfile = File::create(outputfile)?; @@ -189,18 +200,13 @@ fn decoder(inputfile: &str, outputfile: &str) -> Result<()> { .ok_or_else(|| InvalidSizeError(size_string.to_string()))? .trim_start(), )?; - - let format = match unfuck_cif_number_fast( + let format = unfuck_color_type( size_split[2] .trim_start() .strip_prefix("bitów_na_piksel:") .ok_or_else(|| InvalidSizeError(size_string.to_string()))? .trim_start(), - )? { - 24 => ColorType::Rgb, - 32 => ColorType::Rgba, - _ => return Err(InvalidSizeError(size_string.to_string())), - }; + )?; if width == 0 || height == 0 { return Err(NoDataError) diff --git a/src/nummap.rs b/src/nummap.rs index 0e51fde..1bbc58e 100644 --- a/src/nummap.rs +++ b/src/nummap.rs @@ -1261,3 +1261,8 @@ pub static U8_NUMMAP: Map<&'static str, u8> = phf_map! { "dwieście pięćdziesiąt cztery" => 254, "dwieście pięćdziesiąt pięć" => 255, }; + +pub static COLOR_TYPE_NUMMAP: Map<&'static str, png::ColorType> = phf_map! { + "dwadzieścia cztery" => png::ColorType::Rgb, + "trzydzieści dwa" => png::ColorType::Rgba, +};