From ccda4b15171157f3e7c47203f65a07d09419f99e Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 5 Jan 2019 17:53:56 +0100 Subject: [PATCH] Accept negative values for the timestamp --- src/entry.rs | 62 ++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index 63cf365..0a2ab03 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -57,10 +57,21 @@ pub struct Entry { pub params: Option, } +impl Entry { + pub fn from_str(entry: &str) -> Result { + let (_, res) = parse_entry(entry)?; + Ok(res) + } +} + fn is_ascii_digit(c: char) -> bool { 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 { c.is_ascii_hexdigit() } @@ -77,6 +88,14 @@ fn to_u64_hex(s: &str) -> Result { 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>, alt_complete!( 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>, alt_complete! ( 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>, do_parse!( token: parse_u64_hex >> @@ -172,14 +183,7 @@ named!( event, token, session_id, - params: params, + params, }) ) ); - -impl Entry { - pub fn from_str(entry: &str) -> Result { - let (_, res) = parse_entry(entry)?; - Ok(res) - } -}