src/tools/nom.rs: move nom helpers into separate file

This commit is contained in:
Dietmar Maurer 2020-06-18 12:41:13 +02:00
parent 0686b1f4db
commit 177a2de992
5 changed files with 91 additions and 96 deletions

View File

@ -33,6 +33,7 @@ pub mod ticket;
pub mod timer; pub mod timer;
pub mod statistics; pub mod statistics;
pub mod systemd; pub mod systemd;
pub mod nom;
mod wrapped_reader_stream; mod wrapped_reader_stream;
pub use wrapped_reader_stream::*; pub use wrapped_reader_stream::*;

View File

@ -1,8 +1,11 @@
use anyhow::{bail, Error}; use anyhow::{bail, Error};
use crate::tools::nom::{
multispace0, multispace1, notspace1, IResult,
};
use nom::{ use nom::{
error::VerboseError, bytes::complete::{take_while1, take_till, take_till1},
bytes::complete::{take_while, take_while1, take_till, take_till1},
combinator::{map_res, all_consuming, recognize, opt}, combinator::{map_res, all_consuming, recognize, opt},
sequence::{preceded, tuple}, sequence::{preceded, tuple},
character::complete::{digit1, char, line_ending}, character::complete::{digit1, char, line_ending},
@ -26,22 +29,6 @@ pub struct ZFSPoolInfo {
pub devices: Vec<String>, pub devices: Vec<String>,
} }
type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>;
/// Recognizes zero or more spaces and tabs (but not carage returns or line feeds)
fn multispace0(i: &str) -> IResult<&str, &str> {
take_while(|c| c == ' ' || c == '\t')(i)
}
/// Recognizes one or more spaces and tabs (but not carage returns or line feeds)
fn multispace1(i: &str) -> IResult<&str, &str> {
take_while1(|c| c == ' ' || c == '\t')(i)
}
/// Recognizes one or more non-whitespace-characters
fn notspace1(i: &str) -> IResult<&str, &str> {
take_while1(|c| !(c == ' ' || c == '\t' || c == '\n'))(i)
}
fn parse_optional_u64(i: &str) -> IResult<&str, Option<u64>> { fn parse_optional_u64(i: &str) -> IResult<&str, Option<u64>> {
if i.starts_with('-') { if i.starts_with('-') {

View File

@ -1,36 +1,19 @@
use anyhow::{bail, Error}; use anyhow::{Error};
use serde_json::{json, Value}; use serde_json::{json, Value};
use ::serde::{Deserialize, Serialize}; use ::serde::{Deserialize, Serialize};
use nom::{ use crate::tools::nom::{
error::VerboseError, parse_complete, parse_failure, multispace0, multispace1, notspace1, parse_u64, IResult,
bytes::complete::{tag, take_while, take_while1},
combinator::{map_res, all_consuming, recognize, opt},
sequence::{preceded},
character::complete::{digit1, line_ending},
multi::{many0},
}; };
type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>; use nom::{
bytes::complete::{tag, take_while, take_while1},
combinator::{opt},
sequence::{preceded},
character::complete::{line_ending},
multi::{many0,many1},
};
/// Recognizes zero or more spaces and tabs (but not carage returns or line feeds)
fn multispace0(i: &str) -> IResult<&str, &str> {
take_while(|c| c == ' ' || c == '\t')(i)
}
// Recognizes one or more spaces and tabs (but not carage returns or line feeds)
fn multispace1(i: &str) -> IResult<&str, &str> {
take_while1(|c| c == ' ' || c == '\t')(i)
}
/// Recognizes one or more non-whitespace-characters
fn notspace1(i: &str) -> IResult<&str, &str> {
take_while1(|c| !(c == ' ' || c == '\t' || c == '\n'))(i)
}
fn parse_u64(i: &str) -> IResult<&str, u64> {
map_res(recognize(digit1), str::parse)(i)
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct ZFSPoolVDevState { pub struct ZFSPoolVDevState {
@ -46,7 +29,12 @@ pub struct ZFSPoolVDevState {
fn parse_zpool_status_vdev(i: &str) -> IResult<&str, ZFSPoolVDevState> { fn parse_zpool_status_vdev(i: &str) -> IResult<&str, ZFSPoolVDevState> {
let (i, indent) = multispace0(i)?; let (n, indent) = multispace0(i)?;
if (indent.len() & 1) != 0 {
return Err(parse_failure(n, "wrong indent length"));
}
let i = n;
let (i, vdev_name) = notspace1(i)?; let (i, vdev_name) = notspace1(i)?;
let (i, state) = preceded(multispace1, notspace1)(i)?; let (i, state) = preceded(multispace1, notspace1)(i)?;
let (i, read) = preceded(multispace1, parse_u64)(i)?; let (i, read) = preceded(multispace1, parse_u64)(i)?;
@ -81,7 +69,7 @@ fn parse_zpool_status_tree(i: &str) -> IResult<&str, Vec<ZFSPoolVDevState>> {
let (i, _) = line_ending(i)?; let (i, _) = line_ending(i)?;
// parse vdev list // parse vdev list
many0(parse_zpool_status_vdev)(i) many1(parse_zpool_status_vdev)(i)
} }
fn parse_zpool_status_field(i: &str) -> IResult<&str, (String, String)> { fn parse_zpool_status_field(i: &str) -> IResult<&str, (String, String)> {
@ -127,29 +115,11 @@ fn parse_zpool_status_field(i: &str) -> IResult<&str, (String, String)> {
} }
pub fn parse_zpool_status_config_tree(i: &str) -> Result<Vec<ZFSPoolVDevState>, Error> { pub fn parse_zpool_status_config_tree(i: &str) -> Result<Vec<ZFSPoolVDevState>, Error> {
match all_consuming(parse_zpool_status_tree)(&i) { parse_complete("zfs status config tree", i, parse_zpool_status_tree)
Err(nom::Err::Error(err)) |
Err(nom::Err::Failure(err)) => {
bail!("unable to parse zfs status config tree - {}", nom::error::convert_error(&i, err));
}
Err(err) => {
bail!("unable to parse zfs status config tree: {}", err);
}
Ok((_, data)) => Ok(data),
}
} }
fn parse_zpool_status(i: &str) -> Result<Vec<(String, String)>, Error> { fn parse_zpool_status(i: &str) -> Result<Vec<(String, String)>, Error> {
match all_consuming(many0(parse_zpool_status_field))(i) { parse_complete("zfs status output", i, many0(parse_zpool_status_field))
Err(nom::Err::Error(err)) |
Err(nom::Err::Failure(err)) => {
bail!("unable to parse zfs status output - {}", nom::error::convert_error(i, err));
}
Err(err) => {
bail!("unable to parse zfs status output - {}", err);
}
Ok((_, data)) => Ok(data),
}
} }
pub fn vdev_list_to_tree(vdev_list: &[ZFSPoolVDevState]) -> Value { pub fn vdev_list_to_tree(vdev_list: &[ZFSPoolVDevState]) -> Value {

58
src/tools/nom.rs Normal file
View File

@ -0,0 +1,58 @@
use anyhow::{bail, Error};
use nom::{
error::{ParseError, VerboseError},
bytes::complete::{take_while, take_while1},
combinator::{map_res, all_consuming, recognize},
character::complete::{digit1},
};
pub type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>;
pub fn parse_error<'a>(i: &'a str, context: &'static str) -> nom::Err<VerboseError<&'a str>> {
let err = VerboseError { errors: Vec::new() };
let err = VerboseError::add_context(i, context, err);
nom::Err::Error(err)
}
pub fn parse_failure<'a>(i: &'a str, context: &'static str) -> nom::Err<VerboseError<&'a str>> {
let err = VerboseError { errors: Vec::new() };
let err = VerboseError::add_context(i, context, err);
nom::Err::Failure(err)
}
/// Recognizes zero or more spaces and tabs (but not carage returns or line feeds)
pub fn multispace0(i: &str) -> IResult<&str, &str> {
take_while(|c| c == ' ' || c == '\t')(i)
}
/// Recognizes one or more spaces and tabs (but not carage returns or line feeds)
pub fn multispace1(i: &str) -> IResult<&str, &str> {
take_while1(|c| c == ' ' || c == '\t')(i)
}
/// Recognizes one or more non-whitespace-characters
pub fn notspace1(i: &str) -> IResult<&str, &str> {
take_while1(|c| !(c == ' ' || c == '\t' || c == '\n'))(i)
}
/// Parse a 64 bit unsigned integer
pub fn parse_u64(i: &str) -> IResult<&str, u64> {
map_res(recognize(digit1), str::parse)(i)
}
pub fn parse_complete<'a, F, O>(what: &str, i: &'a str, parser: F) -> Result<O, Error>
where F: Fn(&'a str) -> IResult<&'a str, O>,
{
match all_consuming(parser)(i) {
Err(nom::Err::Error(err)) |
Err(nom::Err::Failure(err)) => {
bail!("unable to parse {} - {}", what, nom::error::convert_error(i, err));
}
Err(err) => {
bail!("unable to parse {} - {}", what, err);
}
Ok((_, data)) => Ok(data),
}
}

View File

@ -5,8 +5,12 @@ use lazy_static::lazy_static;
use super::time::*; use super::time::*;
use crate::tools::nom::{
parse_complete, parse_u64, parse_error, IResult,
};
use nom::{ use nom::{
error::{context, ParseError, VerboseError}, error::{context},
bytes::complete::{tag, take_while1}, bytes::complete::{tag, take_while1},
combinator::{map_res, all_consuming, opt, recognize}, combinator::{map_res, all_consuming, opt, recognize},
sequence::{pair, preceded, tuple}, sequence::{pair, preceded, tuple},
@ -14,14 +18,6 @@ use nom::{
multi::separated_nonempty_list, multi::separated_nonempty_list,
}; };
type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>;
fn parse_error<'a>(i: &'a str, context: &'static str) -> nom::Err<VerboseError<&'a str>> {
let err = VerboseError { errors: Vec::new() };
let err = VerboseError::add_context(i, context, err);
nom::Err::Error(err)
}
lazy_static! { lazy_static! {
pub static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = { pub static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = {
let mut map = HashMap::new(); let mut map = HashMap::new();
@ -100,11 +96,7 @@ fn parse_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, u32> {
} }
} }
fn parse_u64(i: &str) -> IResult<&str, u64> { fn parse_weekday(i: &str) -> IResult<&str, WeekDays> {
map_res(recognize(digit1), str::parse)(i)
}
fn parse_weekday(i: &str) -> IResult<&str, WeekDays, VerboseError<&str>> {
let (i, text) = alpha1(i)?; let (i, text) = alpha1(i)?;
match text.to_ascii_lowercase().as_str() { match text.to_ascii_lowercase().as_str() {
@ -192,20 +184,7 @@ fn parse_time_spec(i: &str) -> IResult<&str, (Vec<DateTimeValue>, Vec<DateTimeVa
} }
pub fn parse_calendar_event(i: &str) -> Result<CalendarEvent, Error> { pub fn parse_calendar_event(i: &str) -> Result<CalendarEvent, Error> {
match all_consuming(parse_calendar_event_incomplete)(i) { parse_complete("calendar event", i, parse_calendar_event_incomplete)
Err(nom::Err::Error(VerboseError { errors })) |
Err(nom::Err::Failure(VerboseError { errors })) => {
if errors.is_empty() {
bail!("unable to parse calendar event");
} else {
bail!("unable to parse calendar event at '{}': {:?}", errors[0].0, errors[0].1);
}
}
Err(err) => {
bail!("unable to parse calendar event: {}", err);
}
Ok((_, ce)) => Ok(ce),
}
} }
fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> { fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> {