iri_string/parser/validate/
path.rs1use crate::parser::char;
4use crate::parser::str::{find_split2_hole, satisfy_chars_with_pct_encoded};
5use crate::spec::Spec;
6use crate::validate::{Error, ErrorKind};
7
8pub(super) fn validate_path_abempty<S: Spec>(i: &str) -> Result<(), Error> {
10 if i.is_empty() {
11 return Ok(());
12 }
13 let i = match i.strip_prefix('/') {
14 Some(rest) => rest,
15 None => return Err(Error::with_kind(ErrorKind::InvalidPath)),
16 };
17 let is_valid = satisfy_chars_with_pct_encoded(
18 i,
19 char::is_ascii_pchar_slash,
20 S::is_nonascii_char_unreserved,
21 );
22 if is_valid {
23 Ok(())
24 } else {
25 Err(Error::with_kind(ErrorKind::InvalidPath))
26 }
27}
28
29pub(super) fn validate_path_absolute_authority_absent<S: Spec>(i: &str) -> Result<(), Error> {
32 if i.is_empty() {
33 return Ok(());
35 }
36 if i.starts_with("//") {
37 unreachable!("this case should be handled by the caller");
38 }
39 let is_valid = satisfy_chars_with_pct_encoded(
40 i,
41 char::is_ascii_pchar_slash,
42 S::is_nonascii_char_unreserved,
43 );
44 if is_valid {
45 Ok(())
46 } else {
47 Err(Error::with_kind(ErrorKind::InvalidPath))
48 }
49}
50
51pub(super) fn validate_path_relative_authority_absent<S: Spec>(i: &str) -> Result<(), Error> {
54 if i.starts_with("//") {
55 unreachable!("this case should be handled by the caller");
56 }
57 let is_valid = match find_split2_hole(i, b'/', b':') {
58 Some((_, b'/', _)) | None => satisfy_chars_with_pct_encoded(
59 i,
60 char::is_ascii_pchar_slash,
61 S::is_nonascii_char_unreserved,
62 ),
63 Some((_, c, _)) => {
64 debug_assert_eq!(c, b':');
65 return Err(Error::with_kind(ErrorKind::InvalidPath));
67 }
68 };
69 if is_valid {
70 Ok(())
71 } else {
72 Err(Error::with_kind(ErrorKind::InvalidPath))
73 }
74}
75
76pub(crate) fn validate_path<S: Spec>(i: &str) -> Result<(), Error> {
78 let is_valid = satisfy_chars_with_pct_encoded(
79 i,
80 char::is_ascii_pchar_slash,
81 S::is_nonascii_char_unreserved,
82 );
83 if is_valid {
84 Ok(())
85 } else {
86 Err(Error::with_kind(ErrorKind::InvalidPath))
87 }
88}
89
90pub(crate) fn validate_path_segment<S: Spec>(i: &str) -> Result<(), Error> {
92 let is_valid =
93 satisfy_chars_with_pct_encoded(i, char::is_ascii_pchar, S::is_nonascii_char_unreserved);
94 if is_valid {
95 Ok(())
96 } else {
97 Err(Error::with_kind(ErrorKind::InvalidPath))
98 }
99}