papergrid/config/
entity.rs
1#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
3pub enum Entity {
4 Global,
6 Column(usize),
8 Row(usize),
10 Cell(usize, usize),
12}
13
14impl Entity {
15 pub fn iter(&self, count_rows: usize, count_cols: usize) -> EntityIterator {
17 EntityIterator {
18 entity: *self,
19 count_rows,
20 count_cols,
21 i: 0,
22 j: 0,
23 }
24 }
25}
26
27pub type Position = (usize, usize);
29
30impl From<Position> for Entity {
31 fn from((row, col): Position) -> Self {
32 Self::Cell(row, col)
33 }
34}
35
36#[derive(Debug)]
40pub struct EntityIterator {
41 entity: Entity,
42 count_rows: usize,
43 count_cols: usize,
44 i: usize,
45 j: usize,
46}
47
48impl Iterator for EntityIterator {
49 type Item = Position;
50
51 fn next(&mut self) -> Option<Self::Item> {
52 if self.count_rows == 0 || self.count_cols == 0 {
53 return None;
54 }
55
56 match self.entity {
57 Entity::Cell(row, col) => {
58 self.count_cols = 0;
59 self.count_rows = 0;
60
61 Some((row, col))
62 }
63 Entity::Column(col) => {
64 if self.i >= self.count_rows {
65 return None;
66 }
67
68 let i = self.i;
69 self.i += 1;
70
71 Some((i, col))
72 }
73 Entity::Row(row) => {
74 if self.j >= self.count_cols {
75 return None;
76 }
77
78 let j = self.j;
79 self.j += 1;
80
81 Some((row, j))
82 }
83 Entity::Global => {
84 if self.j >= self.count_cols {
85 self.j = 0;
86 self.i += 1;
87
88 if self.i >= self.count_rows {
89 return None;
90 }
91 }
92
93 let j = self.j;
94 self.j += 1;
95
96 Some((self.i, j))
97 }
98 }
99 }
100}