1use std::fmt;
2
3use nom::{
4 branch::alt,
5 bytes::complete::{tag, tag_no_case},
6 character::complete::space0,
7 combinator::opt,
8 multi::many1,
9 number::complete::double,
10 IResult, Parser,
11};
12use serde::{Deserialize, Serialize};
13
14use crate::specification::common::{colon, format_value};
15
16#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
17pub struct SolutionUnitValue {
18 pub value: f64,
19 pub unit: String,
20}
21
22impl fmt::Display for SolutionUnitValue {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 write!(f, "{}{}", format_value(self.value), self.unit)
25 }
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
29pub struct Content {
30 pub solution: SolutionUnitValue,
31 pub solutes: Vec<SolutionUnitValue>,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
35pub struct Solution {
36 pub content: Option<Content>,
37 pub concentration: Option<String>,
38}
39
40pub fn solution_unit_value(input: &str) -> IResult<&str, SolutionUnitValue> {
41 let (input, (value, _, unit)) = (
42 double,
43 space0,
44 alt((
45 tag_no_case("μg"),
46 tag_no_case("ng"),
47 tag_no_case("mg"),
48 tag_no_case("ug"),
49 tag_no_case("g"),
50 tag_no_case("ml"),
51 tag_no_case("l"),
52 tag_no_case("IU"),
53 tag_no_case("U"),
54 )),
55 )
56 .parse(input)?;
57
58 Ok((
59 input,
60 SolutionUnitValue {
61 value,
62 unit: unit.to_string(),
63 },
64 ))
65}
66
67pub fn concentration(input: &str) -> IResult<&str, String> {
68 let (input, (value, _, _)) = (double, space0, alt((tag("%"), tag("%")))).parse(input)?;
69 let concentration = format!("{value}%");
70
71 Ok((input, concentration))
72}
73
74pub fn content_without_parentheses(input: &str) -> IResult<&str, Solution> {
75 let (input, (solution, solutes)) = (
76 solution_unit_value,
77 many1((space0, colon, space0, solution_unit_value)),
78 )
79 .parse(input)?;
80 let solutes = solutes
81 .into_iter()
82 .map(|(_, _, _, solute)| solute)
83 .collect();
84
85 Ok((
86 input,
87 Solution {
88 content: Some(Content { solution, solutes }),
89 concentration: None,
90 },
91 ))
92}
93
94pub fn content_with_parentheses(input: &str) -> IResult<&str, Solution> {
95 let (input, (_, _, solution, _, _)) = (
96 alt((tag("("), tag("("))),
97 space0,
98 content_without_parentheses,
99 space0,
100 alt((tag(")"), tag(")"))),
101 )
102 .parse(input)?;
103
104 Ok((input, solution))
105}
106
107pub fn content_maybe_with_parentheses(input: &str) -> IResult<&str, Solution> {
108 let (input, solution) =
109 alt((content_with_parentheses, content_without_parentheses)).parse(input)?;
110
111 Ok((input, solution))
112}
113
114pub fn solution(input: &str) -> IResult<&str, Solution> {
115 let (input, result) = alt((
116 |input| {
117 let (input, (concentration, _, solution)) =
118 (opt(concentration), space0, content_maybe_with_parentheses).parse(input)?;
119
120 Ok((
121 input,
122 Solution {
123 content: solution.content,
124 concentration,
125 },
126 ))
127 },
128 |input| {
129 let (input, (concentration, _, solution)) =
130 (concentration, space0, opt(content_maybe_with_parentheses)).parse(input)?;
131
132 Ok((
133 input,
134 Solution {
135 content: solution.and_then(|s| s.content),
136 concentration: Some(concentration),
137 },
138 ))
139 },
140 ))
141 .parse(input)?;
142
143 Ok((input, result))
144}
145
146#[cfg(test)]
147mod tests {
148 use super::*;
149
150 #[test]
151 fn test_solution_unit_value() {
152 let test_cases = vec![
153 (
154 "10.5mg",
155 SolutionUnitValue {
156 value: 10.5,
157 unit: "mg".to_string(),
158 },
159 ),
160 (
161 "0.001μg",
162 SolutionUnitValue {
163 value: 0.001,
164 unit: "μg".to_string(),
165 },
166 ),
167 (
168 "1000 IU",
169 SolutionUnitValue {
170 value: 1000.0,
171 unit: "IU".to_string(),
172 },
173 ),
174 (
175 "5.5 ml",
176 SolutionUnitValue {
177 value: 5.5,
178 unit: "ml".to_string(),
179 },
180 ),
181 ];
182
183 for (input, expected) in test_cases {
184 assert_eq!(solution_unit_value(input).unwrap().1, expected);
185 }
186
187 let invalid_inputs = ["invalid", "10.5 km"];
188 for input in invalid_inputs {
189 assert!(solution_unit_value(input).is_err());
190 }
191 }
192
193 #[test]
194 fn test_concentration() {
195 let result = concentration("10%").unwrap();
196 assert_eq!(result.0, "");
197 assert_eq!(result.1, "10%");
198
199 let result = concentration("0.5%").unwrap();
200 assert_eq!(result.0, "");
201 assert_eq!(result.1, "0.5%");
202
203 let result = concentration("5.5 %").unwrap();
204 assert_eq!(result.0, "");
205 assert_eq!(result.1, "5.5%");
206
207 assert!(concentration("invalid").is_err());
208 assert!(concentration("10").is_err());
209 assert!(concentration("%").is_err());
210 }
211
212 #[test]
213 fn test_content_without_parentheses() {
214 let test_cases = vec![
215 (
216 "10.5mg:5ml",
217 Solution {
218 content: Some(Content {
219 solution: SolutionUnitValue {
220 value: 10.5,
221 unit: "mg".to_string(),
222 },
223 solutes: vec![SolutionUnitValue {
224 value: 5.0,
225 unit: "ml".to_string(),
226 }],
227 }),
228 concentration: None,
229 },
230 ),
231 (
232 "1000IU:2ml",
233 Solution {
234 content: Some(Content {
235 solution: SolutionUnitValue {
236 value: 1000.0,
237 unit: "IU".to_string(),
238 },
239 solutes: vec![SolutionUnitValue {
240 value: 2.0,
241 unit: "ml".to_string(),
242 }],
243 }),
244 concentration: None,
245 },
246 ),
247 (
248 "10ml :2mg",
249 Solution {
250 content: Some(Content {
251 solution: SolutionUnitValue {
252 value: 10.0,
253 unit: "ml".to_string(),
254 },
255 solutes: vec![SolutionUnitValue {
256 value: 2.0,
257 unit: "mg".to_string(),
258 }],
259 }),
260 concentration: None,
261 },
262 ),
263 (
264 "0.5μg : 1ml",
265 Solution {
266 content: Some(Content {
267 solution: SolutionUnitValue {
268 value: 0.5,
269 unit: "μg".to_string(),
270 },
271 solutes: vec![SolutionUnitValue {
272 value: 1.0,
273 unit: "ml".to_string(),
274 }],
275 }),
276 concentration: None,
277 },
278 ),
279 (
280 "10ml : 20mg : 600μg",
281 Solution {
282 content: Some(Content {
283 solution: SolutionUnitValue {
284 value: 10.0,
285 unit: "ml".to_string(),
286 },
287 solutes: vec![
288 SolutionUnitValue {
289 value: 20.0,
290 unit: "mg".to_string(),
291 },
292 SolutionUnitValue {
293 value: 600.0,
294 unit: "μg".to_string(),
295 },
296 ],
297 }),
298 concentration: None,
299 },
300 ),
301 ];
302
303 for (input, expected) in test_cases {
304 assert_eq!(content_without_parentheses(input).unwrap().1, expected);
305 }
306
307 let invalid_inputs = ["invalid", "10.5mg", "10.5mg:", ":5ml"];
308 for input in invalid_inputs {
309 assert!(content_without_parentheses(input).is_err());
310 }
311 }
312
313 #[test]
314 fn test_content_with_parentheses() {
315 let test_cases = vec![
316 (
317 "(10.5mg:5ml)",
318 Solution {
319 content: Some(Content {
320 solution: SolutionUnitValue {
321 value: 10.5,
322 unit: "mg".to_string(),
323 },
324 solutes: vec![SolutionUnitValue {
325 value: 5.0,
326 unit: "ml".to_string(),
327 }],
328 }),
329 concentration: None,
330 },
331 ),
332 (
333 "(1000IU:2ml)",
334 Solution {
335 content: Some(Content {
336 solution: SolutionUnitValue {
337 value: 1000.0,
338 unit: "IU".to_string(),
339 },
340 solutes: vec![SolutionUnitValue {
341 value: 2.0,
342 unit: "ml".to_string(),
343 }],
344 }),
345 concentration: None,
346 },
347 ),
348 (
349 "(10ml :2mg)",
350 Solution {
351 content: Some(Content {
352 solution: SolutionUnitValue {
353 value: 10.0,
354 unit: "ml".to_string(),
355 },
356 solutes: vec![SolutionUnitValue {
357 value: 2.0,
358 unit: "mg".to_string(),
359 }],
360 }),
361 concentration: None,
362 },
363 ),
364 (
365 "(0.5μg : 1ml)",
366 Solution {
367 content: Some(Content {
368 solution: SolutionUnitValue {
369 value: 0.5,
370 unit: "μg".to_string(),
371 },
372 solutes: vec![SolutionUnitValue {
373 value: 1.0,
374 unit: "ml".to_string(),
375 }],
376 }),
377 concentration: None,
378 },
379 ),
380 (
381 "(10ml : 20mg : 600μg)",
382 Solution {
383 content: Some(Content {
384 solution: SolutionUnitValue {
385 value: 10.0,
386 unit: "ml".to_string(),
387 },
388 solutes: vec![
389 SolutionUnitValue {
390 value: 20.0,
391 unit: "mg".to_string(),
392 },
393 SolutionUnitValue {
394 value: 600.0,
395 unit: "μg".to_string(),
396 },
397 ],
398 }),
399 concentration: None,
400 },
401 ),
402 ];
403
404 for (input, expected) in test_cases {
405 assert_eq!(content_with_parentheses(input).unwrap().1, expected);
406 }
407
408 let invalid_inputs = ["(invalid)", "(10.5mg)", "(10.5mg:)", "(:5ml)", "10.5mg:5ml"];
409 for input in invalid_inputs {
410 assert!(content_with_parentheses(input).is_err());
411 }
412 }
413
414 #[test]
415 fn test_solution() {
416 let test_cases = vec![
417 (
418 "(10.5mg:5ml)",
419 Solution {
420 content: Some(Content {
421 solution: SolutionUnitValue {
422 value: 10.5,
423 unit: "mg".to_string(),
424 },
425 solutes: vec![SolutionUnitValue {
426 value: 5.0,
427 unit: "ml".to_string(),
428 }],
429 }),
430 concentration: None,
431 },
432 ),
433 (
434 "10%(10.5mg:5ml)",
435 Solution {
436 content: Some(Content {
437 solution: SolutionUnitValue {
438 value: 10.5,
439 unit: "mg".to_string(),
440 },
441 solutes: vec![SolutionUnitValue {
442 value: 5.0,
443 unit: "ml".to_string(),
444 }],
445 }),
446 concentration: Some("10%".to_string()),
447 },
448 ),
449 (
450 "0.5%(1000IU:2ml)",
451 Solution {
452 content: Some(Content {
453 solution: SolutionUnitValue {
454 value: 1000.0,
455 unit: "IU".to_string(),
456 },
457 solutes: vec![SolutionUnitValue {
458 value: 2.0,
459 unit: "ml".to_string(),
460 }],
461 }),
462 concentration: Some("0.5%".to_string()),
463 },
464 ),
465 (
466 "5.5%",
467 Solution {
468 content: None,
469 concentration: Some("5.5%".to_string()),
470 },
471 ),
472 ];
473
474 for (input, expected) in test_cases {
475 assert_eq!(solution(input).unwrap().1, expected);
476 }
477
478 let invalid_inputs = ["invalid", "10.5mg", "%", "(10.5mg)"];
479 for input in invalid_inputs {
480 assert!(solution(input).is_err());
481 }
482 }
483
484 #[test]
485 fn test_solution_unit_value_to_string() {
486 let unit_value = SolutionUnitValue {
487 value: 10.5,
488 unit: "ml".to_string(),
489 };
490 assert_eq!(&unit_value.to_string(), "10.5ml");
491
492 let unit_value = SolutionUnitValue {
493 value: 5.0,
494 unit: "mg".to_string(),
495 };
496 assert_eq!(&unit_value.to_string(), "5mg");
497 }
498}