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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::fmt::{self, Debug};
use std::str::FromStr;
use num_traits::Float;
use crate::errors::Error;
pub trait CoordType: Float + Debug {}
impl<T: Float + Debug> CoordType for T {}
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Coord<T: CoordType = f64> {
pub x: T,
pub y: T,
pub z: Option<T>,
}
impl<T> Coord<T>
where
T: CoordType,
{
pub fn new(x: T, y: T, z: Option<T>) -> Self {
Coord { x, y, z }
}
}
impl<T> From<(T, T)> for Coord<T>
where
T: CoordType,
{
fn from(coord: (T, T)) -> Self {
Coord::new(coord.0, coord.1, None)
}
}
impl<T> From<[T; 2]> for Coord<T>
where
T: CoordType,
{
fn from(coord: [T; 2]) -> Self {
Coord::new(coord[0], coord[1], None)
}
}
impl<T> From<(T, T, Option<T>)> for Coord<T>
where
T: CoordType,
{
fn from(coord: (T, T, Option<T>)) -> Self {
Coord::new(coord.0, coord.1, coord.2)
}
}
impl<T> From<(T, T, T)> for Coord<T>
where
T: CoordType,
{
fn from(coord: (T, T, T)) -> Self {
Coord::new(coord.0, coord.1, Some(coord.2))
}
}
impl<T> From<[T; 3]> for Coord<T>
where
T: CoordType,
{
fn from(coord: [T; 3]) -> Self {
Coord::new(coord[0], coord[1], Some(coord[2]))
}
}
impl<T> FromStr for Coord<T>
where
T: CoordType + FromStr,
{
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.trim().split(',');
let x_str = parts.next().ok_or(Error::CoordEmpty)?;
let x: T = x_str
.parse()
.map_err(|_| Error::NumParse(x_str.to_string()))?;
let y_str = parts.next().ok_or(Error::CoordEmpty)?;
let y: T = y_str
.parse()
.map_err(|_| Error::NumParse(y_str.to_string()))?;
let z = if let Some(z) = parts.next() {
Some(z.parse::<T>().map_err(|_| Error::NumParse(z.to_string()))?)
} else {
None
};
Ok(Coord { x, y, z })
}
}
impl<T> fmt::Display for Coord<T>
where
T: fmt::Display + CoordType,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(z) = self.z {
write!(f, "{},{},{}", self.x, self.y, z)
} else {
write!(f, "{},{}", self.x, self.y)
}
}
}
pub fn coords_from_str<T: CoordType + FromStr>(s: &str) -> Result<Vec<Coord<T>>, Error> {
s.split_whitespace().map(Coord::from_str).collect()
}
#[cfg(test)]
mod tests {
use super::{coords_from_str, Coord};
use std::str::FromStr;
#[test]
fn test_coord_from_str() {
assert_eq!(
Coord::from_str(" 1.0,2.0,3 ").unwrap(),
Coord {
x: 1.,
y: 2.,
z: Some(3.)
}
);
assert_eq!(
Coord::from_str("1,1").unwrap(),
Coord {
x: 1.,
y: 1.,
z: None
}
);
}
#[test]
fn test_coords_from_str() {
assert_eq!(
coords_from_str("1,1\n\n 2,2 ").unwrap(),
vec![
Coord {
x: 1.,
y: 1.,
z: None
},
Coord {
x: 2.,
y: 2.,
z: None
}
]
)
}
}