Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context when encountering invalid separator #137

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ pub enum AttributeError {
}

/// An error that can occur when parsing RPSL text.
///
/// # Example
/// ```
/// # use rpsl::parse_object;
/// let rpsl = "\
/// role; ACME Company
///
/// ";
/// let err = parse_object(rpsl).unwrap_err();
/// let message = "\
/// parse error at line 1, column 5
/// |
/// 1 | role; ACME Company
/// | ^
/// invalid separator
/// expected `:`";
/// assert_eq!(err.to_string(), message);
/// ```
#[derive(Error, Debug)]
pub struct ParseError(String);

Expand Down
4 changes: 2 additions & 2 deletions src/parser/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{Object, ParseError};
/// ```
///
/// # Errors
/// Returns a `ParseError` if the input is not valid RPSL.
/// Returns a [`ParseError`] if the input is not valid RPSL.
///
/// # Examples
/// ```
Expand Down Expand Up @@ -134,7 +134,7 @@ pub fn parse_object(rpsl: &str) -> Result<Object, ParseError> {
/// Parse a WHOIS server response into [`Object`]s contained within.
///
/// # Errors
/// Returns a `ParseError` error if the input is not valid RPSL.
/// Returns a [`ParseError`] error if the input is not valid RPSL.
///
/// # Examples
/// ```
Expand Down
18 changes: 13 additions & 5 deletions src/parser/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use winnow::{
ascii::{newline, space0},
combinator::{alt, delimited, peek, preceded, repeat, separated_pair, terminated},
error::{ContextError, ParserError},
error::{AddContext, ContextError, ParserError, StrContext, StrContextValue},
token::{one_of, take_while},
Parser,
};
Expand All @@ -13,7 +13,7 @@ use crate::{Attribute, Name, Object, Value};
/// is textually represented as a list of attribute-value pairs that ends when a blank line is encountered.
pub fn object_block<'s, E>() -> impl Parser<&'s str, Object<'s>, E>
where
E: ParserError<&'s str>,
E: ParserError<&'s str> + AddContext<&'s str, StrContext>,
{
terminated(repeat(1.., attribute()), newline)
.with_taken()
Expand Down Expand Up @@ -60,10 +60,18 @@ where
// The attributes name and value are separated by a colon and optional spaces.
fn attribute<'s, E>() -> impl Parser<&'s str, Attribute<'s>, E>
where
E: ParserError<&'s str>,
E: ParserError<&'s str> + AddContext<&'s str, StrContext>,
{
separated_pair(attribute_name(), (':', space0), attribute_value())
.map(|(name, value)| Attribute::new(name, value))
separated_pair(
attribute_name(),
(
':'.context(StrContext::Label("separator"))
.context(StrContext::Expected(StrContextValue::StringLiteral(":"))),
space0,
),
attribute_value(),
)
.map(|(name, value)| Attribute::new(name, value))
}

/// Generate an attribute value parser that parses an ASCII sequence of letters,
Expand Down