From 40c99af086ffe17707f6a20a29e21e41319a3bec Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Thu, 11 Jul 2019 22:42:58 +0200 Subject: [PATCH] Store parameters as a vector of strings instead of an option --- opensmtpd/src/entry.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/opensmtpd/src/entry.rs b/opensmtpd/src/entry.rs index 99f7b94..d809cff 100644 --- a/opensmtpd/src/entry.rs +++ b/opensmtpd/src/entry.rs @@ -8,11 +8,10 @@ use crate::errors::Error; use nom::branch::alt; -use nom::bytes::complete::tag; -use nom::character::complete::{anychar, digit1, hex_digit1, line_ending}; -use nom::combinator::{map_res, opt, value}; -use nom::multi::many_till; -use nom::sequence::preceded; +use nom::bytes::complete::{tag, take_till}; +use nom::character::complete::{digit1, hex_digit1, line_ending}; +use nom::combinator::{map_res, value}; +use nom::multi::many0; use nom::IResult; use std::str::FromStr; @@ -149,7 +148,7 @@ pub struct V1Report { pub subsystem: Subsystem, pub event: Event, pub session_id: u64, - pub params: Option, + pub params: Vec, } #[derive(Debug)] @@ -159,7 +158,7 @@ pub struct V1Filter { pub event: Event, pub session_id: u64, pub token: u64, - pub params: Option, + pub params: Vec, } fn separator(input: &str) -> IResult<&str, &str> { @@ -220,9 +219,14 @@ fn parse_session_id(input: &str) -> IResult<&str, u64> { map_res(hex_digit1, |s: &str| u64::from_str_radix(s, 16))(input) } -fn parse_params(input: &str) -> IResult<&str, String> { - let (input, params) = many_till(anychar, line_ending)(input)?; - Ok((input, params.0.into_iter().collect())) +fn parse_param(input: &str) -> IResult<&str, String> { + let (input, _) = separator(input)?; + let (input, param) = take_till(is_end_param)(input)?; + Ok((input, param.to_string())) +} + +fn is_end_param(c: char) -> bool { + c == '|' || c == '\r' || c == '\n' } fn parse_v1_report(input: &str) -> IResult<&str, Entry> { @@ -233,10 +237,8 @@ fn parse_v1_report(input: &str) -> IResult<&str, Entry> { let (input, event) = parse_event(input)?; let (input, _) = separator(input)?; let (input, session_id) = parse_session_id(input)?; - let (input, params) = opt(preceded(separator, parse_params))(input)?; - if params.is_none() { - let _ = line_ending(input)?; - } + let (input, params) = many0(parse_param)(input)?; + let _ = line_ending(input)?; let report = V1Report { timestamp, subsystem, @@ -257,10 +259,8 @@ fn parse_v1_filter(input: &str) -> IResult<&str, Entry> { let (input, token) = parse_token(input)?; let (input, _) = separator(input)?; let (input, session_id) = parse_session_id(input)?; - let (input, params) = opt(preceded(separator, parse_params))(input)?; - if params.is_none() { - let _ = line_ending(input)?; - } + let (input, params) = many0(parse_param)(input)?; + let _ = line_ending(input)?; let filter = V1Filter { timestamp, subsystem,