1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use core::convert::From;
use core::str;
use nom;
use nom::error::{ErrorKind, ParseError};
#[derive(Debug)]
pub struct SipParseError<'a> {
pub code: u32,
pub message: Option<&'a str>,
}
impl<'a> From<(&'a str, ErrorKind)> for SipParseError<'a> {
fn from(error: (&'a str, ErrorKind)) -> Self {
SipParseError {
code: error.1 as u32,
message: Some(error.0),
}
}
}
impl<'a> ParseError<&'a str> for SipParseError<'a> {
fn from_error_kind(error: &'a str, kind: ErrorKind) -> Self {
SipParseError {
code: kind as u32,
message: Some(error),
}
}
fn append(error: &'a str, kind: ErrorKind, _other: SipParseError) -> Self {
SipParseError {
code: kind as u32,
message: Some(error),
}
}
}
#[macro_export]
macro_rules! sip_parse_error {
($error_code:expr) => {
Err(nom::Err::Error(SipParseError::new($error_code, None)))
};
($error_code:expr, $message:expr) => {
Err(nom::Err::Error(SipParseError::new(
$error_code,
Some($message),
)))
};
}
impl<'a> SipParseError<'a> {
pub fn new(code: u32, message: Option<&'a str>) -> SipParseError {
SipParseError {
code: code,
message: message,
}
}
}
impl<'a> ParseError<&'a [u8]> for SipParseError<'a> {
fn from_error_kind(error: &'a [u8], kind: ErrorKind) -> Self {
let error_str: &str;
match str::from_utf8(error) {
Ok(err_str) => {
error_str = err_str;
}
Err(_) => {
error_str = "Internal error of parser. Can't cast error string to to utf8";
}
}
SipParseError {
code: kind as u32,
message: Some(error_str),
}
}
fn append(error: &'a [u8], kind: ErrorKind, _other: SipParseError) -> Self {
let error_str: &str;
match str::from_utf8(error) {
Ok(err_str) => {
error_str = err_str;
}
Err(_) => {
error_str = "Internal error of parser. Can't cast error string to to utf8";
}
}
SipParseError {
code: kind as u32,
message: Some(error_str),
}
}
}