Skip to content

Commit

Permalink
fix: parse ipath-abempty from RFC 3987
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Jul 23, 2024
1 parent a991527 commit b0bcc7b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/parser/link_url/parse_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,38 @@ fn get_correct_link(link: &str) -> Option<usize> {
None
}

fn parse_ipath_abempty(input: &str) -> IResult<&str, &str, CustomError<&str>> {
recognize(many0(tuple((char('/'), opt(take_while_ipchar1)))))(input)
}

#[test]
fn test_ipath_abempty() {
assert_eq!(
parse_ipath_abempty("///foo/bar").unwrap(),

Check failure on line 343 in src/parser/link_url/parse_link.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

error: used `unwrap()` on a `Result` value --> src/parser/link_url/parse_link.rs:343:9 | 343 | parse_ipath_abempty("///foo/bar").unwrap(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used note: the lint level is defined here --> src/lib.rs:19:5 | 19 | clippy::unwrap_used, | ^^^^^^^^^^^^^^^^^^^
("", "///foo/bar")
);
}

fn parse_ipath_absolute(input: &str) -> IResult<&str, &str, CustomError<&str>> {
recognize(tuple((
char('/'),
opt(tuple((
take_while_ipchar1,
many0(tuple((char('/'), opt(take_while_ipchar1)))),
))),
)))(input)
}

#[test]
fn test_ipath_absolute() {
assert_eq!(parse_ipath_absolute("/foo").unwrap(), ("", "/foo"));

Check failure on line 360 in src/parser/link_url/parse_link.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

error: used `unwrap()` on a `Result` value --> src/parser/link_url/parse_link.rs:360:16 | 360 | assert_eq!(parse_ipath_absolute("/foo").unwrap(), ("", "/foo")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
assert_eq!(parse_ipath_absolute("/foo/bar").unwrap(), ("", "/foo/bar"));

Check failure on line 361 in src/parser/link_url/parse_link.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

error: used `unwrap()` on a `Result` value --> src/parser/link_url/parse_link.rs:361:16 | 361 | assert_eq!(parse_ipath_absolute("/foo/bar").unwrap(), ("", "/foo/bar")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
assert_eq!(
parse_ipath_absolute("/foo//bar").unwrap(),

Check failure on line 363 in src/parser/link_url/parse_link.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

error: used `unwrap()` on a `Result` value --> src/parser/link_url/parse_link.rs:363:9 | 363 | parse_ipath_absolute("/foo//bar").unwrap(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
("", "/foo//bar")
);
}

// IRI links per RFC3987 and RFC3986
fn parse_iri(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {
let input_ = <&str>::clone(&input);
Expand All @@ -345,13 +377,8 @@ fn parse_iri(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {
// host is actually part of authority but we need it separately
// see iauthority function description for more information
let (input, path) = opt(alt((
recognize(tuple((
char('/'),
opt(tuple((
take_while_ipchar1,
many0(tuple((char('/'), opt(take_while_ipchar1)))),
))),
))), // ipath-absolute
parse_ipath_abempty,
parse_ipath_absolute,
recognize(tuple((
take_while_ipchar,
many0(tuple((char('/'), opt(take_while_ipchar1)))),
Expand Down
2 changes: 2 additions & 0 deletions tests/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ fn basic_parsing() {
"mailto:[email protected]?subject=hi&body=hello%20world",
"mailto:foö@ü.chat",
"ftp://test-test",
"https://www.openmandriva.org/en/news/article/openmandriva-rome-24-07-released",
"https://www.openmandriva.org///en/news/article/openmandriva-rome-24-07-released",
];

let test_cases_with_puny = vec!["https://ü.app#help", "http://münchen.de"];
Expand Down

0 comments on commit b0bcc7b

Please sign in to comment.