1 #[cfg(feature = "verbose-errors")]
2 #[cfg(feature = "std")]
3 use internal::{Err, IResult};
4 #[cfg(feature = "verbose-errors")]
5 use verbose_errors::Context;
6
7 #[cfg(feature = "std")]
8 use std::collections::HashMap;
9
10 #[cfg(feature = "alloc")]
11 use lib::std::string::ToString;
12 #[cfg(feature = "alloc")]
13 use lib::std::vec::Vec;
14
15 #[cfg(feature = "std")]
16 pub trait HexDisplay {
17 /// Converts the value of `self` to a hex dump, returning the owned
18 /// string.
to_hex(&self, chunk_size: usize) -> String19 fn to_hex(&self, chunk_size: usize) -> String;
20
21 /// Converts the value of `self` to a hex dump beginning at `from` address, returning the owned
22 /// string.
to_hex_from(&self, chunk_size: usize, from: usize) -> String23 fn to_hex_from(&self, chunk_size: usize, from: usize) -> String;
24 }
25
26 #[cfg(feature = "std")]
27 static CHARS: &'static [u8] = b"0123456789abcdef";
28
29 #[cfg(feature = "std")]
30 impl HexDisplay for [u8] {
31 #[allow(unused_variables)]
to_hex(&self, chunk_size: usize) -> String32 fn to_hex(&self, chunk_size: usize) -> String {
33 self.to_hex_from(chunk_size, 0)
34 }
35
36 #[allow(unused_variables)]
to_hex_from(&self, chunk_size: usize, from: usize) -> String37 fn to_hex_from(&self, chunk_size: usize, from: usize) -> String {
38 let mut v = Vec::with_capacity(self.len() * 3);
39 let mut i = from;
40 for chunk in self.chunks(chunk_size) {
41 let s = format!("{:08x}", i);
42 for &ch in s.as_bytes().iter() {
43 v.push(ch);
44 }
45 v.push(b'\t');
46
47 i += chunk_size;
48
49 for &byte in chunk {
50 v.push(CHARS[(byte >> 4) as usize]);
51 v.push(CHARS[(byte & 0xf) as usize]);
52 v.push(b' ');
53 }
54 if chunk_size > chunk.len() {
55 for j in 0..(chunk_size - chunk.len()) {
56 v.push(b' ');
57 v.push(b' ');
58 v.push(b' ');
59 }
60 }
61 v.push(b'\t');
62
63 for &byte in chunk {
64 if (byte >= 32 && byte <= 126) || byte >= 128 {
65 v.push(byte);
66 } else {
67 v.push(b'.');
68 }
69 }
70 v.push(b'\n');
71 }
72
73 String::from_utf8_lossy(&v[..]).into_owned()
74 }
75 }
76
77 #[cfg(feature = "std")]
78 impl HexDisplay for str {
79 #[allow(unused_variables)]
to_hex(&self, chunk_size: usize) -> String80 fn to_hex(&self, chunk_size: usize) -> String {
81 self.to_hex_from(chunk_size, 0)
82 }
83
84 #[allow(unused_variables)]
to_hex_from(&self, chunk_size: usize, from: usize) -> String85 fn to_hex_from(&self, chunk_size: usize, from: usize) -> String {
86 self.as_bytes().to_hex_from(chunk_size, from)
87 }
88 }
89
90 #[macro_export]
91 macro_rules! nom_line (
92 () => (line!());
93 );
94
95 #[macro_export]
96 macro_rules! nom_println (
97 ($($args:tt)*) => (println!($($args)*));
98 );
99
100 #[macro_export]
101 macro_rules! nom_stringify (
102 ($($args:tt)*) => (stringify!($($args)*));
103 );
104
105
106 /// Prints a message if the parser fails
107 ///
108 /// The message prints the `Error` or `Incomplete`
109 /// and the parser's calling code
110 ///
111 /// ```
112 /// # #[macro_use] extern crate nom;
113 /// # fn main() {
114 /// named!(f, dbg!( tag!( "abcd" ) ) );
115 ///
116 /// let a = &b"efgh"[..];
117 ///
118 /// // Will print the following message:
119 /// // Error(Position(0, [101, 102, 103, 104])) at l.5 by ' tag ! ( "abcd" ) '
120 /// f(a);
121 /// # }
122 /// ```
123 #[macro_export(local_inner_macros)]
124 macro_rules! dbg (
125 ($i: expr, $submac:ident!( $($args:tt)* )) => (
126 {
127 use $crate::lib::std::result::Result::*;
128 let l = nom_line!();
129 match $submac!($i, $($args)*) {
130 Err(e) => {
131 nom_println!("Err({:?}) at l.{} by ' {} '", e, l, nom_stringify!($submac!($($args)*)));
132 Err(e)
133 },
134 a => a,
135 }
136 }
137 );
138
139 ($i:expr, $f:ident) => (
140 dbg!($i, call!($f));
141 );
142 );
143
144 /// Prints a message and the input if the parser fails
145 ///
146 /// The message prints the `Error` or `Incomplete`
147 /// and the parser's calling code.
148 ///
149 /// It also displays the input in hexdump format
150 ///
151 /// ```ignore
152 /// # #[macro_use] extern crate nom;
153 /// # fn main() {
154 /// named!(f, dbg_dmp!( tag!( "abcd" ) ) );
155 ///
156 /// let a = &b"efghijkl"[..];
157 ///
158 /// // Will print the following message:
159 /// // Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at l.5 by ' tag ! ( "abcd" ) '
160 /// // 00000000 65 66 67 68 69 6a 6b 6c efghijkl
161 /// f(a);
162 /// # }
163 #[macro_export(local_inner_macros)]
164 macro_rules! dbg_dmp (
165 ($i: expr, $submac:ident!( $($args:tt)* )) => (
166 {
167 use $crate::HexDisplay;
168 let l = nom_line!();
169 match $submac!($i, $($args)*) {
170 Err(e) => {
171 nom_println!("Error({:?}) at l.{} by ' {} '\n{}", e, l, nom_stringify!($submac!($($args)*)), $i.to_hex(8));
172 Err(e)
173 },
174 a => a,
175 }
176 }
177 );
178
179 ($i:expr, $f:ident) => (
180 dbg_dmp!($i, call!($f));
181 );
182 );
183
184 #[cfg(feature = "verbose-errors")]
error_to_list<P: Clone, E: Clone>(e: &Context<P, E>) -> Vec<(P, ErrorKind<E>)>185 pub fn error_to_list<P: Clone, E: Clone>(e: &Context<P, E>) -> Vec<(P, ErrorKind<E>)> {
186 match e {
187 &Context::Code(ref i, ref err) => {
188 let mut v = Vec::new();
189 v.push((i.clone(), err.clone()));
190 return v;
191 }
192 &Context::List(ref v) => {
193 let mut v2 = v.clone();
194 v2.reverse();
195 v2
196 }
197 }
198 }
199
200 #[cfg(feature = "verbose-errors")]
compare_error_paths<P: Clone + PartialEq, E: Clone + PartialEq>(e1: &Context<P, E>, e2: &Context<P, E>) -> bool201 pub fn compare_error_paths<P: Clone + PartialEq, E: Clone + PartialEq>(e1: &Context<P, E>, e2: &Context<P, E>) -> bool {
202 error_to_list(e1) == error_to_list(e2)
203 }
204
205 #[cfg(feature = "std")]
206 #[cfg(feature = "verbose-errors")]
207 use lib::std::hash::Hash;
208
209 #[cfg(feature = "std")]
210 #[cfg(feature = "verbose-errors")]
add_error_pattern<'a, I: Clone + Hash + Eq, O, E: Clone + Hash + Eq>( h: &mut HashMap<Vec<(I, ErrorKind<E>)>, &'a str>, res: IResult<I, O, E>, message: &'a str, ) -> bool211 pub fn add_error_pattern<'a, I: Clone + Hash + Eq, O, E: Clone + Hash + Eq>(
212 h: &mut HashMap<Vec<(I, ErrorKind<E>)>, &'a str>,
213 res: IResult<I, O, E>,
214 message: &'a str,
215 ) -> bool {
216 match res {
217 Err(Err::Error(e)) | Err(Err::Failure(e)) => {
218 h.insert(error_to_list(&e), message);
219 true
220 }
221 _ => false,
222 }
223 }
224
slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize)225 pub fn slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize) {
226 let start = input.as_ptr();
227 let off1 = s.as_ptr() as usize - start as usize;
228 let off2 = off1 + s.len();
229 (off1, off2)
230 }
231
232 #[cfg(feature = "std")]
233 #[cfg(feature = "verbose-errors")]
prepare_errors<O, E: Clone>(input: &[u8], res: IResult<&[u8], O, E>) -> Option<Vec<(ErrorKind<E>, usize, usize)>>234 pub fn prepare_errors<O, E: Clone>(input: &[u8], res: IResult<&[u8], O, E>) -> Option<Vec<(ErrorKind<E>, usize, usize)>> {
235 if let Err(Err::Error(e)) = res {
236 let mut v: Vec<(ErrorKind<E>, usize, usize)> = Vec::new();
237
238 match e {
239 Context::Code(p, kind) => {
240 let (o1, o2) = slice_to_offsets(input, p);
241 v.push((kind, o1, o2));
242 }
243 Context::List(mut l) => {
244 for (p, kind) in l.drain(..) {
245 let (o1, o2) = slice_to_offsets(input, p);
246 v.push((kind, o1, o2));
247 }
248
249 v.reverse()
250 }
251 }
252
253 v.sort_by(|a, b| a.1.cmp(&b.1));
254 Some(v)
255 } else {
256 None
257 }
258 }
259
260 #[cfg(feature = "std")]
261 #[cfg(feature = "verbose-errors")]
print_error<O, E: Clone>(input: &[u8], res: IResult<&[u8], O, E>)262 pub fn print_error<O, E: Clone>(input: &[u8], res: IResult<&[u8], O, E>) {
263 if let Some(v) = prepare_errors(input, res) {
264 let colors = generate_colors(&v);
265 println!("parser codes: {}", print_codes(&colors, &HashMap::new()));
266 println!("{}", print_offsets(input, 0, &v));
267 } else {
268 println!("not an error");
269 }
270 }
271
272 #[cfg(feature = "std")]
273 #[cfg(feature = "verbose-errors")]
generate_colors<E>(v: &[(ErrorKind<E>, usize, usize)]) -> HashMap<u32, u8>274 pub fn generate_colors<E>(v: &[(ErrorKind<E>, usize, usize)]) -> HashMap<u32, u8> {
275 let mut h: HashMap<u32, u8> = HashMap::new();
276 let mut color = 0;
277
278 for &(ref c, _, _) in v.iter() {
279 h.insert(error_to_u32(c), color + 31);
280 color = color + 1 % 7;
281 }
282
283 h
284 }
285
code_from_offset<E>(v: &[(ErrorKind<E>, usize, usize)], offset: usize) -> Option<u32>286 pub fn code_from_offset<E>(v: &[(ErrorKind<E>, usize, usize)], offset: usize) -> Option<u32> {
287 let mut acc: Option<(u32, usize, usize)> = None;
288 for &(ref ek, s, e) in v.iter() {
289 let c = error_to_u32(ek);
290 if s <= offset && offset <= e {
291 if let Some((_, start, end)) = acc {
292 if start <= s && e <= end {
293 acc = Some((c, s, e));
294 }
295 } else {
296 acc = Some((c, s, e));
297 }
298 }
299 }
300 if let Some((code, _, _)) = acc {
301 return Some(code);
302 } else {
303 return None;
304 }
305 }
306
307 #[cfg(feature = "alloc")]
reset_color(v: &mut Vec<u8>)308 pub fn reset_color(v: &mut Vec<u8>) {
309 v.push(0x1B);
310 v.push(b'[');
311 v.push(0);
312 v.push(b'm');
313 }
314
315 #[cfg(feature = "alloc")]
write_color(v: &mut Vec<u8>, color: u8)316 pub fn write_color(v: &mut Vec<u8>, color: u8) {
317 v.push(0x1B);
318 v.push(b'[');
319 v.push(1);
320 v.push(b';');
321 let s = color.to_string();
322 let bytes = s.as_bytes();
323 v.extend(bytes.iter().cloned());
324 v.push(b'm');
325 }
326
327 #[cfg(feature = "std")]
328 #[cfg_attr(feature = "cargo-clippy", allow(implicit_hasher))]
print_codes(colors: &HashMap<u32, u8>, names: &HashMap<u32, &str>) -> String329 pub fn print_codes(colors: &HashMap<u32, u8>, names: &HashMap<u32, &str>) -> String {
330 let mut v = Vec::new();
331 for (code, &color) in colors {
332 if let Some(&s) = names.get(code) {
333 let bytes = s.as_bytes();
334 write_color(&mut v, color);
335 v.extend(bytes.iter().cloned());
336 } else {
337 let s = code.to_string();
338 let bytes = s.as_bytes();
339 write_color(&mut v, color);
340 v.extend(bytes.iter().cloned());
341 }
342 reset_color(&mut v);
343 v.push(b' ');
344 }
345 reset_color(&mut v);
346
347 String::from_utf8_lossy(&v[..]).into_owned()
348 }
349
350 #[cfg(feature = "std")]
351 #[cfg(feature = "verbose-errors")]
print_offsets<E>(input: &[u8], from: usize, offsets: &[(ErrorKind<E>, usize, usize)]) -> String352 pub fn print_offsets<E>(input: &[u8], from: usize, offsets: &[(ErrorKind<E>, usize, usize)]) -> String {
353 let mut v = Vec::with_capacity(input.len() * 3);
354 let mut i = from;
355 let chunk_size = 8;
356 let mut current_code: Option<u32> = None;
357 let mut current_code2: Option<u32> = None;
358
359 let colors = generate_colors(&offsets);
360
361 for chunk in input.chunks(chunk_size) {
362 let s = format!("{:08x}", i);
363 for &ch in s.as_bytes().iter() {
364 v.push(ch);
365 }
366 v.push(b'\t');
367
368 let mut k = i;
369 let mut l = i;
370 for &byte in chunk {
371 if let Some(code) = code_from_offset(&offsets, k) {
372 if let Some(current) = current_code {
373 if current != code {
374 reset_color(&mut v);
375 current_code = Some(code);
376 if let Some(&color) = colors.get(&code) {
377 write_color(&mut v, color);
378 }
379 }
380 } else {
381 current_code = Some(code);
382 if let Some(&color) = colors.get(&code) {
383 write_color(&mut v, color);
384 }
385 }
386 }
387 v.push(CHARS[(byte >> 4) as usize]);
388 v.push(CHARS[(byte & 0xf) as usize]);
389 v.push(b' ');
390 k = k + 1;
391 }
392
393 reset_color(&mut v);
394
395 if chunk_size > chunk.len() {
396 for _ in 0..(chunk_size - chunk.len()) {
397 v.push(b' ');
398 v.push(b' ');
399 v.push(b' ');
400 }
401 }
402 v.push(b'\t');
403
404 for &byte in chunk {
405 if let Some(code) = code_from_offset(&offsets, l) {
406 if let Some(current) = current_code2 {
407 if current != code {
408 reset_color(&mut v);
409 current_code2 = Some(code);
410 if let Some(&color) = colors.get(&code) {
411 write_color(&mut v, color);
412 }
413 }
414 } else {
415 current_code2 = Some(code);
416 if let Some(&color) = colors.get(&code) {
417 write_color(&mut v, color);
418 }
419 }
420 }
421 if (byte >= 32 && byte <= 126) || byte >= 128 {
422 v.push(byte);
423 } else {
424 v.push(b'.');
425 }
426 l = l + 1;
427 }
428 reset_color(&mut v);
429
430 v.push(b'\n');
431 i = i + chunk_size;
432 }
433
434 String::from_utf8_lossy(&v[..]).into_owned()
435 }
436
437 /// indicates which parser returned an error
438 #[cfg_attr(rustfmt, rustfmt_skip)]
439 #[derive(Debug,PartialEq,Eq,Hash,Clone)]
440 #[allow(deprecated)]
441 pub enum ErrorKind<E = u32> {
442 Custom(E),
443 Tag,
444 MapRes,
445 MapOpt,
446 Alt,
447 IsNot,
448 IsA,
449 SeparatedList,
450 SeparatedNonEmptyList,
451 Many0,
452 Many1,
453 ManyTill,
454 Count,
455 TakeUntilAndConsume,
456 TakeUntil,
457 TakeUntilEitherAndConsume,
458 TakeUntilEither,
459 LengthValue,
460 TagClosure,
461 Alpha,
462 Digit,
463 HexDigit,
464 OctDigit,
465 AlphaNumeric,
466 Space,
467 MultiSpace,
468 LengthValueFn,
469 Eof,
470 ExprOpt,
471 ExprRes,
472 CondReduce,
473 Switch,
474 TagBits,
475 OneOf,
476 NoneOf,
477 Char,
478 CrLf,
479 RegexpMatch,
480 RegexpMatches,
481 RegexpFind,
482 RegexpCapture,
483 RegexpCaptures,
484 TakeWhile1,
485 Complete,
486 Fix,
487 Escaped,
488 EscapedTransform,
489 #[deprecated(since = "4.0.0", note = "Please use `Tag` instead")]
490 TagStr,
491 #[deprecated(since = "4.0.0", note = "Please use `IsNot` instead")]
492 IsNotStr,
493 #[deprecated(since = "4.0.0", note = "Please use `IsA` instead")]
494 IsAStr,
495 #[deprecated(since = "4.0.0", note = "Please use `TakeWhile1` instead")]
496 TakeWhile1Str,
497 NonEmpty,
498 ManyMN,
499 #[deprecated(since = "4.0.0", note = "Please use `TakeUntilAndConsume` instead")]
500 TakeUntilAndConsumeStr,
501 #[deprecated(since = "4.0.0", note = "Please use `TakeUntil` instead")]
502 TakeUntilStr,
503 Not,
504 Permutation,
505 Verify,
506 TakeTill1,
507 TakeUntilAndConsume1,
508 TakeWhileMN,
509 ParseTo,
510 TooLarge,
511 Many0Count,
512 Many1Count,
513 }
514
515 #[cfg_attr(rustfmt, rustfmt_skip)]
516 #[allow(deprecated)]
error_to_u32<E>(e: &ErrorKind<E>) -> u32517 pub fn error_to_u32<E>(e: &ErrorKind<E>) -> u32 {
518 match *e {
519 ErrorKind::Custom(_) => 0,
520 ErrorKind::Tag => 1,
521 ErrorKind::MapRes => 2,
522 ErrorKind::MapOpt => 3,
523 ErrorKind::Alt => 4,
524 ErrorKind::IsNot => 5,
525 ErrorKind::IsA => 6,
526 ErrorKind::SeparatedList => 7,
527 ErrorKind::SeparatedNonEmptyList => 8,
528 ErrorKind::Many1 => 9,
529 ErrorKind::Count => 10,
530 ErrorKind::TakeUntilAndConsume => 11,
531 ErrorKind::TakeUntil => 12,
532 ErrorKind::TakeUntilEitherAndConsume => 13,
533 ErrorKind::TakeUntilEither => 14,
534 ErrorKind::LengthValue => 15,
535 ErrorKind::TagClosure => 16,
536 ErrorKind::Alpha => 17,
537 ErrorKind::Digit => 18,
538 ErrorKind::AlphaNumeric => 19,
539 ErrorKind::Space => 20,
540 ErrorKind::MultiSpace => 21,
541 ErrorKind::LengthValueFn => 22,
542 ErrorKind::Eof => 23,
543 ErrorKind::ExprOpt => 24,
544 ErrorKind::ExprRes => 25,
545 ErrorKind::CondReduce => 26,
546 ErrorKind::Switch => 27,
547 ErrorKind::TagBits => 28,
548 ErrorKind::OneOf => 29,
549 ErrorKind::NoneOf => 30,
550 ErrorKind::Char => 40,
551 ErrorKind::CrLf => 41,
552 ErrorKind::RegexpMatch => 42,
553 ErrorKind::RegexpMatches => 43,
554 ErrorKind::RegexpFind => 44,
555 ErrorKind::RegexpCapture => 45,
556 ErrorKind::RegexpCaptures => 46,
557 ErrorKind::TakeWhile1 => 47,
558 ErrorKind::Complete => 48,
559 ErrorKind::Fix => 49,
560 ErrorKind::Escaped => 50,
561 ErrorKind::EscapedTransform => 51,
562 ErrorKind::TagStr => 52,
563 ErrorKind::IsNotStr => 53,
564 ErrorKind::IsAStr => 54,
565 ErrorKind::TakeWhile1Str => 55,
566 ErrorKind::NonEmpty => 56,
567 ErrorKind::ManyMN => 57,
568 ErrorKind::TakeUntilAndConsumeStr => 58,
569 ErrorKind::HexDigit => 59,
570 ErrorKind::TakeUntilStr => 60,
571 ErrorKind::OctDigit => 61,
572 ErrorKind::Many0 => 62,
573 ErrorKind::Not => 63,
574 ErrorKind::Permutation => 64,
575 ErrorKind::ManyTill => 65,
576 ErrorKind::Verify => 66,
577 ErrorKind::TakeTill1 => 67,
578 ErrorKind::TakeUntilAndConsume1 => 68,
579 ErrorKind::TakeWhileMN => 69,
580 ErrorKind::ParseTo => 70,
581 ErrorKind::TooLarge => 71,
582 ErrorKind::Many0Count => 72,
583 ErrorKind::Many1Count => 73,
584 }
585 }
586
587 impl<E> ErrorKind<E> {
588 #[cfg_attr(rustfmt, rustfmt_skip)]
589 #[allow(deprecated)]
description(&self) -> &str590 pub fn description(&self) -> &str {
591 match *self {
592 ErrorKind::Custom(_) => "Custom error",
593 ErrorKind::Tag => "Tag",
594 ErrorKind::MapRes => "Map on Result",
595 ErrorKind::MapOpt => "Map on Option",
596 ErrorKind::Alt => "Alternative",
597 ErrorKind::IsNot => "IsNot",
598 ErrorKind::IsA => "IsA",
599 ErrorKind::SeparatedList => "Separated list",
600 ErrorKind::SeparatedNonEmptyList => "Separated non empty list",
601 ErrorKind::Many0 => "Many0",
602 ErrorKind::Many1 => "Many1",
603 ErrorKind::Count => "Count",
604 ErrorKind::TakeUntilAndConsume => "Take until and consume",
605 ErrorKind::TakeUntil => "Take until",
606 ErrorKind::TakeUntilEitherAndConsume => "Take until either and consume",
607 ErrorKind::TakeUntilEither => "Take until either",
608 ErrorKind::LengthValue => "Length followed by value",
609 ErrorKind::TagClosure => "Tag closure",
610 ErrorKind::Alpha => "Alphabetic",
611 ErrorKind::Digit => "Digit",
612 ErrorKind::AlphaNumeric => "AlphaNumeric",
613 ErrorKind::Space => "Space",
614 ErrorKind::MultiSpace => "Multiple spaces",
615 ErrorKind::LengthValueFn => "LengthValueFn",
616 ErrorKind::Eof => "End of file",
617 ErrorKind::ExprOpt => "Evaluate Option",
618 ErrorKind::ExprRes => "Evaluate Result",
619 ErrorKind::CondReduce => "Condition reduce",
620 ErrorKind::Switch => "Switch",
621 ErrorKind::TagBits => "Tag on bitstream",
622 ErrorKind::OneOf => "OneOf",
623 ErrorKind::NoneOf => "NoneOf",
624 ErrorKind::Char => "Char",
625 ErrorKind::CrLf => "CrLf",
626 ErrorKind::RegexpMatch => "RegexpMatch",
627 ErrorKind::RegexpMatches => "RegexpMatches",
628 ErrorKind::RegexpFind => "RegexpFind",
629 ErrorKind::RegexpCapture => "RegexpCapture",
630 ErrorKind::RegexpCaptures => "RegexpCaptures",
631 ErrorKind::TakeWhile1 => "TakeWhile1",
632 ErrorKind::Complete => "Complete",
633 ErrorKind::Fix => "Fix",
634 ErrorKind::Escaped => "Escaped",
635 ErrorKind::EscapedTransform => "EscapedTransform",
636 ErrorKind::TagStr => "Tag on strings",
637 ErrorKind::IsNotStr => "IsNot on strings",
638 ErrorKind::IsAStr => "IsA on strings",
639 ErrorKind::TakeWhile1Str => "TakeWhile1 on strings",
640 ErrorKind::NonEmpty => "NonEmpty",
641 ErrorKind::ManyMN => "Many(m, n)",
642 ErrorKind::TakeUntilAndConsumeStr => "Take until and consume on strings",
643 ErrorKind::HexDigit => "Hexadecimal Digit",
644 ErrorKind::TakeUntilStr => "Take until on strings",
645 ErrorKind::OctDigit => "Octal digit",
646 ErrorKind::Not => "Negation",
647 ErrorKind::Permutation => "Permutation",
648 ErrorKind::ManyTill => "ManyTill",
649 ErrorKind::Verify => "predicate verification",
650 ErrorKind::TakeTill1 => "TakeTill1",
651 ErrorKind::TakeUntilAndConsume1 => "Take at least 1 until and consume",
652 ErrorKind::TakeWhileMN => "TakeWhileMN",
653 ErrorKind::ParseTo => "Parse string to the specified type",
654 ErrorKind::TooLarge => "Needed data size is too large",
655 ErrorKind::Many0Count => "Count occurrence of >=0 patterns",
656 ErrorKind::Many1Count => "Count occurrence of >=1 patterns",
657 }
658 }
659
660 /// Convert Err into an ErrorKind.
661 ///
662 /// This allows application code to use ErrorKind and stay independent from the `verbose-errors` features activation.
into_error_kind(self) -> ErrorKind<E>663 pub fn into_error_kind(self) -> ErrorKind<E> {
664 self
665 }
666 }
667
668 pub trait Convert<T> {
convert(T) -> Self669 fn convert(T) -> Self;
670 }
671
672 impl<F, E: From<F>> Convert<ErrorKind<F>> for ErrorKind<E> {
673 #[cfg_attr(rustfmt, rustfmt_skip)]
674 #[allow(deprecated)]
convert(e: ErrorKind<F>) -> Self675 fn convert(e: ErrorKind<F>) -> Self {
676 match e {
677 ErrorKind::Custom(c) => ErrorKind::Custom(E::from(c)),
678 ErrorKind::Tag => ErrorKind::Tag,
679 ErrorKind::MapRes => ErrorKind::MapRes,
680 ErrorKind::MapOpt => ErrorKind::MapOpt,
681 ErrorKind::Alt => ErrorKind::Alt,
682 ErrorKind::IsNot => ErrorKind::IsNot,
683 ErrorKind::IsA => ErrorKind::IsA,
684 ErrorKind::SeparatedList => ErrorKind::SeparatedList,
685 ErrorKind::SeparatedNonEmptyList => ErrorKind::SeparatedNonEmptyList,
686 ErrorKind::Many1 => ErrorKind::Many1,
687 ErrorKind::Count => ErrorKind::Count,
688 ErrorKind::TakeUntilAndConsume => ErrorKind::TakeUntilAndConsume,
689 ErrorKind::TakeUntil => ErrorKind::TakeUntil,
690 ErrorKind::TakeUntilEitherAndConsume => ErrorKind::TakeUntilEitherAndConsume,
691 ErrorKind::TakeUntilEither => ErrorKind::TakeUntilEither,
692 ErrorKind::LengthValue => ErrorKind::LengthValue,
693 ErrorKind::TagClosure => ErrorKind::TagClosure,
694 ErrorKind::Alpha => ErrorKind::Alpha,
695 ErrorKind::Digit => ErrorKind::Digit,
696 ErrorKind::AlphaNumeric => ErrorKind::AlphaNumeric,
697 ErrorKind::Space => ErrorKind::Space,
698 ErrorKind::MultiSpace => ErrorKind::MultiSpace,
699 ErrorKind::LengthValueFn => ErrorKind::LengthValueFn,
700 ErrorKind::Eof => ErrorKind::Eof,
701 ErrorKind::ExprOpt => ErrorKind::ExprOpt,
702 ErrorKind::ExprRes => ErrorKind::ExprRes,
703 ErrorKind::CondReduce => ErrorKind::CondReduce,
704 ErrorKind::Switch => ErrorKind::Switch,
705 ErrorKind::TagBits => ErrorKind::TagBits,
706 ErrorKind::OneOf => ErrorKind::OneOf,
707 ErrorKind::NoneOf => ErrorKind::NoneOf,
708 ErrorKind::Char => ErrorKind::Char,
709 ErrorKind::CrLf => ErrorKind::CrLf,
710 ErrorKind::RegexpMatch => ErrorKind::RegexpMatch,
711 ErrorKind::RegexpMatches => ErrorKind::RegexpMatches,
712 ErrorKind::RegexpFind => ErrorKind::RegexpFind,
713 ErrorKind::RegexpCapture => ErrorKind::RegexpCapture,
714 ErrorKind::RegexpCaptures => ErrorKind::RegexpCaptures,
715 ErrorKind::TakeWhile1 => ErrorKind::TakeWhile1,
716 ErrorKind::Complete => ErrorKind::Complete,
717 ErrorKind::Fix => ErrorKind::Fix,
718 ErrorKind::Escaped => ErrorKind::Escaped,
719 ErrorKind::EscapedTransform => ErrorKind::EscapedTransform,
720 ErrorKind::TagStr => ErrorKind::TagStr,
721 ErrorKind::IsNotStr => ErrorKind::IsNotStr,
722 ErrorKind::IsAStr => ErrorKind::IsAStr,
723 ErrorKind::TakeWhile1Str => ErrorKind::TakeWhile1Str,
724 ErrorKind::NonEmpty => ErrorKind::NonEmpty,
725 ErrorKind::ManyMN => ErrorKind::ManyMN,
726 ErrorKind::TakeUntilAndConsumeStr => ErrorKind::TakeUntilAndConsumeStr,
727 ErrorKind::HexDigit => ErrorKind::HexDigit,
728 ErrorKind::TakeUntilStr => ErrorKind::TakeUntilStr,
729 ErrorKind::OctDigit => ErrorKind::OctDigit,
730 ErrorKind::Many0 => ErrorKind::Many0,
731 ErrorKind::Not => ErrorKind::Not,
732 ErrorKind::Permutation => ErrorKind::Permutation,
733 ErrorKind::ManyTill => ErrorKind::ManyTill,
734 ErrorKind::Verify => ErrorKind::Verify,
735 ErrorKind::TakeTill1 => ErrorKind::TakeTill1,
736 ErrorKind::TakeUntilAndConsume1 => ErrorKind::TakeUntilAndConsume1,
737 ErrorKind::TakeWhileMN => ErrorKind::TakeWhileMN,
738 ErrorKind::ParseTo => ErrorKind::ParseTo,
739 ErrorKind::TooLarge => ErrorKind::TooLarge,
740 ErrorKind::Many0Count => ErrorKind::Many0Count,
741 ErrorKind::Many1Count => ErrorKind::Many1Count,
742 }
743 }
744 }
745