1use std::{
2 collections::{HashSet, hash_set},
3 ops::{Add, RangeInclusive},
4};
5
6use azalea_registry::Block;
7
8use crate::{BlockState, block_state::BlockStateIntegerRepr};
9
10#[derive(Debug, Clone)]
11pub struct BlockStates {
12 pub set: HashSet<BlockState>,
13}
14
15impl From<RangeInclusive<BlockStateIntegerRepr>> for BlockStates {
16 fn from(range: RangeInclusive<BlockStateIntegerRepr>) -> Self {
17 let mut set = HashSet::with_capacity((range.end() - range.start() + 1) as usize);
18 for id in range {
19 set.insert(BlockState::try_from(id).unwrap_or_default());
20 }
21 Self { set }
22 }
23}
24
25impl IntoIterator for BlockStates {
26 type Item = BlockState;
27 type IntoIter = hash_set::IntoIter<BlockState>;
28
29 fn into_iter(self) -> Self::IntoIter {
30 self.set.into_iter()
31 }
32}
33
34impl BlockStates {
35 pub fn contains(&self, state: &BlockState) -> bool {
36 self.set.contains(state)
37 }
38}
39
40impl Add for BlockStates {
41 type Output = Self;
42
43 fn add(self, rhs: Self) -> Self::Output {
44 Self {
45 set: self.set.union(&rhs.set).copied().collect(),
46 }
47 }
48}
49
50impl From<HashSet<Block>> for BlockStates {
51 fn from(set: HashSet<Block>) -> Self {
52 Self::from(&set)
53 }
54}
55
56impl From<&HashSet<Block>> for BlockStates {
57 fn from(set: &HashSet<Block>) -> Self {
58 let mut block_states = HashSet::with_capacity(set.len());
59 for &block in set {
60 block_states.extend(BlockStates::from(block));
61 }
62 Self { set: block_states }
63 }
64}
65
66impl<const N: usize> From<[Block; N]> for BlockStates {
67 fn from(arr: [Block; N]) -> Self {
68 Self::from(&arr[..])
69 }
70}
71impl From<&[Block]> for BlockStates {
72 fn from(arr: &[Block]) -> Self {
73 let mut block_states = HashSet::with_capacity(arr.len());
74 for &block in arr {
75 block_states.extend(BlockStates::from(block));
76 }
77 Self { set: block_states }
78 }
79}