1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
//! Utilities.
#![allow(dead_code)]
use core::fmt;
use RawKind::*;
/// Raw kind (exclusive).
#[derive(Clone, Copy, PartialEq, Eq)]
enum RawKind {
/// Invalid string.
Invalid,
/// IRI.
Iri,
/// Absolute IRI.
IriAbsolute,
/// Relative IRI.
IriRelative,
/// URI.
Uri,
/// Absolute URI.
UriAbsolute,
/// Relative URI.
UriRelative,
}
impl RawKind {
fn spec_is(self, spec: Spec) -> bool {
match spec {
Spec::Uri => matches!(self, Self::Uri | Self::UriAbsolute | Self::UriRelative),
Spec::Iri => self != Self::Invalid,
}
}
fn kind_is(self, kind: Kind) -> bool {
match kind {
Kind::Absolute => matches!(self, Self::UriAbsolute | Self::IriAbsolute),
Kind::Normal => matches!(
self,
Self::UriAbsolute | Self::Uri | Self::IriAbsolute | Self::Iri
),
Kind::Reference => self != Self::Invalid,
Kind::Relative => matches!(self, Self::UriRelative | Self::IriRelative),
}
}
fn is(self, spec: Spec, kind: Kind) -> bool {
self.spec_is(spec) && self.kind_is(kind)
}
}
/// Strings.
/// ```
/// # use iri_string::types::IriReferenceStr;
/// // `<` and `>` cannot directly appear in an IRI reference.
/// assert!(IriReferenceStr::new("<not allowed>").is_err());
/// // Broken percent encoding cannot appear in an IRI reference.
/// assert!(IriReferenceStr::new("%").is_err());
/// assert!(IriReferenceStr::new("%GG").is_err());
/// ```
const STRINGS: &[(RawKind, &str)] = &[
(UriAbsolute, "https://user:pass@example.com:8080"),
(UriAbsolute, "https://example.com/"),
(UriAbsolute, "https://example.com/foo?bar=baz"),
(Uri, "https://example.com/foo?bar=baz#qux"),
(UriAbsolute, "foo:bar"),
(UriAbsolute, "foo:"),
(UriAbsolute, "foo:/"),
(UriAbsolute, "foo://"),
(UriAbsolute, "foo:///"),
(UriAbsolute, "foo:////"),
(UriAbsolute, "foo://///"),
(UriRelative, "foo"),
(UriRelative, "foo/bar"),
(UriRelative, "foo//bar"),
(UriRelative, "/"),
(UriRelative, "/foo"),
(UriRelative, "/foo/bar"),
(UriRelative, "//foo/bar"),
(UriRelative, "/foo//bar"),
(UriRelative, "?"),
(UriRelative, "???"),
(UriRelative, "?foo"),
(UriRelative, "#"),
(UriRelative, "#foo"),
(Invalid, "##"),
(Invalid, "fragment#cannot#have#hash#char"),
// `<` cannot appear in an IRI reference.
(Invalid, "<"),
// `>` cannot appear in an IRI reference.
(Invalid, ">"),
// `<` and `>` cannot appear in an IRI reference.
(Invalid, "lt<and-gt>not-allowed"),
// Incomplete percent encoding.
(Invalid, "%"),
(Invalid, "%0"),
(Invalid, "%f"),
(Invalid, "%F"),
// Invalid percent encoding.
(Invalid, "%0g"),
(Invalid, "%0G"),
(Invalid, "%GG"),
(Invalid, "%G0"),
];
/// Spec.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Spec {
/// URI.
Uri,
/// IRI and URI.
Iri,
}
/// Kind.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Kind {
/// Absolute IRI / URI.
Absolute,
/// IRI / URI.
Normal,
/// IRI / URI reference.
Reference,
/// Relative IRI / URI reference.
Relative,
}
pub fn positive(spec: Spec, kind: Kind) -> impl Iterator<Item = &'static str> {
STRINGS
.iter()
.filter(move |(raw_kind, _)| raw_kind.is(spec, kind))
.map(|(_, s)| *s)
}
pub fn negative(spec: Spec, kind: Kind) -> impl Iterator<Item = &'static str> {
STRINGS
.iter()
.filter(move |(raw_kind, _)| !raw_kind.is(spec, kind))
.map(|(_, s)| *s)
}
/// Returns true if the two equals after they are converted to strings.
pub(crate) fn eq_display_str<T>(d: &T, s: &str) -> bool
where
T: ?Sized + fmt::Display,
{
use core::fmt::Write as _;
/// Dummy writer to compare the formatted object to the given string.
struct CmpWriter<'a>(&'a str);
impl fmt::Write for CmpWriter<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
if self.0.len() < s.len() {
return Err(fmt::Error);
}
let (prefix, rest) = self.0.split_at(s.len());
self.0 = rest;
if prefix == s {
Ok(())
} else {
Err(fmt::Error)
}
}
}
let mut writer = CmpWriter(s);
let succeeded = write!(writer, "{}", d).is_ok();
succeeded && writer.0.is_empty()
}
#[allow(unused_macros)]
macro_rules! assert_eq_display {
($left:expr, $right:expr $(,)?) => {{
match (&$left, &$right) {
(left, right) => {
assert!(
utils::eq_display_str(left, right.as_ref()),
"`eq_str_display(left, right)`\n left: `{left}`,\n right: `{right}`",
);
#[cfg(feature = "alloc")]
{
let left = left.to_string();
let right = right.to_string();
assert_eq!(left, right);
}
}
}
}};
($left:expr, $right:expr, $($args:tt)*) => {{
match (&$left, &$right) {
(left, right) => {
assert!(
utils::eq_display_str(left, right.as_ref()),
"{}",
format_args!(
"{}: {}",
format_args!(
"`eq_str_display(left, right)`\n left: `{left}`,\n right: `{right}`",
),
format_args!($($args)*)
)
);
#[cfg(feature = "alloc")]
{
let left = left.to_string();
let right = right.to_string();
assert_eq!(left, right, $($args)*);
}
}
}
}};
}
|