1use std::fmt;
2
3use nom::{
4 branch::alt,
5 bytes::complete::{tag, tag_no_case},
6 character::complete::space0,
7 number::complete::double,
8 sequence::preceded,
9 IResult, Parser,
10};
11use serde::{Deserialize, Serialize};
12
13use crate::specification::common::format_value;
14
15use super::common::multiply_with_space;
16
17#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
18pub struct PatchUnitValue {
19 pub value: f64,
20 pub unit: String,
21}
22
23impl fmt::Display for PatchUnitValue {
24 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25 write!(f, "{}{}", format_value(self.value), self.unit)
26 }
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
30pub struct Patch {
31 pub length: PatchUnitValue,
32 pub width: PatchUnitValue,
33}
34
35impl fmt::Display for Patch {
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 write!(f, "{}*{}", self.length, self.width)
38 }
39}
40
41pub fn patch_unit_value(input: &str) -> IResult<&str, PatchUnitValue> {
42 let (input, (value, _, unit)) =
43 (double, space0, alt((tag_no_case("mm"), tag_no_case("cm")))).parse(input)?;
44
45 Ok((
46 input,
47 PatchUnitValue {
48 value,
49 unit: unit.to_string(),
50 },
51 ))
52}
53
54pub fn patch_without_parentheses(input: &str) -> IResult<&str, Patch> {
55 let (input, (length, width)) = (
56 patch_unit_value,
57 preceded(multiply_with_space, patch_unit_value),
58 )
59 .parse(input)?;
60
61 Ok((input, Patch { length, width }))
62}
63
64pub fn area_with_parentheses(input: &str) -> IResult<&str, Patch> {
65 let (input, (_, _, patch, _, _)) = (
66 alt((tag("("), tag("("))),
67 space0,
68 patch_without_parentheses,
69 space0,
70 alt((tag(")"), tag(")"))),
71 )
72 .parse(input)?;
73
74 Ok((input, patch))
75}
76
77pub fn patch(input: &str) -> IResult<&str, Patch> {
78 let (input, patch) = alt((area_with_parentheses, patch_without_parentheses)).parse(input)?;
79
80 Ok((input, patch))
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_patch_unit_value() {
89 let test_cases = vec![
90 (
91 "10.5cm",
92 PatchUnitValue {
93 value: 10.5,
94 unit: "cm".to_string(),
95 },
96 ),
97 (
98 "5.5 mm",
99 PatchUnitValue {
100 value: 5.5,
101 unit: "mm".to_string(),
102 },
103 ),
104 ];
105
106 for (input, expected) in test_cases {
107 assert_eq!(patch_unit_value(input).unwrap().1, expected);
108 }
109
110 let invalid_inputs = ["invalid", "10.5 km"];
111 for input in invalid_inputs {
112 assert!(patch_unit_value(input).is_err());
113 }
114 }
115
116 #[test]
117 fn test_patch_without_parentheses() {
118 let test_cases = vec![
119 (
120 "10mm*5mm",
121 Patch {
122 length: PatchUnitValue {
123 value: 10.0,
124 unit: "mm".to_string(),
125 },
126 width: PatchUnitValue {
127 value: 5.0,
128 unit: "mm".to_string(),
129 },
130 },
131 ),
132 (
133 "1cm x 5cm",
134 Patch {
135 length: PatchUnitValue {
136 value: 1.0,
137 unit: "cm".to_string(),
138 },
139 width: PatchUnitValue {
140 value: 5.0,
141 unit: "cm".to_string(),
142 },
143 },
144 ),
145 (
146 "1cm × 5mm",
147 Patch {
148 length: PatchUnitValue {
149 value: 1.0,
150 unit: "cm".to_string(),
151 },
152 width: PatchUnitValue {
153 value: 5.0,
154 unit: "mm".to_string(),
155 },
156 },
157 ),
158 ];
159
160 for (input, expected) in test_cases {
161 assert_eq!(patch_without_parentheses(input).unwrap().1, expected);
162 }
163
164 let invalid_inputs = ["invalid", "10.5cm", "10.5mm:", "*5mm"];
165 for input in invalid_inputs {
166 assert!(patch_without_parentheses(input).is_err());
167 }
168 }
169
170 #[test]
171 fn test_patch_with_parentheses() {
172 let test_cases = vec![
173 (
174 "(10mm*5mm)",
175 Patch {
176 length: PatchUnitValue {
177 value: 10.0,
178 unit: "mm".to_string(),
179 },
180 width: PatchUnitValue {
181 value: 5.0,
182 unit: "mm".to_string(),
183 },
184 },
185 ),
186 (
187 "( 1cm x 5cm )",
188 Patch {
189 length: PatchUnitValue {
190 value: 1.0,
191 unit: "cm".to_string(),
192 },
193 width: PatchUnitValue {
194 value: 5.0,
195 unit: "cm".to_string(),
196 },
197 },
198 ),
199 (
200 "(1cm × 5mm )",
201 Patch {
202 length: PatchUnitValue {
203 value: 1.0,
204 unit: "cm".to_string(),
205 },
206 width: PatchUnitValue {
207 value: 5.0,
208 unit: "mm".to_string(),
209 },
210 },
211 ),
212 ];
213
214 for (input, expected) in test_cases {
215 assert_eq!(area_with_parentheses(input).unwrap().1, expected);
216 }
217
218 let invalid_inputs = ["(invalid)", "(10.5mm)", "(10.5mm:)", "(:5cm)", "10.5mmx5mm"];
219 for input in invalid_inputs {
220 assert!(area_with_parentheses(input).is_err());
221 }
222 }
223
224 #[test]
225 fn test_patch() {
226 let test_cases = vec![
227 (
228 "10mm*5mm",
229 Patch {
230 length: PatchUnitValue {
231 value: 10.0,
232 unit: "mm".to_string(),
233 },
234 width: PatchUnitValue {
235 value: 5.0,
236 unit: "mm".to_string(),
237 },
238 },
239 ),
240 (
241 "1cm x 5cm",
242 Patch {
243 length: PatchUnitValue {
244 value: 1.0,
245 unit: "cm".to_string(),
246 },
247 width: PatchUnitValue {
248 value: 5.0,
249 unit: "cm".to_string(),
250 },
251 },
252 ),
253 (
254 "1cm × 5mm",
255 Patch {
256 length: PatchUnitValue {
257 value: 1.0,
258 unit: "cm".to_string(),
259 },
260 width: PatchUnitValue {
261 value: 5.0,
262 unit: "mm".to_string(),
263 },
264 },
265 ),
266 (
267 "(10mm*5mm)",
268 Patch {
269 length: PatchUnitValue {
270 value: 10.0,
271 unit: "mm".to_string(),
272 },
273 width: PatchUnitValue {
274 value: 5.0,
275 unit: "mm".to_string(),
276 },
277 },
278 ),
279 (
280 "( 1cm x 5cm )",
281 Patch {
282 length: PatchUnitValue {
283 value: 1.0,
284 unit: "cm".to_string(),
285 },
286 width: PatchUnitValue {
287 value: 5.0,
288 unit: "cm".to_string(),
289 },
290 },
291 ),
292 (
293 "(1cm × 5mm )",
294 Patch {
295 length: PatchUnitValue {
296 value: 1.0,
297 unit: "cm".to_string(),
298 },
299 width: PatchUnitValue {
300 value: 5.0,
301 unit: "mm".to_string(),
302 },
303 },
304 ),
305 ];
306
307 for (input, expected) in test_cases {
308 assert_eq!(patch(input).unwrap().1, expected);
309 }
310
311 let invalid_inputs = ["invalid", "10.5mm", "(10.5mmx1)"];
312 for input in invalid_inputs {
313 assert!(patch(input).is_err());
314 }
315 }
316}