Skip to content

Commit

Permalink
Impl display for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn committed Mar 8, 2020
1 parent 149da54 commit d72db93
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ authors = ["Chinedu Francis Nwafili <[email protected]>"]
edition = "2018"
keywords = ["texture", "atlas", "bin", "rect", "box", "packer"]

[dependencies]
thiserror = "1"
27 changes: 21 additions & 6 deletions src/bin_section.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{HeuristicFn, LayeredRect, PackedLocation, RotatedBy, WidthHeightDepth};
use std::cmp::Ordering;
use std::hint::unreachable_unchecked;
use std::fmt::{Display, Error, Formatter};

/// Given two sets of containers, which of these is the more suitable for our packing.
///
Expand Down Expand Up @@ -42,16 +42,32 @@ impl BinSection {
}

/// An error while attempting to place a rectangle within a bin section;
#[derive(Debug, thiserror::Error, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
#[allow(missing_docs)]
pub enum BinSectionError {
#[error("Can not place a rectangle inside of a bin that is wider than that rectangle.")]
PlacementWiderThanBinSection,
#[error("Can not place a rectangle inside of a bin that is taller than that rectangle.")]
PlacementTallerThanBinSection,
#[error("Can not place a rectangle inside of a bin that is deeper than that rectangle.")]
PlacementDeeperThanBinSection,
}

impl Display for BinSectionError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
let err = match self {
BinSectionError::PlacementWiderThanBinSection => {
"Can not place a rectangle inside of a bin that is wider than that rectangle."
}
BinSectionError::PlacementTallerThanBinSection => {
"Can not place a rectangle inside of a bin that is taller than that rectangle."
}
BinSectionError::PlacementDeeperThanBinSection => {
"Can not place a rectangle inside of a bin that is deeper than that rectangle."
}
};

f.write_str(err)
}
}

impl BinSection {
/// Create a new BinSection
pub fn new(x: u32, y: u32, z: u32, whd: WidthHeightDepth) -> Self {
Expand Down Expand Up @@ -400,7 +416,6 @@ impl BinSection {
mod tests {
use super::*;
use crate::{volume_heuristic, LayeredRect};
use std::sync::mpsc::TrySendError::Full;

const BIGGEST: u32 = 50;
const MIDDLE: u32 = 25;
Expand Down
28 changes: 19 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! `rectangle-pack` is a library focused on laying out any number of smaller rectangles
//! (both 2d rectangles and 3d rectangular prisms) inside any number of larger rectangles.
#![deny(missing_docs)]

use crate::bin_section::{BinSection, MoreSuitableContainersFn};
use crate::layered_rect_groups::{Group, LayeredRectGroups};
use std::collections::hash_map::Entry;

use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::{Debug, Display, Error, Formatter};
use std::hash::Hash;
use std::iter::Once;
use std::ops::Range;

pub use crate::bin_section::contains_smallest_box;

Expand Down Expand Up @@ -39,12 +40,12 @@ fn pack_rects<
GroupId: Debug + Hash + PartialEq + Eq + Clone,
>(
incoming_groups: &LayeredRectGroups<InboundId, GroupId>,
mut target_bins: HashMap<BinId, TargetBin>,
target_bins: HashMap<BinId, TargetBin>,
box_size_heuristic: &HeuristicFn,
more_suitable_containers_fn: &MoreSuitableContainersFn,
) -> Result<RectanglePackOk<InboundId, BinId>, RectanglePackError> {
let mut packed_locations = HashMap::new();
let mut bin_stats = HashMap::new();
let bin_stats = HashMap::new();

let mut target_bins: Vec<(BinId, TargetBin)> = target_bins.into_iter().collect();
target_bins.sort_unstable_by(|a, b| {
Expand Down Expand Up @@ -93,7 +94,7 @@ fn pack_rects<
});

// FIXME: Split into individual functions for readability ... Too nested
'group: for (group_id, incomings) in group_id_to_inbound_ids {
'group: for (_group_id, incomings) in group_id_to_inbound_ids {
'incoming: for incoming_id in incomings.iter() {
'bin: for (bin_id, bin) in target_bins.iter_mut() {
let mut bin_clone = bin.clone();
Expand Down Expand Up @@ -234,13 +235,22 @@ impl LayeredRect {
}

/// An error while attempting to pack rectangles into bins.
#[derive(Debug, thiserror::Error, PartialEq)]
#[derive(Debug, PartialEq)]
pub enum RectanglePackError {
/// The rectangles can't be placed into the bins. More bin space needs to be provided.
#[error(r#"Not enough space to place all of the rectangles."#)]
NotEnoughBinSpace,
}

impl Display for RectanglePackError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
RectanglePackError::NotEnoughBinSpace => {
f.write_str("Not enough space to place all of the rectangles.")
}
}
}
}

#[derive(Debug, Clone)]
struct TargetBin {
max_width: u32,
Expand Down

0 comments on commit d72db93

Please sign in to comment.