1
1
use std:: fmt;
2
2
use std:: ops:: { Index , IndexMut } ;
3
3
4
+ use crate :: error:: ShapeError ;
5
+
4
6
#[ derive( Debug , Clone , PartialEq ) ]
5
7
pub struct Coordinate {
6
8
indices : Vec < usize > ,
7
9
}
8
10
9
11
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 } )
12
17
}
13
18
14
19
pub fn len ( & self ) -> usize {
@@ -73,13 +78,13 @@ mod tests {
73
78
74
79
#[ test]
75
80
fn test_len ( ) {
76
- let coord = coord ! [ 1 , 2 , 3 ] ;
81
+ let coord = coord ! [ 1 , 2 , 3 ] . unwrap ( ) ;
77
82
assert_eq ! ( coord. len( ) , 3 ) ;
78
83
}
79
84
80
85
#[ test]
81
86
fn test_iter ( ) {
82
- let coord = coord ! [ 1 , 2 , 3 ] ;
87
+ let coord = coord ! [ 1 , 2 , 3 ] . unwrap ( ) ;
83
88
let mut iter = coord. iter ( ) ;
84
89
assert_eq ! ( iter. next( ) , Some ( & 1 ) ) ;
85
90
assert_eq ! ( iter. next( ) , Some ( & 2 ) ) ;
@@ -89,38 +94,38 @@ mod tests {
89
94
90
95
#[ test]
91
96
fn test_insert ( ) {
92
- let coord = coord ! [ 1 , 2 , 3 ] ;
97
+ let coord = coord ! [ 1 , 2 , 3 ] . unwrap ( ) ;
93
98
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 ( ) ) ;
95
100
}
96
101
97
102
#[ test]
98
103
fn test_index ( ) {
99
- let coord = coord ! [ 1 , 2 , 3 ] ;
104
+ let coord = coord ! [ 1 , 2 , 3 ] . unwrap ( ) ;
100
105
assert_eq ! ( coord[ 0 ] , 1 ) ;
101
106
assert_eq ! ( coord[ 1 ] , 2 ) ;
102
107
assert_eq ! ( coord[ 2 ] , 3 ) ;
103
108
}
104
109
105
110
#[ test]
106
111
fn test_index_mut ( ) {
107
- let mut coord = coord ! [ 1 , 2 , 3 ] ;
112
+ let mut coord = coord ! [ 1 , 2 , 3 ] . unwrap ( ) ;
108
113
coord[ 1 ] = 4 ;
109
114
assert_eq ! ( coord[ 1 ] , 4 ) ;
110
115
}
111
116
112
117
#[ test]
113
118
fn test_display ( ) {
114
- let coord = coord ! [ 1 , 2 , 3 ] ;
119
+ let coord = coord ! [ 1 , 2 , 3 ] . unwrap ( ) ;
115
120
assert_eq ! ( format!( "{}" , coord) , "(1, 2, 3)" ) ;
116
121
}
117
122
118
123
#[ test]
119
124
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 ( ) ) ;
122
127
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 ( ) ) ;
125
130
}
126
131
}
0 commit comments