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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
//! IRI reference. use std::{convert::TryFrom, fmt}; #[cfg(feature = "serde")] use serde::{ de::{self, Visitor}, Deserialize, Deserializer, Serialize, }; use crate::{ resolve::resolve_iri, types::{ iri::set_fragment, AbsoluteIriStr, CreationError, IriFragmentStr, IriStr, IriString, RelativeIriStr, RelativeIriString, }, validate::iri::{iri as validate_iri, iri_reference, Error}, }; custom_slice_macros::define_slice_types_pair! { /// An owned string of an IRI reference. /// /// This corresponds to `IRI-reference` rule in RFC 3987. /// This is `IRI / irelative-ref` /// In other words, this is union of `IriString` and `RelativeIriString. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize))] #[cfg_attr(feature = "serde", serde(transparent))] #[custom_slice(owned)] #[custom_slice(derive( AsRefSlice, AsRefSliceInner, Deref, IntoInner, PartialEqBulk, PartialEqInnerBulk, PartialOrdBulk, PartialOrdInnerBulk, TryFromInner, ))] #[custom_slice(error(type = "CreationError<String>", map = "{|e, v| CreationError::new(e, v)}"))] #[custom_slice(new_unchecked = " /// Creates a new `IriReferenceString` without validation. pub(crate) unsafe fn new_always_unchecked ")] pub struct IriReferenceString(String); /// A borrowed slice of an IRI reference. /// /// This corresponds to `IRI-reference` rule in RFC 3987. /// This is `IRI / irelative-ref` /// In other words, this is union of `IriStr` and `RelativeIriStr. #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] #[allow(clippy::derive_hash_xor_eq)] #[cfg_attr(feature = "serde", derive(Serialize))] #[cfg_attr(feature = "serde", serde(transparent))] #[custom_slice(slice)] #[custom_slice(derive( AsRefSlice, AsRefSliceInner, DefaultRef, Deref, PartialEqBulk, PartialEqInnerBulk, PartialOrdBulk, PartialOrdInnerBulk, IntoArc, IntoBox, IntoRc, TryFromInner, ))] #[custom_slice(error(type = "Error"))] #[custom_slice(new_unchecked = " /// Creates a new `&IriReferenceStr` without validation. pub(crate) unsafe fn new_always_unchecked ")] pub struct IriReferenceStr(str); /// Validates the given string as an IRI reference. #[custom_slice(validator)] fn validate(s: &str) -> Result<(), Error> { iri_reference(s) } } impl IriReferenceString { /// Creates a new `IriReferenceString` maybe without validation. /// /// This does validation on debug build. pub(crate) unsafe fn new_unchecked(s: String) -> Self { debug_assert_eq!(validate(&s), Ok(())); Self::new_always_unchecked(s) } /// Returns the string as `IriString`, if it is valid as an IRI. /// /// If it is not an IRI, then `RelativeIriString` is returned as `Err(_)`. pub fn into_iri(self) -> Result<IriString, RelativeIriString> { let s: String = self.into(); // Check with `IRI` rule first, because of the syntax. // // > Some productions are ambiguous. The "first-match-wins" (a.k.a. // > "greedy") algorithm applies. For details, see [RFC3986]. // > // > --- <https://tools.ietf.org/html/rfc3987#section-2.2>. if validate_iri(&s).is_ok() { Ok(unsafe { // This is safe because `s` is already validated by condition // of `if`. IriString::new_always_unchecked(s) }) } else { Err(unsafe { // This is safe because of the syntax rule // `IRI-reference = IRI / irelative-ref`. // It says that if an IRI reference is not an IRI, then it is // a relative IRI. RelativeIriString::new_unchecked(s) }) } } /// Returns the string as `RelativeIriString`, if it is valid as an IRI. /// /// If it is not an IRI, then `IriString` is returned as `Err(_)`. pub fn into_relative_iri(self) -> Result<RelativeIriString, IriString> { match self.into_iri() { Ok(iri) => Err(iri), Err(relative) => Ok(relative), } } /// Sets the fragment part to the given string. /// /// Removes fragment part (and following `#` character) if `None` is given. pub fn set_fragment(&mut self, fragment: Option<&IriFragmentStr>) { set_fragment(&mut self.0, fragment.map(AsRef::as_ref)); debug_assert!(iri_reference(&self.0).is_ok()); } /// Shrinks the capacity of the inner buffer to match its length. pub fn shrink_to_fit(&mut self) { self.0.shrink_to_fit() } } impl IriReferenceStr { /// Creates a new `&IriReferenceStr` maybe without validation. /// /// This does validation on debug build. pub(crate) unsafe fn new_unchecked(s: &str) -> &Self { debug_assert_eq!(validate(s), Ok(())); Self::new_always_unchecked(s) } /// Returns the string as `&IriStr`, if it is valid as an IRI. /// /// If it is not an IRI, then `&RelativeIriStr` is returned as `Err(_)`. pub fn to_iri(&self) -> Result<&IriStr, &RelativeIriStr> { // Check with `IRI` rule first, because of the syntax. // // > Some productions are ambiguous. The "first-match-wins" (a.k.a. // > "greedy") algorithm applies. For details, see [RFC3986]. // > // > --- <https://tools.ietf.org/html/rfc3987#section-2.2>. <&IriStr>::try_from(self.as_str()).map_err(|_| unsafe { // This is safe because of the syntax rule // `IRI-reference = IRI / irelative-ref`. // It says that if an IRI reference is not an IRI, then it is // a relative IRI. RelativeIriStr::new_unchecked(self) }) } /// Returns the string as `&RelativeIriStr`, if it is valid as an IRI. /// /// If it is not an IRI, then `&IriStr` is returned as `Err(_)`. pub fn to_relative_iri(&self) -> Result<&RelativeIriStr, &IriStr> { match self.to_iri() { Ok(iri) => Err(iri), Err(relative) => Ok(relative), } } /// Returns resolved IRI using strict resolver. /// /// About reference resolution output example, see [RFC 3986 section /// 5.4](https://tools.ietf.org/html/rfc3986#section-5.4). /// /// About resolver strictness, see [RFC 3986 section /// 5.4.2](https://tools.ietf.org/html/rfc3986#section-5.4.2): /// /// > Some parsers allow the scheme name to be present in a relative /// > reference if it is the same as the base URI scheme. This is considered /// > to be a loophole in prior specifications of partial URI /// > [RFC1630](https://tools.ietf.org/html/rfc1630). Its use should be /// avoided but is allowed for backward compatibility. /// > /// > --- <https://tools.ietf.org/html/rfc3986#section-5.4.2> /// /// Usual users will want to use strict resolver. pub fn resolve(&self, base: &AbsoluteIriStr) -> IriString { resolve_iri(self, base, true) } /// Returns the fragment part if exists. /// /// A leading `#` character is truncated if the fragment part exists. /// /// # Examples /// /// ``` /// use std::convert::TryFrom; /// # use iri_string::{types::{IriFragmentStr, IriReferenceStr}, validate::iri::Error}; /// let iri = <&IriReferenceStr>::try_from("foo://bar/baz?qux=quux#corge")?; /// let fragment = <&IriFragmentStr>::try_from("corge")?; /// assert_eq!(iri.fragment(), Some(fragment)); /// # Ok::<_, Error>(()) /// ``` /// /// ``` /// use std::convert::TryFrom; /// # use iri_string::{types::{IriFragmentStr, IriReferenceStr}, validate::iri::Error}; /// let iri = <&IriReferenceStr>::try_from("foo://bar/baz?qux=quux#")?; /// let fragment = <&IriFragmentStr>::try_from("")?; /// assert_eq!(iri.fragment(), Some(fragment)); /// # Ok::<_, Error>(()) /// ``` /// /// ``` /// use std::convert::TryFrom; /// # use iri_string::{types::IriReferenceStr, validate::iri::Error}; /// let iri = <&IriReferenceStr>::try_from("foo://bar/baz?qux=quux")?; /// assert_eq!(iri.fragment(), None); /// # Ok::<_, Error>(()) /// ``` /// /// ``` /// use std::convert::TryFrom; /// # use iri_string::{types::{IriFragmentStr, IriReferenceStr}, validate::iri::Error}; /// let iri = <&IriReferenceStr>::try_from("#foo")?; /// let fragment = <&IriFragmentStr>::try_from("foo")?; /// assert_eq!(iri.fragment(), Some(fragment)); /// # Ok::<_, Error>(()) /// ``` /// /// ``` /// use std::convert::TryFrom; /// # use iri_string::{types::IriReferenceStr, validate::iri::Error}; /// let iri = <&IriReferenceStr>::try_from("")?; /// assert_eq!(iri.fragment(), None); /// # Ok::<_, Error>(()) /// ``` pub fn fragment(&self) -> Option<&IriFragmentStr> { let s: &str = self.as_ref(); s.find('#').map(|colon_pos| unsafe { // This is safe because the fragment part of the valid // `IriReferenceStr` is guaranteed to be a valid fragment. IriFragmentStr::new_unchecked(&s[(colon_pos + 1)..]) }) } /// Returns `&str`. pub fn as_str(&self) -> &str { self.as_ref() } } impl fmt::Display for IriReferenceString { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { AsRef::<IriReferenceStr>::as_ref(self).fmt(f) } } impl fmt::Display for &IriReferenceStr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.as_str()) } } impl std::str::FromStr for IriReferenceString { type Err = Error; fn from_str(s: &str) -> Result<Self, Self::Err> { <&IriReferenceStr>::try_from(s).map(ToOwned::to_owned) } } /// `IriReferenceString` visitor. #[cfg(feature = "serde")] #[derive(Debug, Clone, Copy)] struct IriReferenceStringVisitor; #[cfg(feature = "serde")] impl<'de> Visitor<'de> for IriReferenceStringVisitor { type Value = IriReferenceString; fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("an IRI reference") } fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: de::Error, { <&IriReferenceStr>::try_from(v) .map(ToOwned::to_owned) .map_err(E::custom) } fn visit_string<E>(self, v: String) -> Result<Self::Value, E> where E: de::Error, { IriReferenceString::try_from(v).map_err(E::custom) } } #[cfg(feature = "serde")] impl<'de> Deserialize<'de> for IriReferenceString { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { deserializer.deserialize_str(IriReferenceStringVisitor) } } /// `IriReferenceStr` visitor. #[cfg(feature = "serde")] #[derive(Debug, Clone, Copy)] struct IriReferenceStrVisitor; #[cfg(feature = "serde")] impl<'de> Visitor<'de> for IriReferenceStrVisitor { type Value = &'de IriReferenceStr; fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("an IRI reference") } fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> where E: de::Error, { <&'de IriReferenceStr>::try_from(v).map_err(E::custom) } } #[cfg(feature = "serde")] impl<'de: 'a, 'a> Deserialize<'de> for &'a IriReferenceStr { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { deserializer.deserialize_string(IriReferenceStrVisitor) } }