This repository has been archived by the owner on Apr 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput.rs
231 lines (203 loc) · 6.47 KB
/
input.rs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use geo::algorithm::relate::IntersectionMatrix;
use geo::{Geometry, Point};
use serde::{Deserialize, Deserializer};
use super::Result;
/// Example of the XML that these structures represent
///
/// ```xml
/// <run>
/// <precisionModel scale="1.0" offsetx="0.0" offsety="0.0"/>
///
/// <case>
/// <desc>AA disjoint</desc>
/// <a>
/// POLYGON(
/// (0 0, 80 0, 80 80, 0 80, 0 0))
/// </a>
/// <b>
/// POLYGON(
/// (100 200, 100 140, 180 140, 180 200, 100 200))
/// </b>
/// <test><op name="relate" arg3="FF2FF1212" arg1="A" arg2="B"> true </op>
/// </test>
/// <test> <op name="intersects" arg1="A" arg2="B"> false </op></test>
/// <test> <op name="contains" arg1="A" arg2="B"> false </op></test>
/// </case>
/// </run>
/// ```
#[derive(Debug, Deserialize)]
pub(crate) struct Run {
#[serde(rename = "case")]
pub cases: Vec<Case>,
}
#[derive(Debug, Deserialize)]
pub(crate) struct Case {
#[serde(default)]
pub(crate) desc: String,
#[serde(deserialize_with = "wkt::deserialize_geometry")]
pub(crate) a: Geometry<f64>,
#[serde(deserialize_with = "deserialize_opt_geometry", default)]
pub(crate) b: Option<Geometry<f64>>,
#[serde(rename = "test", default)]
pub(crate) tests: Vec<Test>,
}
#[derive(Debug, Deserialize)]
pub(crate) struct Test {
#[serde(rename = "op")]
pub(crate) operation_input: OperationInput,
}
#[derive(Debug, Deserialize)]
pub struct CentroidInput {
pub(crate) arg1: String,
#[serde(rename = "$value", deserialize_with = "wkt::deserialize_point")]
pub(crate) expected: Option<geo::Point<f64>>,
}
#[derive(Debug, Deserialize)]
pub struct ConvexHullInput {
pub(crate) arg1: String,
#[serde(rename = "$value", deserialize_with = "wkt::deserialize_geometry")]
pub(crate) expected: geo::Geometry<f64>,
}
#[derive(Debug, Deserialize)]
pub struct IntersectsInput {
pub(crate) arg1: String,
pub(crate) arg2: String,
#[serde(rename = "$value", deserialize_with = "deserialize_from_str")]
pub(crate) expected: bool,
}
#[derive(Debug, Deserialize)]
pub struct RelateInput {
pub(crate) arg1: String,
pub(crate) arg2: String,
#[serde(rename = "arg3", deserialize_with = "deserialize_from_str")]
pub(crate) expected: IntersectionMatrix,
}
#[derive(Debug, Deserialize)]
pub struct ContainsInput {
pub(crate) arg1: String,
pub(crate) arg2: String,
#[serde(rename = "$value", deserialize_with = "deserialize_from_str")]
pub(crate) expected: bool,
}
#[derive(Debug, Deserialize)]
#[serde(tag = "name")]
pub(crate) enum OperationInput {
#[serde(rename = "getCentroid")]
CentroidInput(CentroidInput),
#[serde(rename = "convexhull")]
ConvexHullInput(ConvexHullInput),
#[serde(rename = "intersects")]
IntersectsInput(IntersectsInput),
#[serde(rename = "relate")]
RelateInput(RelateInput),
#[serde(rename = "contains")]
ContainsInput(ContainsInput),
#[serde(other)]
Unsupported,
}
#[derive(Debug)]
pub(crate) enum Operation {
Centroid {
subject: Geometry<f64>,
expected: Option<Point<f64>>,
},
Contains {
subject: Geometry<f64>,
target: Geometry<f64>,
expected: bool,
},
ConvexHull {
subject: Geometry<f64>,
expected: Geometry<f64>,
},
Intersects {
subject: Geometry<f64>,
clip: Geometry<f64>,
expected: bool,
},
Relate {
a: Geometry<f64>,
b: Geometry<f64>,
expected: IntersectionMatrix,
},
}
impl OperationInput {
pub(crate) fn into_operation(self, case: &Case) -> Result<Operation> {
let geometry = &case.a;
match self {
Self::CentroidInput(centroid_input) => {
assert_eq!("A", centroid_input.arg1);
Ok(Operation::Centroid {
subject: geometry.clone(),
expected: centroid_input.expected,
})
}
Self::ConvexHullInput(convex_hull_input) => {
assert_eq!("A", convex_hull_input.arg1);
Ok(Operation::ConvexHull {
subject: geometry.clone(),
expected: convex_hull_input.expected,
})
}
Self::IntersectsInput(input) => {
assert_eq!("A", input.arg1);
assert_eq!("B", input.arg2);
assert!(
case.b.is_some(),
"intersects test case must contain geometry b"
);
Ok(Operation::Intersects {
subject: geometry.clone(),
clip: case.b.clone().expect("no geometry b in case"),
expected: input.expected,
})
}
Self::RelateInput(input) => {
assert_eq!("A", input.arg1);
assert_eq!("B", input.arg2);
assert!(
case.b.is_some(),
"intersects test case must contain geometry b"
);
Ok(Operation::Relate {
a: geometry.clone(),
b: case.b.clone().expect("no geometry b in case"),
expected: input.expected,
})
}
Self::ContainsInput(input) => {
assert_eq!("A", input.arg1);
assert_eq!("B", input.arg2);
assert!(
case.b.is_some(),
"intersects test case must contain geometry b"
);
Ok(Operation::Contains {
subject: geometry.clone(),
target: case.b.clone().expect("no geometry b in case"),
expected: input.expected,
})
}
Self::Unsupported => Err("This OperationInput not supported".into()),
}
}
}
pub fn deserialize_opt_geometry<'de, D>(
deserializer: D,
) -> std::result::Result<Option<Geometry<f64>>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Debug, Deserialize)]
struct Wrapper(#[serde(deserialize_with = "wkt::deserialize_geometry")] Geometry<f64>);
Option::<Wrapper>::deserialize(deserializer).map(|opt_wrapped| opt_wrapped.map(|w| w.0))
}
pub fn deserialize_from_str<'de, T, D>(deserializer: D) -> std::result::Result<T, D::Error>
where
T: std::str::FromStr,
D: Deserializer<'de>,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
String::deserialize(deserializer)
.and_then(|str| T::from_str(&str).map_err(serde::de::Error::custom))
}