Add typedef to abstract away custom parsing error type

This commit is contained in:
Oliver Hamlet
2018-10-07 10:45:24 +01:00
parent 10d078b843
commit d28c194c9d
2 changed files with 15 additions and 12 deletions
+10 -9
View File
@@ -1,13 +1,14 @@
use std::path::{Component, Path, PathBuf}; use std::path::{Component, Path, PathBuf};
use std::str; use std::str;
use nom::{hex_digit, Context, Err, ErrorKind, IResult}; use nom::{hex_digit, Context, Err, ErrorKind};
use regex::{Regex, RegexBuilder}; use regex::{Regex, RegexBuilder};
use ParsingResult;
use super::{ComparisonOperator, Function}; use super::{ComparisonOperator, Function};
impl ComparisonOperator { impl ComparisonOperator {
pub fn parse(input: &str) -> IResult<&str, ComparisonOperator> { pub fn parse(input: &str) -> ParsingResult<ComparisonOperator> {
do_parse!( do_parse!(
input, input,
operator: operator:
@@ -45,7 +46,7 @@ fn is_in_game_path(path: &Path) -> bool {
true true
} }
fn parse_regex(input: &str) -> IResult<&str, Regex> { fn parse_regex(input: &str) -> ParsingResult<Regex> {
RegexBuilder::new(input) RegexBuilder::new(input)
.case_insensitive(true) .case_insensitive(true)
.build() .build()
@@ -53,7 +54,7 @@ fn parse_regex(input: &str) -> IResult<&str, Regex> {
.map_err(|_| Err::Failure(Context::Code(input, PARSE_REGEX_ERROR))) .map_err(|_| Err::Failure(Context::Code(input, PARSE_REGEX_ERROR)))
} }
fn parse_version_args(input: &str) -> IResult<&str, (PathBuf, &str, ComparisonOperator)> { fn parse_version_args(input: &str) -> ParsingResult<(PathBuf, &str, ComparisonOperator)> {
let (remaining_input, (path, version, comparator)) = try_parse!( let (remaining_input, (path, version, comparator)) = try_parse!(
input, input,
do_parse!( do_parse!(
@@ -77,13 +78,13 @@ fn parse_version_args(input: &str) -> IResult<&str, (PathBuf, &str, ComparisonOp
} }
} }
fn parse_crc(input: &str) -> IResult<&str, u32> { fn parse_crc(input: &str) -> ParsingResult<u32> {
u32::from_str_radix(input, 16) u32::from_str_radix(input, 16)
.map(|c| ("", c)) .map(|c| ("", c))
.map_err(|_| Err::Failure(Context::Code(input, PARSE_CRC_ERROR))) .map_err(|_| Err::Failure(Context::Code(input, PARSE_CRC_ERROR)))
} }
fn parse_checksum_args(input: &str) -> IResult<&str, (PathBuf, u32)> { fn parse_checksum_args(input: &str) -> ParsingResult<(PathBuf, u32)> {
let (remaining_input, (path, crc)) = try_parse!( let (remaining_input, (path, crc)) = try_parse!(
input, input,
do_parse!( do_parse!(
@@ -103,7 +104,7 @@ fn parse_checksum_args(input: &str) -> IResult<&str, (PathBuf, u32)> {
} }
} }
fn parse_path(input: &str) -> IResult<&str, PathBuf> { fn parse_path(input: &str) -> ParsingResult<PathBuf> {
let (remaining_input, path) = let (remaining_input, path) =
try_parse!(input, map!(is_not!(INVALID_PATH_CHARS), PathBuf::from)); try_parse!(input, map!(is_not!(INVALID_PATH_CHARS), PathBuf::from));
@@ -116,7 +117,7 @@ fn parse_path(input: &str) -> IResult<&str, PathBuf> {
/// Parse a string that is a path where the last component is a regex string /// Parse a string that is a path where the last component is a regex string
/// that may contain characters that are invalid in paths but valid in regex. /// that may contain characters that are invalid in paths but valid in regex.
fn parse_regex_path(input: &str) -> IResult<&str, (PathBuf, Regex)> { fn parse_regex_path(input: &str) -> ParsingResult<(PathBuf, Regex)> {
let (remaining_input, string) = try_parse!(input, is_not!(INVALID_REGEX_PATH_CHARS)); let (remaining_input, string) = try_parse!(input, is_not!(INVALID_REGEX_PATH_CHARS));
if string.ends_with('/') { if string.ends_with('/') {
@@ -140,7 +141,7 @@ fn parse_regex_path(input: &str) -> IResult<&str, (PathBuf, Regex)> {
} }
impl Function { impl Function {
pub fn parse(input: &str) -> IResult<&str, Function> { pub fn parse(input: &str) -> ParsingResult<Function> {
do_parse!( do_parse!(
input, input,
function: function:
+5 -3
View File
@@ -73,6 +73,8 @@ impl error::Error for Error {
} }
} }
type ParsingResult<'a, T> = IResult<&'a str, T, u32>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GameType { pub enum GameType {
Tes4, Tes4,
@@ -181,7 +183,7 @@ impl str::FromStr for Expression {
} }
} }
fn parse_expression(input: &str) -> IResult<&str, Expression> { fn parse_expression(input: &str) -> ParsingResult<Expression> {
do_parse!( do_parse!(
input, input,
compound_conditions: separated_list_complete!(ws!(tag!("or")), CompoundCondition::parse) compound_conditions: separated_list_complete!(ws!(tag!("or")), CompoundCondition::parse)
@@ -210,7 +212,7 @@ impl CompoundCondition {
Ok(true) Ok(true)
} }
fn parse(input: &str) -> IResult<&str, CompoundCondition> { fn parse(input: &str) -> ParsingResult<CompoundCondition> {
do_parse!( do_parse!(
input, input,
conditions: separated_list_complete!(ws!(tag!("and")), Condition::parse) conditions: separated_list_complete!(ws!(tag!("and")), Condition::parse)
@@ -242,7 +244,7 @@ impl Condition {
} }
} }
fn parse(input: &str) -> IResult<&str, Condition> { fn parse(input: &str) -> ParsingResult<Condition> {
do_parse!( do_parse!(
input, input,
condition: condition: