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
/// When tokens are used or separators are used between elements,
/// whitespace is often allowed before or after these characters:

///    STAR    =  SWS "*" SWS ; asterisk
///    SLASH   =  SWS "/" SWS ; slash
///    EQUAL   =  SWS "=" SWS ; equal
///    LPAREN  =  SWS "(" SWS ; left parenthesis
///    RPAREN  =  SWS ")" SWS ; right parenthesis
///    RAQUOT  =  ">" SWS ; right angle quote
///    LAQUOT  =  SWS "<"; left angle quote
///    COMMA   =  SWS "," SWS ; comma
///    SEMI    =  SWS ";" SWS ; semicolon
///    COLON   =  SWS ":" SWS ; colon
///    LDQUOT  =  SWS DQUOTE; open double quotation mark
///    RDQUOT  =  DQUOTE SWS ; close double quotation mark
use crate::common::{
    errorparse::SipParseError,
    nom_wrappers::{take_sws, take_while_trim_sws},
};
use nom::{bytes::complete::tag, sequence::tuple};

macro_rules! take_func {
    ($inp: expr, $chr:expr) => {
        take_while_trim_sws($inp, |c: u8| c == $chr)
    };
}

pub fn star(input: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8], &[u8]), SipParseError> {
    take_func!(input, b'*')
}
pub fn slash(input: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8], &[u8]), SipParseError> {
    take_func!(input, b'/')
}
pub fn equal(input: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8], &[u8]), SipParseError> {
    take_func!(input, b'=')
}
pub fn lparen(input: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8], &[u8]), SipParseError> {
    take_func!(input, b'(')
}
pub fn rparen(input: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8], &[u8]), SipParseError> {
    take_func!(input, b')')
}

pub fn comma(input: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8], &[u8]), SipParseError> {
    take_func!(input, b',')
}
pub fn semi(input: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8], &[u8]), SipParseError> {
    take_func!(input, b';')
}
pub fn colon(input: &[u8]) -> nom::IResult<&[u8], (&[u8], &[u8], &[u8]), SipParseError> {
    take_func!(input, b':')
}

pub fn raquot(input: &[u8]) -> nom::IResult<&[u8], &[u8], SipParseError> {
    let (input, (_, wsps)) = tuple((tag(">"), take_sws))(input)?;
    Ok((input, wsps))
}
pub fn laquot(input: &[u8]) -> nom::IResult<&[u8], &[u8], SipParseError> {
    let (input, (wsps, _)) = tuple((take_sws, tag("<")))(input)?;
    Ok((input, wsps))
}
pub fn ldquot(input: &[u8]) -> nom::IResult<&[u8], &[u8], SipParseError> {
    let (input, (wsps, _)) = tuple((take_sws, tag("\"")))(input)?;
    Ok((input, wsps))
}
pub fn rdquot(input: &[u8]) -> nom::IResult<&[u8], &[u8], SipParseError> {
    let (input, (_, wsps)) = tuple((tag("\""), take_sws))(input)?;
    Ok((input, wsps))
}