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 |input| {
53 let (input, spec_packing) = all_consuming(specs).parse(input)?;
54
55 Ok((input, spec_packing))
56 },
57 |input| {
59 let (input, spec_packing) = all_consuming(spec_packings).parse(input)?;
60
61 Ok((input, spec_packing))
62 },
63 |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 ))
79 .parse(input)?;
80
81 Ok((input, result))
82}
83
84pub fn solution(input: &str) -> IResult<&str, Solution> {
85 let (input, result) = alt((
86 |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 |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 |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 |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 |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 |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 |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 |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 "0.08g/4丸",
253 "0.40g/粒",
255 "100ml/瓶",
257 "10克/支",
259 "200ml/瓶/盒",
261 "24片/盒",
263 "360丸/瓶/盒",
265 "6袋",
267 "12片*3板*2盒",
269 "15丸*10袋",
271 "34mg",
273 "0.1*100片",
275 "0.24g*180粒/瓶/盒",
277 "0.24g*42粒/盒",
279 "0.25g*100片/瓶/盒",
281 "0.42g*12粒",
283 "18s*4板",
285 "3.6gx6丸",
287 "33mg*180丸/瓶/盒",
289 "6g*3袋",
291 "7.5mg×7片",
293 "90mg*10粒/板/袋/盒",
295 "0.125g*10粒*5板",
297 "0.1g*7片/板*1板/盒",
299 "0.1g*7粒/板*2板/盒",
301 "0.24g*10粒/板*4板/盒",
303 "0.24g*15片/板*6板/盒*2盒/中盒",
305 "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 "10g:50mg",
320 "75ml:37.5mg",
322 "0.15g:40mg*100片/瓶",
324 "0.5g:4mg*7袋/盒",
326 "0.5mg:10mg*20片/盒",
328 "10g:10mg*1支/盒",
330 "10ml:10mg*1瓶/盒",
332 "10ml:20mg:600μg*10支/盒",
334 "15ml:1.5g*15袋/盒",
336 "19.2mg:4.8mg*36片/盒",
338 "1g:50mg*12袋/盒",
340 "2%(60ml:1.2g)*2瓶/盒",
342 "2ml:0.5mg*10支/盒",
344 "5g:0.1g*1支/盒",
346 "5g:0.1g*1支/盒",
348 "5ml:10mg*1支/盒",
350 "5ml:15mg*1支/盒",
352 "5ml:15mg*1瓶/盒",
354 "5ml:25mg*1支/盒",
356 "62.5mg:375mg*4片/盒",
358 "75mg:0.1g:5mg*7粒/盒",
360 "8ml:8mg*1支/盒",
362 "(200mg:5mg)*12粒*2板/盒",
364 "0.3%*5ml",
366 "0.005%*15g*1支/盒",
368 "0.1%*10g*1支/盒",
370 "0.1%*10g*1支/盒",
372 "10g:0.2g*12g*1支/盒",
374 "10mg:0.5mg*20g*1支/盒",
376 "2.5%*10g*1支/盒",
378 "5ml:0.15g*5ml*1支/盒",
380 "5ml:15mg*5ml*1瓶/盒",
382 "10ml:10mg*10ml/瓶*1瓶/盒",
384 "(15mg:0.25mg)*50ml/管/盒",
386 "(75ml:37.5mg)*75ml/盒",
388 "100ml:66.7g*60ml/瓶",
390 "(1.5mg:1μg)120ml/瓶/盒",
392 "0.03%(10g:3mg)*10g/支",
394 "0.03%(10g:3mg)*10g/支",
396 "0.1%(10g:10mg)*10g/支/盒",
398 "0.1%(30g:30mg)*10g/支/盒",
400 "100ml:66.7g*60ml/瓶",
402 "5ml:24.4mg*5ml/支/盒",
404 "75ml:37.5mg/瓶*1瓶/盒",
406 "75ml:37.5mg/瓶*1瓶/盒",
408 "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 "5.5cm*5.5cm",
423 "(5.5cm*5.5cm)",
425 "5.5cm*5.5cm*5片*5袋",
427 "5.5cm*5.5cm*5袋",
429 "5.5cm*5.5cm*5只/包",
431 "5mm*5mm*5贴/袋",
433 "5mm*5mm*5片/盒",
435 "5cm*5cm/罐",
437 "5cm*5cm/贴*5贴*5袋",
439 "5mm*5mm/5片",
441 ];
442
443 for input in test_cases {
444 let output = patch(input);
445 assert!(output.is_ok());
446 }
447 }
448}