Skip to main content

saferx_parser/specification/
specifications.rs

1use crate::specification::{
2    common::{division_with_space, multiply_with_space},
3    packing::{many_packings, packing, spec, spec_packings, specs, SpecOrPacking},
4    patch::{self},
5    solution::{self},
6};
7use nom::{
8    branch::alt,
9    combinator::{all_consuming, opt},
10    sequence::{preceded, terminated},
11    IResult, Parser,
12};
13use serde::{Deserialize, Serialize};
14
15#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
16pub struct Plain {
17    pub specifications: Vec<SpecOrPacking>,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21pub struct Solution {
22    pub specifications: Vec<SpecOrPacking>,
23    pub solution: solution::Solution,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27pub struct Patch {
28    pub specifications: Vec<SpecOrPacking>,
29    pub patch: patch::Patch,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
33pub enum Specification {
34    Plain(Plain),
35    Solution(Solution),
36    Patch(Patch),
37}
38
39impl Specification {
40    pub fn specifications(&self) -> &[SpecOrPacking] {
41        match self {
42            Specification::Plain(plain) => &plain.specifications,
43            Specification::Solution(solution) => &solution.specifications,
44            Specification::Patch(patch) => &patch.specifications,
45        }
46    }
47}
48
49pub fn plain(input: &str) -> IResult<&str, Vec<SpecOrPacking>> {
50    let (input, result) = alt((
51        // s+
52        |input| {
53            let (input, spec_packing) = all_consuming(specs).parse(input)?;
54
55            Ok((input, spec_packing))
56        },
57        // sp
58        |input| {
59            let (input, spec_packing) = all_consuming(spec_packings).parse(input)?;
60
61            Ok((input, spec_packing))
62        },
63        // (s *)? p+
64        |input| {
65            let (input, (spec, packings)) =
66                all_consuming((opt(terminated(spec, multiply_with_space)), many_packings))
67                    .parse(input)?;
68
69            let mut specifications = vec![];
70            if let Some(spec) = spec {
71                specifications.push(SpecOrPacking::Spec(spec));
72            }
73            specifications.extend(packings);
74
75            Ok((input, specifications))
76        },
77        // S
78    ))
79    .parse(input)?;
80
81    Ok((input, result))
82}
83
84pub fn solution(input: &str) -> IResult<&str, Solution> {
85    let (input, result) = alt((
86        // S
87        |input| {
88            let (input, solution) = all_consuming(solution::solution).parse(input)?;
89
90            Ok((
91                input,
92                Solution {
93                    solution,
94                    specifications: vec![],
95                },
96            ))
97        },
98        // S * p+
99        |input| {
100            let (input, (solution, packings)) = all_consuming((
101                solution::solution,
102                preceded(opt(multiply_with_space), many_packings),
103            ))
104            .parse(input)?;
105
106            Ok((
107                input,
108                Solution {
109                    solution,
110                    specifications: packings,
111                },
112            ))
113        },
114        // S * s (* p)?
115        |input| {
116            let (input, (solution, spec, packings)) = all_consuming((
117                solution::solution,
118                preceded(opt(multiply_with_space), spec),
119                opt(preceded(multiply_with_space, many_packings)),
120            ))
121            .parse(input)?;
122
123            let mut specifications = vec![SpecOrPacking::Spec(spec)];
124            if let Some(packings) = packings {
125                specifications.extend(packings);
126            }
127
128            Ok((
129                input,
130                Solution {
131                    solution,
132                    specifications,
133                },
134            ))
135        },
136        // S * sp (* p)?
137        |input| {
138            let (input, (solution, spec_packings, packings)) = all_consuming((
139                solution::solution,
140                preceded(opt(multiply_with_space), spec_packings),
141                opt(preceded(multiply_with_space, many_packings)),
142            ))
143            .parse(input)?;
144
145            let mut specifications = spec_packings;
146            if let Some(packings) = packings {
147                specifications.extend(packings);
148            }
149
150            Ok((
151                input,
152                Solution {
153                    solution,
154                    specifications,
155                },
156            ))
157        },
158        // S / p (* p)?
159        |input| {
160            let (input, (solution, packing, packings)) = all_consuming((
161                solution::solution,
162                preceded(opt(division_with_space), packing),
163                opt(preceded(multiply_with_space, many_packings)),
164            ))
165            .parse(input)?;
166
167            let mut specifications = vec![SpecOrPacking::Packing(packing)];
168            if let Some(packings) = packings {
169                specifications.extend(packings);
170            }
171
172            Ok((
173                input,
174                Solution {
175                    solution,
176                    specifications,
177                },
178            ))
179        },
180    ))
181    .parse(input)?;
182
183    Ok((input, result))
184}
185
186pub fn patch(input: &str) -> IResult<&str, Patch> {
187    let (input, result) = alt((
188        // P
189        |input| {
190            let (input, patch) = all_consuming(patch::patch).parse(input)?;
191
192            Ok((
193                input,
194                Patch {
195                    patch,
196                    specifications: vec![],
197                },
198            ))
199        },
200        // P * p+
201        |input| {
202            let (input, (patch, packings)) = all_consuming((
203                patch::patch,
204                preceded(opt(multiply_with_space), many_packings),
205            ))
206            .parse(input)?;
207
208            Ok((
209                input,
210                Patch {
211                    patch,
212                    specifications: packings,
213                },
214            ))
215        },
216        // P / p (* p)?
217        |input| {
218            let (input, (patch, packing, packings)) = all_consuming((
219                patch::patch,
220                preceded(opt(division_with_space), packing),
221                opt(preceded(multiply_with_space, many_packings)),
222            ))
223            .parse(input)?;
224
225            let mut specifications = vec![SpecOrPacking::Packing(packing)];
226            if let Some(packings) = packings {
227                specifications.extend(packings);
228            }
229
230            Ok((
231                input,
232                Patch {
233                    patch,
234                    specifications,
235                },
236            ))
237        },
238    ))
239    .parse(input)?;
240
241    Ok((input, result))
242}
243
244#[cfg(test)]
245mod tests {
246    use super::*;
247
248    #[test]
249    fn test_specifications() {
250        let test_cases = vec![
251            // sp
252            "0.08g/4丸",
253            // sp
254            "0.40g/粒",
255            // sp
256            "100ml/瓶",
257            // sp
258            "10克/支",
259            // sp
260            "200ml/瓶/盒",
261            // p
262            "24片/盒",
263            // p
264            "360丸/瓶/盒",
265            // p
266            "6袋",
267            // p+
268            "12片*3板*2盒",
269            // p+
270            "15丸*10袋",
271            // s
272            "34mg",
273            // s * p
274            "0.1*100片",
275            // s * p
276            "0.24g*180粒/瓶/盒",
277            // s * p
278            "0.24g*42粒/盒",
279            // s * p
280            "0.25g*100片/瓶/盒",
281            // s * p
282            "0.42g*12粒",
283            // s * p
284            "18s*4板",
285            // s * p
286            "3.6gx6丸",
287            // s * p
288            "33mg*180丸/瓶/盒",
289            // s * p
290            "6g*3袋",
291            // s * p
292            "7.5mg×7片",
293            // s * p
294            "90mg*10粒/板/袋/盒",
295            // s * p+
296            "0.125g*10粒*5板",
297            // s * p+
298            "0.1g*7片/板*1板/盒",
299            // s * p+
300            "0.1g*7粒/板*2板/盒",
301            // s * p+
302            "0.24g*10粒/板*4板/盒",
303            // s * p+
304            "0.24g*15片/板*6板/盒*2盒/中盒",
305            // s * p+
306            "0.25*20粒*2板",
307        ];
308
309        for input in test_cases {
310            let output = plain(input);
311            assert!(output.is_ok());
312        }
313    }
314
315    #[test]
316    fn test_solution() {
317        let test_cases = vec![
318            // S
319            "10g:50mg",
320            // S
321            "75ml:37.5mg",
322            // S * p
323            "0.15g:40mg*100片/瓶",
324            // S * p
325            "0.5g:4mg*7袋/盒",
326            // S * p
327            "0.5mg:10mg*20片/盒",
328            // S * p
329            "10g:10mg*1支/盒",
330            // S * p
331            "10ml:10mg*1瓶/盒",
332            // S * p
333            "10ml:20mg:600μg*10支/盒",
334            // S * p
335            "15ml:1.5g*15袋/盒",
336            // S * p
337            "19.2mg:4.8mg*36片/盒",
338            // S * p
339            "1g:50mg*12袋/盒",
340            // S * p
341            "2%(60ml:1.2g)*2瓶/盒",
342            // S * p
343            "2ml:0.5mg*10支/盒",
344            // S * p
345            "5g:0.1g*1支/盒",
346            // S * p
347            "5g:0.1g*1支/盒",
348            // S * p
349            "5ml:10mg*1支/盒",
350            // S * p
351            "5ml:15mg*1支/盒",
352            // S * p
353            "5ml:15mg*1瓶/盒",
354            // S * p
355            "5ml:25mg*1支/盒",
356            // S * p
357            "62.5mg:375mg*4片/盒",
358            // S * p
359            "75mg:0.1g:5mg*7粒/盒",
360            // S * p
361            "8ml:8mg*1支/盒",
362            // S * p+
363            "(200mg:5mg)*12粒*2板/盒",
364            // S * s
365            "0.3%*5ml",
366            // S * s * p
367            "0.005%*15g*1支/盒",
368            // S * s * p
369            "0.1%*10g*1支/盒",
370            // S * s * p
371            "0.1%*10g*1支/盒",
372            // S * s * p
373            "10g:0.2g*12g*1支/盒",
374            // S * s * p
375            "10mg:0.5mg*20g*1支/盒",
376            // S * s * p
377            "2.5%*10g*1支/盒",
378            // S * s * p
379            "5ml:0.15g*5ml*1支/盒",
380            // S * s * p
381            "5ml:15mg*5ml*1瓶/盒",
382            // S * sp * p
383            "10ml:10mg*10ml/瓶*1瓶/盒",
384            // S * sp
385            "(15mg:0.25mg)*50ml/管/盒",
386            // S * sp
387            "(75ml:37.5mg)*75ml/盒",
388            // S * sp
389            "100ml:66.7g*60ml/瓶",
390            // S * sp
391            "(1.5mg:1μg)120ml/瓶/盒",
392            // S * sp
393            "0.03%(10g:3mg)*10g/支",
394            // S * sp
395            "0.03%(10g:3mg)*10g/支",
396            // S * sp
397            "0.1%(10g:10mg)*10g/支/盒",
398            // S * sp
399            "0.1%(30g:30mg)*10g/支/盒",
400            // S * sp
401            "100ml:66.7g*60ml/瓶",
402            // S * sp
403            "5ml:24.4mg*5ml/支/盒",
404            // S / p * p
405            "75ml:37.5mg/瓶*1瓶/盒",
406            // S / p * p
407            "75ml:37.5mg/瓶*1瓶/盒",
408            // S / p
409            "5ml:15mg/盒",
410        ];
411
412        for input in test_cases {
413            let output = solution(input);
414            assert!(output.is_ok());
415        }
416    }
417
418    #[test]
419    fn test_patch() {
420        let test_cases = vec![
421            // P
422            "5.5cm*5.5cm",
423            // P
424            "(5.5cm*5.5cm)",
425            // P * p+
426            "5.5cm*5.5cm*5片*5袋",
427            // P * p+
428            "5.5cm*5.5cm*5袋",
429            // P * p+
430            "5.5cm*5.5cm*5只/包",
431            // P * p+
432            "5mm*5mm*5贴/袋",
433            // P * p+
434            "5mm*5mm*5片/盒",
435            // P * p+
436            "5cm*5cm/罐",
437            // P / p (* p)?
438            "5cm*5cm/贴*5贴*5袋",
439            // P / p (* p)?
440            "5mm*5mm/5片",
441        ];
442
443        for input in test_cases {
444            let output = patch(input);
445            assert!(output.is_ok());
446        }
447    }
448}