-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use functions returning parser instead of functions implicitly implem…
…enting parser (#116) Allows for easier modification of parser behavior at runtime which might be implemented in the future.
- Loading branch information
Showing
5 changed files
with
401 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,12 @@ | ||
use winnow::{ | ||
ascii::{multispace0, newline}, | ||
combinator::{alt, delimited, repeat, terminated}, | ||
PResult, Parser, | ||
ascii::multispace0, | ||
combinator::{delimited, repeat}, | ||
Parser, | ||
}; | ||
|
||
use super::component; | ||
use super::core::{object_block, object_block_padded}; | ||
use crate::{Object, ParseError}; | ||
|
||
/// Parse an object with at least one attribute terminated by a newline. | ||
/// | ||
/// As per [RFC 2622](https://datatracker.ietf.org/doc/html/rfc2622#section-2), an RPSL object | ||
/// is textually represented as a list of attribute-value pairs that ends when a blank line is encountered. | ||
fn object_block<'s>(input: &mut &'s str) -> PResult<Object<'s>> { | ||
let (attributes, source) = terminated(repeat(1.., component::attribute), newline) | ||
.with_taken() | ||
.parse_next(input)?; | ||
Ok(Object::from_parsed(source, attributes)) | ||
} | ||
|
||
/// Extends the object block parser to consume optional padding server messages or newlines. | ||
fn object_block_padded<'s>(input: &mut &'s str) -> PResult<Object<'s>> { | ||
delimited( | ||
consume_opt_message_or_newlines, | ||
object_block, | ||
consume_opt_message_or_newlines, | ||
) | ||
.parse_next(input) | ||
} | ||
|
||
/// Consume optional server messages or newlines. | ||
fn consume_opt_message_or_newlines(input: &mut &str) -> PResult<()> { | ||
repeat(0.., alt((newline.void(), component::server_message.void()))).parse_next(input) | ||
} | ||
|
||
/// Parse RPSL into an [`Object`], borrowing from the source. | ||
/// | ||
/// ```text | ||
|
@@ -152,7 +126,8 @@ fn consume_opt_message_or_newlines(input: &mut &str) -> PResult<()> { | |
/// # } | ||
/// ``` | ||
pub fn parse_object(rpsl: &str) -> Result<Object, ParseError> { | ||
let object = delimited(multispace0, object_block, multispace0).parse(rpsl)?; | ||
let block_parser = object_block(); | ||
let object = delimited(multispace0, block_parser, multispace0).parse(rpsl)?; | ||
Ok(object) | ||
} | ||
|
||
|
@@ -217,82 +192,7 @@ pub fn parse_object(rpsl: &str) -> Result<Object, ParseError> { | |
/// # Ok(()) | ||
/// # } | ||
pub fn parse_whois_response(response: &str) -> Result<Vec<Object>, ParseError> { | ||
let objects = repeat(1.., object_block_padded).parse(response)?; | ||
let block_parser = object_block_padded(object_block()); | ||
let objects = repeat(1.., block_parser).parse(response)?; | ||
Ok(objects) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use rstest::*; | ||
|
||
use super::*; | ||
use crate::{Attribute, Object}; | ||
|
||
#[test] | ||
fn object_block_valid() { | ||
let object = &mut concat!( | ||
"email: [email protected]\n", | ||
"nic-hdl: RPSL1-RIPE\n", | ||
"\n" | ||
); | ||
assert_eq!( | ||
object_block(object), | ||
Ok(Object::from_parsed( | ||
object, | ||
vec![ | ||
Attribute::unchecked_single("email", "[email protected]"), | ||
Attribute::unchecked_single("nic-hdl", "RPSL1-RIPE") | ||
] | ||
)) | ||
); | ||
} | ||
|
||
#[test] | ||
/// When parsing RPSL, the resulting object contains the original source it was created from. | ||
fn parsed_object_contains_source() { | ||
let rpsl = &mut concat!( | ||
"email: [email protected]\n", | ||
"nic-hdl: RPSL1-RIPE\n", | ||
"\n" | ||
); | ||
let source = *rpsl; | ||
let object = object_block(rpsl).unwrap(); | ||
assert_eq!(object.source().unwrap(), source); | ||
} | ||
|
||
#[test] | ||
fn object_block_without_newline_termination_is_err() { | ||
let object = &mut concat!( | ||
"email: [email protected]\n", | ||
"nic-hdl: RPSL1-RIPE\n", | ||
); | ||
assert!(object_block(object).is_err()); | ||
} | ||
|
||
#[rstest] | ||
#[case( | ||
&mut "% Note: This is a server message\n" | ||
)] | ||
#[case( | ||
&mut concat!( | ||
"\n", | ||
"% Note: This is a server message followed by an empty line\n" | ||
) | ||
)] | ||
#[case( | ||
&mut concat!( | ||
"% Note: This is a server message preceding some newlines.\n", | ||
"\n", | ||
"\n", | ||
) | ||
)] | ||
fn optional_comment_or_newlines_consumed(#[case] given: &mut &str) { | ||
consume_opt_message_or_newlines(given).unwrap(); | ||
assert_eq!(*given, ""); | ||
} | ||
|
||
#[test] | ||
fn optional_comment_or_newlines_optional() { | ||
assert_eq!(consume_opt_message_or_newlines(&mut ""), Ok(())); | ||
} | ||
} |
Oops, something went wrong.