Accept negative values for the timestamp

This commit is contained in:
Rodolphe Breard 2019-01-05 17:53:56 +01:00
parent c127c5ca24
commit ccda4b1517

View file

@ -57,10 +57,21 @@ pub struct Entry {
pub params: Option<String>, pub params: Option<String>,
} }
impl Entry {
pub fn from_str(entry: &str) -> Result<Entry, Error> {
let (_, res) = parse_entry(entry)?;
Ok(res)
}
}
fn is_ascii_digit(c: char) -> bool { fn is_ascii_digit(c: char) -> bool {
c.is_ascii_digit() c.is_ascii_digit()
} }
fn is_ascii_digit_or_neg(c: char) -> bool {
c.is_ascii_digit() || c == '-'
}
fn is_ascii_hexdigit(c: char) -> bool { fn is_ascii_hexdigit(c: char) -> bool {
c.is_ascii_hexdigit() c.is_ascii_hexdigit()
} }
@ -77,6 +88,14 @@ fn to_u64_hex(s: &str) -> Result<u64, std::num::ParseIntError> {
u64::from_str_radix(s, 16) u64::from_str_radix(s, 16)
} }
named!(parse_i64<&str, i64>,
map_res!(take_while!(is_ascii_digit_or_neg), to_i64)
);
named!(parse_u64_hex<&str, u64>,
map_res!(take_while!(is_ascii_hexdigit), to_u64_hex)
);
named!(parse_kind<&str, Kind>, named!(parse_kind<&str, Kind>,
alt_complete!( alt_complete!(
tag!("report") => { |_| Kind::Report } | tag!("report") => { |_| Kind::Report } |
@ -84,6 +103,19 @@ named!(parse_kind<&str, Kind>,
) )
); );
named!(parse_version<&str, u8>,
map_res!(take_while!(is_ascii_digit), to_u8)
);
named!(parse_timestamp<&str, TimeVal>,
do_parse!(
sec: parse_i64 >>
tag!(".") >>
usec: parse_i64 >>
(TimeVal { sec, usec})
)
);
named!(parse_subsystem<&str, Subsystem>, named!(parse_subsystem<&str, Subsystem>,
alt_complete! ( alt_complete! (
tag!("smtp-in") => { |_| Subsystem::SmtpIn } | tag!("smtp-in") => { |_| Subsystem::SmtpIn } |
@ -111,27 +143,6 @@ named!(parse_event<&str, Event>,
) )
); );
named!(parse_timestamp<&str, TimeVal>,
do_parse!(
sec: parse_i64 >>
tag!(".") >>
usec: parse_i64 >>
(TimeVal { sec, usec})
)
);
named!(parse_version<&str, u8>,
map_res!(take_while!(is_ascii_digit), to_u8)
);
named!(parse_i64<&str, i64>,
map_res!(take_while!(is_ascii_digit), to_i64)
);
named!(parse_u64_hex<&str, u64>,
map_res!(take_while!(is_ascii_hexdigit), to_u64_hex)
);
named!(parse_token<&str, u64>, named!(parse_token<&str, u64>,
do_parse!( do_parse!(
token: parse_u64_hex >> token: parse_u64_hex >>
@ -172,14 +183,7 @@ named!(
event, event,
token, token,
session_id, session_id,
params: params, params,
}) })
) )
); );
impl Entry {
pub fn from_str(entry: &str) -> Result<Entry, Error> {
let (_, res) = parse_entry(entry)?;
Ok(res)
}
}