1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::block::{Block, Dimension};
use crate::item::Item;

/// Represents an item that a user will insert into a bin.
#[derive(Clone, Debug)]
pub struct Bin {
    /// Represents the cuboid of this Bin.
    block: Block,
}

impl Bin {
    /// Creates a new Bin from it's dimensions.
    pub fn new(dims: [Dimension; 3]) -> Self {
        Self {
            block: Block::new(dims[0], dims[1], dims[2]),
        }
    }

    /// Returns whether or not the Bin's dimensions can emcompass or match the Bin.
    pub fn does_item_fit(&self, item: &Item<'_>) -> bool {
        self.block.does_it_fit(&item.block)
    }

    /// Returns the remaining bins after the item has been added to the current bin.
    /// Returns None if the item is too big to fit into the bin.
    pub fn best_fit(self, item: &Item<'_>) -> Option<Vec<Bin>> {
        self.block
            .best_fit(&item.block)
            .map(|blocks| blocks.into_iter().map(|block| Bin::from(block)).collect())
    }
}

impl From<Block> for Bin {
    fn from(block: Block) -> Self {
        Bin::new([block.dims[0], block.dims[1], block.dims[2]])
    }
}