eterm_parser/
lib.rs

1//! eterm parser
2//! ==============
3//!
4//! This library contains text parser of the eterm common
5//! command such as av,detr,fd,ml,pat,rt,etc.
6//! 
7//! impl zero allocation and zero cost with lifetime and &str.
8//!
9//! [Docs](https://docs.rs/eterm-parser/) |
10//! [Github](https://github.com/bmrxntfj/eterm-parser/) |
11//! [Crate](https://crates.io/crates/eterm-parser)
12//!
13//!
14//! Example: parse av text
15//! -------------------------------
16//!
17//! ```
18//! let text = r" 03AUG(THU) PKXSHA VIA KN  
19//! 1- *KN6856  DS# JA C8 YA BA HA KA LA RQ SQ TQ  PKXXIY 0900   1120   321 0^B  E  
20//! >   MU2104      GQ UQ ZQ                                            -- T3 02:20
21//!     MU2159  DS# J7 C5 D4 Q2 IQ YA BA MA EA HQ     SHA 1400   1620   32L 0^S  E  
22//! >               KA LA NQ RQ SQ VQ TQ GQ ZQ                          T3 T2 07:20
23//! 2  *KN6856  DS# JA C8 YA BA HA KA LA RQ SQ TQ  PKXXIY 0900   1120   321 0^B  E  
24//! >   MU2104      GQ UQ ZQ                                            -- T3 02:20
25//!    *MU3502  DS# YA BS MA ES KA LS NA RA SQ VQ     PVG 1500   1720   32S 0^S  E  
26//! >   HO1212                                                          T3 T2 08:20
27//! 3   KN5730  DS# WA YA BA MA EA HA KA LA NA R6  PKXWNZ 0915   1145   73U 0^   E  
28//! >               SQ VQ DQ TQ IQ ZQ U5 PQ GQ QS AQ                    -- T2 02:30
29//!     FM9530  DS# J7 C7 D7 Q6 I4 YA BA MA EA HA     PVG 1545   1650   73E 0^   E  
30//! >               KA LA NA RA SA VA TA GS ZA                          T2 T1 07:35
31//! 4+  KN5730  DS# WA YA BA MA EA HA KA LA NA R6  PKXWNZ 0915   1145   73U 0^   E  
32//! >               SQ VQ DQ TQ IQ ZQ U5 PQ GQ QS AQ                    -- T2 02:30
33//!    *MU8610  DS# J7 C7 D7 Q6 I4 YA BA MA EA HA     PVG 1545   1650   73E 0^   E  
34//! >   FM9530      KA LA NA RA SA VA TA GS ZA                          T2 T1 07:35";
35//! if let Ok(info) = eterm_parser::parse_av(text){
36//!     assert_eq!(info.dpt, Some("PKX"));
37//!     assert_eq!(info.arr, Some("SHA"));
38//!     assert_eq!(info.date, Some("03AUG"));
39//! } else {
40//!     assert_eq!(true, false);
41//! }
42//! ```
43//!
44//! Example: parse fd text
45//! -------------------------------
46//!
47//! ```
48//! let text=r"FD:KMGCTU/05SEP23/KY                   /CNY /TPM   744/                         
49//! 01 KY/J     / 5100.00=10200.00/J /C/  /   .   /25DEC19        /J000  PFN:01    
50//! 02 KY/G     / 1700.00= 3400.00/G /Y/  /   .   /25DEC19        /J000  PFN:02    
51//! 03 KY/Y     / 1700.00= 3400.00/Y /Y/  /   .   /25DEC19        /J000  PFN:03    
52//! 04 KY/B     / 1680.00= 3360.00/B /Y/  /   .   /25DEC19        /J000  PFN:04    
53//! 05 KY/M     / 1580.00= 3160.00/M /Y/  /   .   /25DEC19        /J000  PFN:05    
54//! 06 KY/M1    / 1500.00= 3000.00/M /Y/  /   .   /25DEC19        /J000  PFN:06    
55//! 07 KY/U     / 1410.00= 2820.00/U /Y/  /   .   /25DEC19        /J000  PFN:07    
56//! 08 KY/H     / 1330.00= 2660.00/H /Y/  /   .   /25DEC19        /J000  PFN:08    
57//! 09 KY/Q     / 1240.00= 2480.00/Q /Y/  /   .   /25DEC19        /J000  PFN:09    
58//! 10 KY/Q1    / 1160.00= 2320.00/Q /Y/  /   .   /25DEC19        /J000  PFN:10    
59//! 11 KY/V     / 1070.00= 2140.00/V /Y/  /   .   /25DEC19        /J000  PFN:11    
60//! 12 KY/V1    /  990.00= 1980.00/V /Y/  /   .   /25DEC19        /J000  PFN:12    
61//! 13 KY/W     /  900.00= 1800.00/W /Y/  /   .   /25DEC19        /J000  PFN:13    
62//! 14 KY/S     /  820.00= 1640.00/S /Y/  /   .   /25DEC19        /J000  PFN:14    
63//! 15 KY/E     /  730.00= 1460.00/E /Y/  /   .   /25DEC19        /J000  PFN:15    
64//!                                                                                 
65//! PAGE 1/1       /LPRIC/C52DZF3YARTGI11                                           ";
66//! if let Ok(info) = eterm_parser::parse_fd(text){
67//!     assert_eq!(info.org, Some("KMG"));
68//! } else {
69//!     assert_eq!(true, false);
70//! }
71//! ```
72//!
73//!
74
75mod util;
76/// The module include text parser and result type of response of av command.
77pub mod av;
78/// The module include text parser and result type of response of detr command.
79pub mod detr;
80/// The module include text parser and result type of response of fd command.
81pub mod fd;
82/// The module include text parser and result type of response of ml command.
83pub mod ml;
84/// The module include text parser and result type of response of pat command.
85pub mod pat;
86/// The module include text parser and result type of response of rt command.
87pub mod pnr;
88
89/// Parse av text that eterm server response.
90///
91/// # Examples
92///
93/// ```
94/// let text = r" 03AUG(THU) PKXSHA VIA KN  
95/// 1- *KN6856  DS# JA C8 YA BA HA KA LA RQ SQ TQ  PKXXIY 0900   1120   321 0^B  E  
96/// >   MU2104      GQ UQ ZQ                                            -- T3 02:20
97///     MU2159  DS# J7 C5 D4 Q2 IQ YA BA MA EA HQ     SHA 1400   1620   32L 0^S  E  
98/// >               KA LA NQ RQ SQ VQ TQ GQ ZQ                          T3 T2 07:20
99/// 2  *KN6856  DS# JA C8 YA BA HA KA LA RQ SQ TQ  PKXXIY 0900   1120   321 0^B  E  
100/// >   MU2104      GQ UQ ZQ                                            -- T3 02:20
101///    *MU3502  DS# YA BS MA ES KA LS NA RA SQ VQ     PVG 1500   1720   32S 0^S  E  
102/// >   HO1212                                                          T3 T2 08:20
103/// 3   KN5730  DS# WA YA BA MA EA HA KA LA NA R6  PKXWNZ 0915   1145   73U 0^   E  
104/// >               SQ VQ DQ TQ IQ ZQ U5 PQ GQ QS AQ                    -- T2 02:30
105///     FM9530  DS# J7 C7 D7 Q6 I4 YA BA MA EA HA     PVG 1545   1650   73E 0^   E  
106/// >               KA LA NA RA SA VA TA GS ZA                          T2 T1 07:35
107/// 4+  KN5730  DS# WA YA BA MA EA HA KA LA NA R6  PKXWNZ 0915   1145   73U 0^   E  
108/// >               SQ VQ DQ TQ IQ ZQ U5 PQ GQ QS AQ                    -- T2 02:30
109///    *MU8610  DS# J7 C7 D7 Q6 I4 YA BA MA EA HA     PVG 1545   1650   73E 0^   E  
110/// >   FM9530      KA LA NA RA SA VA TA GS ZA                          T2 T1 07:35";
111/// if let Ok(info) = eterm_parser::parse_av(text){
112///     assert_eq!(info.dpt, Some("PKX"));
113///     assert_eq!(info.arr, Some("SHA"));
114///     assert_eq!(info.date, Some("03AUG"));
115/// } else {
116///     panic!("av parse error");
117/// }
118/// ```
119pub fn parse_av(text: &str) -> anyhow::Result<av::Av> {
120    av::Av::parse(text)
121}
122
123/// Parse detr text that eterm server response.
124///
125/// # Examples
126///
127/// ```
128/// let text = r"ET PROCESSING IN PROGRESS   
129/// AATK:TN/9992303753785   
130/// ISSUED BY: AIR CHINA                 ORG/DST: HET/SIA                 ARL-D 
131/// TOUR CODE: ZCC4000LC
132/// PASSENGER: dwfei
133/// EXCH:                               CONJ TKT:   
134/// O FM:1HET CA    8113  S 21MAY 0815 OK S                        20K OPEN FOR USE 
135///      --T2 RL:NZJ0JY  /  
136///   TO: XIY   b
137/// FC: M  21MAY23HET CA XIY308.00CNY308.00END  
138/// FARE:           CNY  308.00|FOP:CC VI184
139/// TAX:               EXEMPTCN|OI: 
140/// TAX:            CNY 60.00YQ|                                                   +
141/// ";
142/// if let Ok(info) = eterm_parser::parse_detr(text){
143///     assert_eq!(info.org, Some("HET"));
144///     assert_eq!(info.dst, Some("SIA"));
145/// } else {
146///     panic!("detr parse error");
147/// }
148/// ```
149pub fn parse_detr(text: &str) -> anyhow::Result<detr::Detr> {
150    detr::Detr::parse(text)
151}
152
153/// Parse fd text that eterm server response.
154///
155/// # Examples
156///
157/// ```
158/// let text=r"FD:KMGCTU/05SEP23/KY                   /CNY /TPM   744/                         
159/// 01 KY/J     / 5100.00=10200.00/J /C/  /   .   /25DEC19        /J000  PFN:01    
160/// 02 KY/G     / 1700.00= 3400.00/G /Y/  /   .   /25DEC19        /J000  PFN:02    
161/// 03 KY/Y     / 1700.00= 3400.00/Y /Y/  /   .   /25DEC19        /J000  PFN:03    
162/// 04 KY/B     / 1680.00= 3360.00/B /Y/  /   .   /25DEC19        /J000  PFN:04    
163/// 05 KY/M     / 1580.00= 3160.00/M /Y/  /   .   /25DEC19        /J000  PFN:05    
164/// 06 KY/M1    / 1500.00= 3000.00/M /Y/  /   .   /25DEC19        /J000  PFN:06    
165/// 07 KY/U     / 1410.00= 2820.00/U /Y/  /   .   /25DEC19        /J000  PFN:07    
166/// 08 KY/H     / 1330.00= 2660.00/H /Y/  /   .   /25DEC19        /J000  PFN:08    
167/// 09 KY/Q     / 1240.00= 2480.00/Q /Y/  /   .   /25DEC19        /J000  PFN:09    
168/// 10 KY/Q1    / 1160.00= 2320.00/Q /Y/  /   .   /25DEC19        /J000  PFN:10    
169/// 11 KY/V     / 1070.00= 2140.00/V /Y/  /   .   /25DEC19        /J000  PFN:11    
170/// 12 KY/V1    /  990.00= 1980.00/V /Y/  /   .   /25DEC19        /J000  PFN:12    
171/// 13 KY/W     /  900.00= 1800.00/W /Y/  /   .   /25DEC19        /J000  PFN:13    
172/// 14 KY/S     /  820.00= 1640.00/S /Y/  /   .   /25DEC19        /J000  PFN:14    
173/// 15 KY/E     /  730.00= 1460.00/E /Y/  /   .   /25DEC19        /J000  PFN:15    
174///                                                                                 
175/// PAGE 1/1       /LPRIC/C52DZF3YARTGI11                                           ";
176/// if let Ok(info) = eterm_parser::parse_fd(text){
177///     assert_eq!(info.org, Some("KMG"));
178/// } else {
179///     panic!("fd parse error");
180/// }
181/// ```
182pub fn parse_fd(text: &str) -> anyhow::Result<fd::Fd> {
183    fd::Fd::parse(text)
184}
185
186/// Parse ml text that eterm server response.
187///
188/// # Examples
189///
190/// ```
191/// let text = r"MULTI                                                                           
192/// 8L9681 /08SEP          C                                                        
193/// KHGNGQ                                                                          
194/// NIL                                                                             
195/// URCKHG                                                                          
196///  001   0DILIAYIAILI      HP3M9L T HX1  VVV211 07SEP      K    T                 
197///  002   1MEIHEREYIABULAI+ KYAH8R T RR1  VVV211 07SEP      K O ST                 
198/// URCNGQ                                                                          
199/// NIL                                                                             
200/// TOTAL NUMBER    1";
201/// if let Ok(ml) = eterm_parser::parse_ml(text) {
202///     assert_eq!(ml.flight_no, Some("8L9681"));
203/// } else {
204///     panic!("ml parse error");
205/// }
206/// ```
207pub fn parse_ml(text: &str) -> anyhow::Result<ml::Ml> {
208    ml::Ml::parse(text)
209}
210
211/// Parse pat text that eterm server response.
212///
213/// # Examples
214///
215/// ```
216/// let text = r">PAT:A                                                                          
217/// 01 T FARE:CNY520.00 TAX:CNY50.00 YQ:CNY110.00  TOTAL:680.00                     
218/// SFC:01   SFN:01                                                               
219/// PAGE 1/1       /LPRIC/L3OF13GAATTP15";
220/// if let Ok(pat) = eterm_parser::parse_pat(text) {
221///     if let Some(items) = pat.items {
222///         for item in items {
223///             assert_eq!(
224///                 item.fare,
225///                 Some(eterm_parser::pat::PatPrice {
226///                     currency: Some("CNY"),
227///                     price: Some(520.0f32),
228///                     is_exemption: false
229///                 })
230///             );
231///         }
232///     }
233/// } else {
234///     panic!("pat parse error");
235/// }
236/// ```
237pub fn parse_pat(text: &str) -> anyhow::Result<pat::Pat> {
238    pat::Pat::parse(text)
239}
240
241/// Parse pnr text that eterm server response.
242///
243/// # Examples
244///
245/// ```
246/// let text = r"  **ELECTRONIC TICKET PNR**                                                     
247///  1.石风芸CHD KE9SWE                                                             
248///  2.  JD5324 Y   WE06SEP  DXJPKX RR1   1045 1310          E                      
249///  3.KMG/T KMG/T 037968926796/KUNMING WKN TANG TRADING CO. LTD./ZHANGSAN      
250///  4.T                                                                            
251///  5.SSR FOID JD HK1 NI433101202105250023/P1                                      
252///  6.SSR ADTK 1E BY KMG28AUG23/1742 OR CXL JD5324 Y06SEP                          
253///  7.SSR TKNE JD HK1 DXJPKX 5324 Y06SEP 8989198306578/1/P1                        
254///  8.SSR CHLD JD HK1 25MAY21/P1                                                   
255///  9.OSI JD CTCT13320512490                                                       
256/// 10.OSI JD CTCM15718791505/P1                                                    
257/// 11.OSI JD ADT/8989198306575    ";
258/// if let Ok(info) = eterm_parser::parse_pnr(text){
259///     assert_eq!(info.pnr_code, Some("KE9SWE"));
260/// } else {
261///     panic!("pnr parse error");
262/// }
263/// ```
264pub fn parse_pnr(text: &str) -> anyhow::Result<pnr::Pnr> {
265    pnr::Pnr::parse(text)
266}