Upgrading to nom 5

This commit is contained in:
Rodolphe Breard 2019-06-29 17:29:52 +02:00
parent 866298c948
commit 4a99257263
3 changed files with 95 additions and 112 deletions

View file

@ -13,7 +13,7 @@ include = ["src/**/*", "Cargo.toml", "LICENSE-*.txt"]
[dependencies] [dependencies]
log = {version = "0.4", features = ["std"]} log = {version = "0.4", features = ["std"]}
nom = "4.2" nom = "5.0"
opensmtpd_derive = { path = "../opensmtpd-derive", version = "0.1" } opensmtpd_derive = { path = "../opensmtpd-derive", version = "0.1" }
[[example]] [[example]]

View file

@ -7,7 +7,13 @@
// except according to those terms. // except according to those terms.
use crate::errors::Error; use crate::errors::Error;
use nom::{alt_complete, call, cond, do_parse, map_res, named, opt, tag, take_until, take_while}; 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::IResult;
use std::str::FromStr; use std::str::FromStr;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -45,7 +51,8 @@ impl FromStr for Event {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_lowercase() let s = s
.to_lowercase()
.replace("link", "link-") .replace("link", "link-")
.replace("tx", "tx-") .replace("tx", "tx-")
.replace("protocol", "protocol-") .replace("protocol", "protocol-")
@ -111,117 +118,93 @@ impl FromStr for Entry {
} }
} }
fn is_ascii_digit(c: char) -> bool { fn separator(input: &str) -> IResult<&str, &str> {
c.is_ascii_digit() tag("|")(input)
} }
fn is_ascii_digit_or_neg(c: char) -> bool { fn parse_kind(input: &str) -> IResult<&str, Kind> {
c.is_ascii_digit() || c == '-' alt((
value(Kind::Report, tag("report")),
value(Kind::Filter, tag("filter")),
))(input)
} }
fn is_ascii_hexdigit(c: char) -> bool { fn parse_version(input: &str) -> IResult<&str, u8> {
c.is_ascii_hexdigit() map_res(digit1, |s: &str| s.parse::<u8>())(input)
} }
fn to_u8(s: &str) -> Result<u8, std::num::ParseIntError> { fn parse_timestamp(input: &str) -> IResult<&str, TimeVal> {
s.parse() let (input, sec) = map_res(digit1, |s: &str| s.parse::<i64>())(input)?;
let (input, _) = tag(".")(input)?;
let (input, usec) = map_res(digit1, |s: &str| s.parse::<i64>())(input)?;
let timestamp = TimeVal { sec, usec };
Ok((input, timestamp))
} }
fn to_i64(s: &str) -> Result<i64, std::num::ParseIntError> { fn parse_subsystem(input: &str) -> IResult<&str, Subsystem> {
s.parse() alt((
value(Subsystem::SmtpIn, tag("smtp-in")),
value(Subsystem::SmtpOut, tag("smtp-out")),
))(input)
} }
fn to_u64_hex(s: &str) -> Result<u64, std::num::ParseIntError> { fn parse_event(input: &str) -> IResult<&str, Event> {
u64::from_str_radix(s, 16) alt((
value(Event::LinkConnect, tag("link-connect")),
value(Event::LinkDisconnect, tag("link-disconnect")),
value(Event::LinkIdentify, tag("link-identify")),
value(Event::LinkTls, tag("link-tls")),
value(Event::TxBegin, tag("tx-begin")),
value(Event::TxMail, tag("tx-mail")),
value(Event::TxRcpt, tag("tx-rcpt")),
value(Event::TxEnvelope, tag("tx-envelope")),
value(Event::TxData, tag("tx-data")),
value(Event::TxCommit, tag("tx-commit")),
value(Event::TxRollback, tag("tx-rollback")),
value(Event::ProtocolClient, tag("protocol-client")),
value(Event::ProtocolServer, tag("protocol-server")),
value(Event::Timeout, tag("timeout")),
value(Event::FilterResponse, tag("filter-response")),
))(input)
} }
named!(parse_i64<&str, i64>, fn parse_token(input: &str) -> IResult<&str, u64> {
map_res!(take_while!(is_ascii_digit_or_neg), to_i64) map_res(hex_digit1, |s: &str| u64::from_str_radix(s, 16))(input)
); }
named!(parse_u64_hex<&str, u64>, fn parse_session_id(input: &str) -> IResult<&str, u64> {
map_res!(take_while!(is_ascii_hexdigit), to_u64_hex) map_res(hex_digit1, |s: &str| u64::from_str_radix(s, 16))(input)
); }
named!(parse_kind<&str, Kind>, fn parse_params(input: &str) -> IResult<&str, String> {
alt_complete!( let (input, params) = many_till(anychar, line_ending)(input)?;
tag!("report") => { |_| Kind::Report } | Ok((input, params.0.into_iter().collect()))
tag!("filter") => { |_| Kind::Filter } }
)
);
named!(parse_version<&str, u8>, fn parse_entry(input: &str) -> IResult<&str, Entry> {
map_res!(take_while!(is_ascii_digit), to_u8) let (input, kind) = parse_kind(input)?;
); let (input, _) = separator(input)?;
let (input, version) = parse_version(input)?;
named!(parse_timestamp<&str, TimeVal>, let (input, _) = separator(input)?;
do_parse!( let (input, timestamp) = parse_timestamp(input)?;
sec: parse_i64 >> let (input, _) = separator(input)?;
tag!(".") >> let (input, subsystem) = parse_subsystem(input)?;
usec: parse_i64 >> let (input, _) = separator(input)?;
(TimeVal { sec, usec}) let (input, event) = parse_event(input)?;
) let (input, token) = if kind == Kind::Filter {
); let (input, _) = separator(input)?;
let (input, token) = parse_token(input)?;
named!(parse_subsystem<&str, Subsystem>, (input, Some(token))
alt_complete! ( } else {
tag!("smtp-in") => { |_| Subsystem::SmtpIn } | (input, None)
tag!("smtp-out") => { |_| Subsystem::SmtpOut } };
) let (input, _) = separator(input)?;
); let (input, session_id) = parse_session_id(input)?;
let (input, params) = opt(preceded(separator, parse_params))(input)?;
named!(parse_event<&str, Event>, if params.is_none() {
alt_complete!( let _ = line_ending(input)?;
tag!("link-connect") => { |_| Event::LinkConnect } | }
tag!("link-disconnect") => { |_| Event::LinkDisconnect } | let entry = Entry {
tag!("link-identify") => { |_| Event::LinkIdentify } |
tag!("link-tls") => { |_| Event::LinkTls } |
tag!("tx-begin") => { |_| Event::TxBegin } |
tag!("tx-mail") => { |_| Event::TxMail } |
tag!("tx-rcpt") => { |_| Event::TxRcpt } |
tag!("tx-envelope") => { |_| Event::TxEnvelope } |
tag!("tx-data") => { |_| Event::TxData } |
tag!("tx-commit") => { |_| Event::TxCommit } |
tag!("tx-rollback") => { |_| Event::TxRollback } |
tag!("protocol-client") => { |_| Event::ProtocolClient } |
tag!("protocol-server") => { |_| Event::ProtocolServer } |
tag!("timeout") => { |_| Event::Timeout } |
tag!("filter-response") => { |_| Event::FilterResponse }
)
);
named!(parse_token<&str, u64>,
do_parse!(
token: parse_u64_hex >>
tag!("|") >>
(token)
)
);
named!(parse_params<&str, String>,
do_parse!(
tag!("|") >>
s: take_until!("\n") >>
(s.to_string())
)
);
named!(parse_entry<&str, Entry>,
do_parse!(
kind: parse_kind >>
tag!("|") >>
version: parse_version >>
tag!("|") >>
timestamp: parse_timestamp >>
tag!("|") >>
subsystem: parse_subsystem >>
tag!("|") >>
event: parse_event >>
tag!("|") >>
token: cond!(kind == Kind::Filter, parse_token) >>
session_id: parse_u64_hex >>
params: opt!(parse_params) >>
(Entry {
kind, kind,
version, version,
timestamp, timestamp,
@ -230,6 +213,6 @@ named!(parse_entry<&str, Entry>,
token, token,
session_id, session_id,
params, params,
}) };
) Ok((input, entry))
); }

View file

@ -40,8 +40,8 @@ impl From<log::SetLoggerError> for Error {
} }
} }
impl From<nom::Err<&str>> for Error { impl From<nom::Err<(&str, nom::error::ErrorKind)>> for Error {
fn from(error: nom::Err<&str>) -> Self { fn from(error: nom::Err<(&str, nom::error::ErrorKind)>) -> Self {
let msg = match error { let msg = match error {
nom::Err::Incomplete(_) => "not enough data".to_string(), nom::Err::Incomplete(_) => "not enough data".to_string(),
nom::Err::Error(c) => format!("{:?}", c), nom::Err::Error(c) => format!("{:?}", c),