Skip to content

Commit

Permalink
reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeow committed Dec 24, 2023
1 parent be4f18d commit bedc9eb
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions src/icon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dirs::{Dirs, ALL_DIRS, ORDINAL_DIRS};
use crate::{error, ztxt, RawDmi};
use crate::dirs::{Dirs, ALL_DIRS, CARDINAL_DIRS};
use crate::{error::DmiError, ztxt, RawDmi};
use image::codecs::png;
use image::GenericImageView;
use image::{imageops, DynamicImage};
Expand Down Expand Up @@ -45,12 +45,12 @@ pub fn dir_to_dmi_index(dir: &Dirs) -> Option<u32> {
}

impl Icon {
pub fn load<R: Read>(reader: R) -> Result<Icon, error::DmiError> {
pub fn load<R: Read>(reader: R) -> Result<Icon, DmiError> {
let raw_dmi = RawDmi::load(reader)?;
let chunk_ztxt = match &raw_dmi.chunk_ztxt {
Some(chunk) => chunk.clone(),
None => {
return Err(error::DmiError::Generic(
return Err(DmiError::Generic(
"Error loading icon: no zTXt chunk found.".to_string(),
))
}
Expand All @@ -61,7 +61,7 @@ impl Icon {

let current_line = decompressed_text.next();
if current_line != Some("# BEGIN DMI") {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: no DMI header found. Beginning: {:#?}",
current_line
)));
Expand All @@ -70,14 +70,14 @@ impl Icon {
let current_line = match decompressed_text.next() {
Some(thing) => thing,
None => {
return Err(error::DmiError::Generic(
return Err(DmiError::Generic(
"Error loading icon: no version header found.".to_string(),
))
}
};
let split_version: Vec<&str> = current_line.split_terminator(" = ").collect();
if split_version.len() != 2 || split_version[0] != "version" {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: improper version header found: {:#?}",
split_version
)));
Expand All @@ -87,14 +87,14 @@ impl Icon {
let current_line = match decompressed_text.next() {
Some(thing) => thing,
None => {
return Err(error::DmiError::Generic(
return Err(DmiError::Generic(
"Error loading icon: no width found.".to_string(),
))
}
};
let split_version: Vec<&str> = current_line.split_terminator(" = ").collect();
if split_version.len() != 2 || split_version[0] != "\twidth" {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: improper width found: {:#?}",
split_version
)));
Expand All @@ -104,22 +104,22 @@ impl Icon {
let current_line = match decompressed_text.next() {
Some(thing) => thing,
None => {
return Err(error::DmiError::Generic(
return Err(DmiError::Generic(
"Error loading icon: no height found.".to_string(),
))
}
};
let split_version: Vec<&str> = current_line.split_terminator(" = ").collect();
if split_version.len() != 2 || split_version[0] != "\theight" {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: improper height found: {:#?}",
split_version
)));
};
let height = split_version[1].parse::<u32>()?;

if width == 0 || height == 0 {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: invalid width ({}) / height ({}) values.",
width, height
)));
Expand All @@ -135,7 +135,7 @@ impl Icon {
let img_height = dimensions.1;

if img_width == 0 || img_height == 0 || img_width % width != 0 || img_height % height != 0 {
return Err(error::DmiError::Generic(format!("Error loading icon: invalid image width ({}) / height ({}) values. Missmatch with metadata width ({}) / height ({}).", img_width, img_height, width, height)));
return Err(DmiError::Generic(format!("Error loading icon: invalid image width ({}) / height ({}) values. Missmatch with metadata width ({}) / height ({}).", img_width, img_height, width, height)));
};

let width_in_states = img_width / width;
Expand All @@ -147,7 +147,7 @@ impl Icon {
let mut current_line = match decompressed_text.next() {
Some(thing) => thing,
None => {
return Err(error::DmiError::Generic(
return Err(DmiError::Generic(
"Error loading icon: no DMI trailer nor states found.".to_string(),
))
}
Expand All @@ -162,19 +162,19 @@ impl Icon {

let split_version: Vec<&str> = current_line.split_terminator(" = ").collect();
if split_version.len() != 2 || split_version[0] != "state" {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: improper state found: {:#?}",
split_version
)));
};

let name = split_version[1].as_bytes();
if !name.starts_with(&[b'\"']) || !name.ends_with(&[b'\"']) {
return Err(error::DmiError::Generic(format!("Error loading icon: invalid name icon_state found in metadata, should be preceded and succeeded by double-quotes (\"): {:#?}", name)));
return Err(DmiError::Generic(format!("Error loading icon: invalid name icon_state found in metadata, should be preceded and succeeded by double-quotes (\"): {:#?}", name)));
};
let name = match name.len() {
0 | 1 => {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: invalid name icon_state found in metadata, improper size: {:#?}",
name
)))
Expand All @@ -196,7 +196,7 @@ impl Icon {
current_line = match decompressed_text.next() {
Some(thing) => thing,
None => {
return Err(error::DmiError::Generic(
return Err(DmiError::Generic(
"Error loading icon: no DMI trailer found.".to_string(),
))
}
Expand All @@ -207,7 +207,7 @@ impl Icon {
};
let split_version: Vec<&str> = current_line.split_terminator(" = ").collect();
if split_version.len() != 2 {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: improper state found: {:#?}",
split_version
)));
Expand All @@ -231,7 +231,7 @@ impl Icon {
let text_coordinates: Vec<&str> = split_version[1].split_terminator(',').collect();
// Hotspot includes a mysterious 3rd parameter that always seems to be 1.
if text_coordinates.len() != 3 {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: improper hotspot found: {:#?}",
split_version
)));
Expand All @@ -258,7 +258,7 @@ impl Icon {
}

if dirs.is_none() || frames.is_none() {
return Err(error::DmiError::Generic(format!(
return Err(DmiError::Generic(format!(
"Error loading icon: state lacks essential settings. dirs: {:#?}. frames: {:#?}.",
dirs, frames
)));
Expand All @@ -267,7 +267,7 @@ impl Icon {
let frames = frames.unwrap();

if index + (dirs as u32 * frames) > max_possible_states {
return Err(error::DmiError::Generic(format!("Error loading icon: metadata settings exceeded the maximum number of states possible ({}).", max_possible_states)));
return Err(DmiError::Generic(format!("Error loading icon: metadata settings exceeded the maximum number of states possible ({}).", max_possible_states)));
};

let mut images = vec![];
Expand Down Expand Up @@ -304,7 +304,7 @@ impl Icon {
})
}

pub fn save<W: Write>(&self, mut writter: &mut W) -> Result<usize, error::DmiError> {
pub fn save<W: Write>(&self, mut writter: &mut W) -> Result<usize, DmiError> {
let mut sprites = vec![];
let mut signature = format!(
"# BEGIN DMI\nversion = {}\n\twidth = {}\n\theight = {}\n",
Expand All @@ -313,7 +313,7 @@ impl Icon {

for icon_state in &self.states {
if icon_state.images.len() as u32 != icon_state.dirs as u32 * icon_state.frames {
return Err(error::DmiError::Generic(format!("Error saving Icon: number of images ({}) differs from the stated metadata. Dirs: {}. Frames: {}. Name: \"{}\".", icon_state.images.len(), icon_state.dirs, icon_state.frames, icon_state.name)));
return Err(DmiError::Generic(format!("Error saving Icon: number of images ({}) differs from the stated metadata. Dirs: {}. Frames: {}. Name: \"{}\".", icon_state.images.len(), icon_state.dirs, icon_state.frames, icon_state.name)));
};

signature.push_str(&format!(
Expand All @@ -325,12 +325,12 @@ impl Icon {
match &icon_state.delay {
Some(delay) => {
if delay.len() as u32 != icon_state.frames {
return Err(error::DmiError::Generic(format!("Error saving Icon: number of frames ({}) differs from the delay entry ({:3?}). Name: \"{}\".", icon_state.frames, delay, icon_state.name)))
return Err(DmiError::Generic(format!("Error saving Icon: number of frames ({}) differs from the delay entry ({:3?}). Name: \"{}\".", icon_state.frames, delay, icon_state.name)))
};
let delay: Vec<String>= delay.iter().map(|&c| c.to_string()).collect();
signature.push_str(&format!("\tdelay = {}\n", delay.join(",")));
},
None => return Err(error::DmiError::Generic(format!("Error saving Icon: number of frames ({}) larger than one without a delay entry in icon state of name \"{}\".", icon_state.frames, icon_state.name)))
None => return Err(DmiError::Generic(format!("Error saving Icon: number of frames ({}) larger than one without a delay entry in icon state of name \"{}\".", icon_state.frames, icon_state.name)))
};
if let Looping::NTimes(flag) = icon_state.loop_flag {
signature.push_str(&format!("\tloop = {}\n", flag))
Expand Down Expand Up @@ -501,18 +501,18 @@ pub struct IconState {

impl IconState {
/// Gets a specific DynamicImage from `images`, given a dir and frame.
pub fn get_image(&self, dir: &Dirs, frame: u32) -> Result<&DynamicImage, error::DmiError> {
pub fn get_image(&self, dir: &Dirs, frame: u32) -> Result<&DynamicImage, DmiError> {
if self.frames < frame {
return Err(error::DmiError::IconState(format!(
return Err(DmiError::IconState(format!(
"Specified frame \"{}\" is larger than the number of frames ({}) in icon_state \"{}\"",
frame, self.frames, self.name
)));
}
if (self.dirs == 1 && *dir != Dirs::SOUTH)
|| (self.dirs == 4 && !ORDINAL_DIRS.contains(dir))
|| (self.dirs == 4 && !CARDINAL_DIRS.contains(dir))
|| (self.dirs == 8 && !ALL_DIRS.contains(dir))
{
return Err(error::DmiError::IconState(format!(
return Err(DmiError::IconState(format!(
"Dir specified {} is not in the set of valid dirs ({} dirs) in icon_state \"{}\"",
dir, self.dirs, self.name
)));
Expand All @@ -521,7 +521,7 @@ impl IconState {
let image_idx = match dir_to_dmi_index(dir) {
Some(idx) => (idx + 1) * frame - 1,
None => {
return Err(error::DmiError::IconState(format!(
return Err(DmiError::IconState(format!(
"Dir specified {} is not a valid dir within DMI ordering! (icon_state: {})",
dir, self.name
)));
Expand All @@ -533,7 +533,7 @@ impl IconState {
Ok(image)
},
None => {
Err(error::DmiError::IconState(format!(
Err(DmiError::IconState(format!(
"Out of bounds index {} in icon_state \"{}\" (images len: {} dirs: {}, frames: {} - dir: {}, frame: {})",
image_idx, self.name, self.images.len(), self.dirs, self.frames, dir, frame
)))
Expand Down

0 comments on commit bedc9eb

Please sign in to comment.