Skip to content

Commit 9b9783b

Browse files
committed
feat!: prevent empty coord
1 parent c9459b8 commit 9b9783b

File tree

5 files changed

+183
-176
lines changed

5 files changed

+183
-176
lines changed

src/coordinate.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
use std::fmt;
22
use std::ops::{Index, IndexMut};
33

4+
use crate::error::ShapeError;
5+
46
#[derive(Debug, Clone, PartialEq)]
57
pub struct Coordinate {
68
indices: Vec<usize>,
79
}
810

911
impl Coordinate {
10-
pub fn new(indices: Vec<usize>) -> Self {
11-
Self { indices }
12+
pub fn new(indices: Vec<usize>) -> Result<Self, ShapeError> {
13+
if indices.is_empty() {
14+
return Err(ShapeError::new("Coordinate cannot be empty"));
15+
}
16+
Ok(Self { indices })
1217
}
1318

1419
pub fn len(&self) -> usize {
@@ -73,13 +78,13 @@ mod tests {
7378

7479
#[test]
7580
fn test_len() {
76-
let coord = coord![1, 2, 3];
81+
let coord = coord![1, 2, 3].unwrap();
7782
assert_eq!(coord.len(), 3);
7883
}
7984

8085
#[test]
8186
fn test_iter() {
82-
let coord = coord![1, 2, 3];
87+
let coord = coord![1, 2, 3].unwrap();
8388
let mut iter = coord.iter();
8489
assert_eq!(iter.next(), Some(&1));
8590
assert_eq!(iter.next(), Some(&2));
@@ -89,38 +94,38 @@ mod tests {
8994

9095
#[test]
9196
fn test_insert() {
92-
let coord = coord![1, 2, 3];
97+
let coord = coord![1, 2, 3].unwrap();
9398
let new_coord = coord.insert(1, 4);
94-
assert_eq!(new_coord, coord![1, 4, 2, 3]);
99+
assert_eq!(new_coord, coord![1, 4, 2, 3].unwrap());
95100
}
96101

97102
#[test]
98103
fn test_index() {
99-
let coord = coord![1, 2, 3];
104+
let coord = coord![1, 2, 3].unwrap();
100105
assert_eq!(coord[0], 1);
101106
assert_eq!(coord[1], 2);
102107
assert_eq!(coord[2], 3);
103108
}
104109

105110
#[test]
106111
fn test_index_mut() {
107-
let mut coord = coord![1, 2, 3];
112+
let mut coord = coord![1, 2, 3].unwrap();
108113
coord[1] = 4;
109114
assert_eq!(coord[1], 4);
110115
}
111116

112117
#[test]
113118
fn test_display() {
114-
let coord = coord![1, 2, 3];
119+
let coord = coord![1, 2, 3].unwrap();
115120
assert_eq!(format!("{}", coord), "(1, 2, 3)");
116121
}
117122

118123
#[test]
119124
fn test_coord_macro() {
120-
let coord = coord![1, 2, 3];
121-
assert_eq!(coord, Coordinate::new(vec![1, 2, 3]));
125+
let coord = coord![1, 2, 3].unwrap();
126+
assert_eq!(coord, Coordinate::new(vec![1, 2, 3]).unwrap());
122127

123-
let coord_repeated = coord![1; 3];
124-
assert_eq!(coord_repeated, Coordinate::new(vec![1, 1, 1]));
128+
let coord_repeated = coord![1; 3].unwrap();
129+
assert_eq!(coord_repeated, Coordinate::new(vec![1, 1, 1]).unwrap());
125130
}
126131
}

src/iter.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::coord;
22
use crate::coordinate::Coordinate;
33
use crate::shape::Shape;
4+
use std::cmp::max;
45

56
pub struct IndexIterator {
67
shape: Shape,
@@ -10,7 +11,8 @@ pub struct IndexIterator {
1011

1112
impl IndexIterator {
1213
pub fn new(shape: &Shape) -> Self {
13-
let current = coord![0; shape.order()];
14+
// (shape.order() == 0) => `next` returns None before `current` is used
15+
let current = coord![0; max(shape.order(), 1)].unwrap();
1416
IndexIterator {
1517
shape: shape.clone(),
1618
current,
@@ -55,12 +57,12 @@ mod tests {
5557
let shape = shape![2, 3].unwrap();
5658
let mut iter = IndexIterator::new(&shape);
5759

58-
assert_eq!(iter.next(), Some(coord![0, 0]));
59-
assert_eq!(iter.next(), Some(coord![0, 1]));
60-
assert_eq!(iter.next(), Some(coord![0, 2]));
61-
assert_eq!(iter.next(), Some(coord![1, 0]));
62-
assert_eq!(iter.next(), Some(coord![1, 1]));
63-
assert_eq!(iter.next(), Some(coord![1, 2]));
60+
assert_eq!(iter.next(), Some(coord![0, 0].unwrap()));
61+
assert_eq!(iter.next(), Some(coord![0, 1].unwrap()));
62+
assert_eq!(iter.next(), Some(coord![0, 2].unwrap()));
63+
assert_eq!(iter.next(), Some(coord![1, 0].unwrap()));
64+
assert_eq!(iter.next(), Some(coord![1, 1].unwrap()));
65+
assert_eq!(iter.next(), Some(coord![1, 2].unwrap()));
6466
assert_eq!(iter.next(), None);
6567
}
6668

@@ -69,10 +71,10 @@ mod tests {
6971
let shape = shape![4].unwrap();
7072
let mut iter = IndexIterator::new(&shape);
7173

72-
assert_eq!(iter.next(), Some(coord![0]));
73-
assert_eq!(iter.next(), Some(coord![1]));
74-
assert_eq!(iter.next(), Some(coord![2]));
75-
assert_eq!(iter.next(), Some(coord![3]));
74+
assert_eq!(iter.next(), Some(coord![0].unwrap()));
75+
assert_eq!(iter.next(), Some(coord![1].unwrap()));
76+
assert_eq!(iter.next(), Some(coord![2].unwrap()));
77+
assert_eq!(iter.next(), Some(coord![3].unwrap()));
7678
assert_eq!(iter.next(), None);
7779
}
7880

0 commit comments

Comments
 (0)