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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
//! [`URIQualifiedName`]. //! //! [`URIQualifiedName`]: //! https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-URIQualifiedName use core::convert::TryFrom; use core::fmt; use core::num::NonZeroUsize; use crate::names::error::{NameError, TargetNameType}; use crate::names::Ncname; /// String slice for [`URIQualifiedName`]. /// /// [`URIQualifiedName`]: /// https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-URIQualifiedName #[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pub struct UriQualifiedName(str); impl UriQualifiedName { /// Creates a new `&UriQualifiedName`. /// /// [`URIQualifiedName`] has `Q{uri}ncname` format. /// `UriQualifiedName` type validates NCName part, but does not validate URI part. /// /// > In most contexts, processors are not required to raise errors if a URI /// > is not lexically valid according to [RFC3986] and [RFC3987]. /// > See [2.4.5 URI Literals][XPATH31-2.4.5] for details. /// > /// > --- [XML Path Language (XPath) 3.1, 2 Basics][XPATH31-2] /// /// > XPath 3.1 requires a statically known, valid URI in a BracedURILiteral. /// > An implementation may raise a static error err:XQST0046 if the value /// > of a Braced URI Literal is of nonzero length and is neither an /// > absolute URI nor a relative URI. /// > /// > --- [XML Path Language (XPath) 3.1, 2.4.5 URI Literals][XPATH31-2.4.5] /// /// It is user's responsibility to validate URI part if necessary. /// /// # Failures /// /// Fails if the given string is not a valid [`UriQualifiedName`]. /// /// # Examples /// /// ``` /// # use xml_string::names::UriQualifiedName; /// let name = UriQualifiedName::from_str("Q{http://example.com/}name")?; /// assert_eq!(name, "Q{http://example.com/}name"); /// /// assert_eq!( /// UriQualifiedName::from_str("Q{}name")?, /// "Q{}name", /// "Empty URI is OK" /// ); /// assert_eq!( /// UriQualifiedName::from_str("Q{foo}bar")?, /// "Q{foo}bar", /// "URI is not validated" /// ); /// /// assert!( /// UriQualifiedName::from_str("foo").is_err(), /// "URIQualifiedName has `Q{{uri}}ncname` format" /// ); /// assert!( /// UriQualifiedName::from_str("Q{http://example.com}foo:bar").is_err(), /// "Colon is not allowed" /// ); /// assert!( /// UriQualifiedName::from_str("Q{foo{bar}qux").is_err(), /// "URI part cannot have `{{` and `}}`" /// ); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` /// /// [`URIQualifiedName`]: /// https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-URIQualifiedName /// [RFC3986]: https://tools.ietf.org/html/rfc3986 /// [RFC3987]: https://tools.ietf.org/html/rfc3987 /// [XPATH31-2]: https://www.w3.org/TR/2017/REC-xpath-31-20170321/#id-basics /// [XPATH31-2.4.5]: https://www.w3.org/TR/2017/REC-xpath-31-20170321/#id-uri-literals // `FromStr` can be implemented only for types with static lifetime. #[allow(clippy::should_implement_trait)] pub fn from_str(s: &str) -> Result<&Self, NameError> { <&Self>::try_from(s) } /// Creates a new `&UriQualifiedName` without validation. /// /// # Safety /// /// The given string should be a valid [`UriQualifiedName`]. /// /// # Examples /// /// ``` /// # use xml_string::names::UriQualifiedName; /// let name = unsafe { /// UriQualifiedName::new_unchecked("Q{foo}bar") /// }; /// assert_eq!(name, "Q{foo}bar"); /// ``` /// /// [`URIQualifiedName`]: /// https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-URIQualifiedName #[inline] #[must_use] pub unsafe fn new_unchecked(s: &str) -> &Self { &*(s as *const str as *const Self) } /// Validates the given string. fn validate(s: &str) -> Result<(), NameError> { match Self::parse_as_possible(s) { Ok(_) => Ok(()), Err(e) => Err(NameError::new( TargetNameType::UriQualifiedName, e.map_or(0, |(_local_name_start, valid_up_to)| valid_up_to.get()), )), } } /// Parses the given string from the beginning as possible. /// /// Retruns `Ok(local_name_start)` if the string is valid QName. /// Returns `Err(None)` if the string is completely invalid. /// Returns `Err(Some((local_name_start, valid_up_to)))` if the string is invalid /// but has valid substring as the prefix. fn parse_as_possible(s: &str) -> Result<NonZeroUsize, Option<(NonZeroUsize, NonZeroUsize)>> { if !s.starts_with("Q{") { return Err(None); } let uri_and_rest = &s[2..]; let uri_len = match uri_and_rest.find(|c| c == '{' || c == '}') { Some(pos) if uri_and_rest.as_bytes()[pos] == b'}' => pos, _ => return Err(None), }; let local_name_start = NonZeroUsize::new(uri_len + 3).expect("Should never be zero"); let local_name = &s[local_name_start.get()..]; match Ncname::from_str(local_name) { Ok(_) => Ok(local_name_start), Err(e) if e.valid_up_to() == 0 => Err(None), Err(e) => Err(Some(( local_name_start, NonZeroUsize::new(local_name_start.get() + e.valid_up_to()) .expect("Should never be zero"), ))), } } /// Returns the string as `&str`. /// /// # Examples /// /// ``` /// # use xml_string::names::UriQualifiedName; /// let name = UriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name, "Q{foo}bar"); /// /// let s: &str = name.as_str(); /// assert_eq!(s, "Q{foo}bar"); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[inline] #[must_use] pub fn as_str(&self) -> &str { &self.0 } /// Parses the leading `UriQualifiedName` and returns the value and the rest input. /// /// # Exmaples /// /// ``` /// # use xml_string::names::UriQualifiedName; /// let input = "Q{foo}bar:012"; /// let expected = UriQualifiedName::from_str("Q{foo}bar") /// .expect("valid UriQualifiedName"); /// assert_eq!( /// UriQualifiedName::parse_next(input), /// Ok((expected, ":012")) /// ); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` /// /// ``` /// # use xml_string::names::UriQualifiedName; /// let input = "012"; /// assert!(UriQualifiedName::parse_next(input).is_err()); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` pub fn parse_next(s: &str) -> Result<(&Self, &str), NameError> { match Self::from_str(s) { Ok(v) => Ok((v, &s[s.len()..])), Err(e) if e.valid_up_to() == 0 => Err(e), Err(e) => { let valid_up_to = e.valid_up_to(); let v = unsafe { let valid = &s[..valid_up_to]; debug_assert!(Self::validate(valid).is_ok()); // This is safe because the substring is valid. Self::new_unchecked(valid) }; Ok((v, &s[valid_up_to..])) } } } /// Returns the position where the local name starts. /// /// Note that this is O(length) operation. #[must_use] fn local_name_start(&self) -> NonZeroUsize { // Find `[2..]` since the first two characters are `Q{`. let pos = self.as_str()[2..] .find('}') .expect("Should never fail: Valid URIQualifiedName has `}` character") + 3; NonZeroUsize::new(pos) .expect("Should never fail: URIQualifiedName cannot start with the local name") } /// Returns the URI. /// /// Note that this is O(length) operation. /// Consider using [`ParsedUriQualifiedName::uri`] method if possible. /// /// # Examples /// /// ``` /// # use xml_string::names::UriQualifiedName; /// let name = UriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name.uri(), "foo"); /// /// let empty_uri = UriQualifiedName::from_str("Q{}foo")?; /// assert_eq!(empty_uri.uri(), ""); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[inline] #[must_use] pub fn uri(&self) -> &str { ParsedUriQualifiedName::new(self, self.local_name_start()).uri() } /// Returns the local name. /// /// Note that this is O(length) operation. /// Consider using [`ParsedUriQualifiedName::local_name`] method if possible. /// /// # Examples /// /// ``` /// # use xml_string::names::UriQualifiedName; /// let name = UriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name.local_name(), "bar"); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[inline] #[must_use] pub fn local_name(&self) -> &Ncname { ParsedUriQualifiedName::new(self, self.local_name_start()).local_name() } /// Returns a pair of the uri and the local name. /// /// Note that this is O(length) operation. /// Consider using [`ParsedUriQualifiedName::uri_and_local`] method if possible. /// /// # Examples /// /// ``` /// # use xml_string::names::UriQualifiedName; /// use std::convert::TryFrom; /// /// let name = UriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name.uri_and_local(), (name.uri(), name.local_name())); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[inline] #[must_use] pub fn uri_and_local(&self) -> (&str, &Ncname) { ParsedUriQualifiedName::new(self, self.local_name_start()).uri_and_local() } /// Converts a `Box<UriQualifiedName>` into a `Box<str>` without copying or allocating. /// /// # Examples /// /// ``` /// # use xml_string::names::UriQualifiedName; /// let name = UriQualifiedName::from_str("Q{foo}bar")?; /// let boxed_name: Box<UriQualifiedName> = name.into(); /// assert_eq!(&*boxed_name, name); /// let boxed_str: Box<str> = boxed_name.into_boxed_str(); /// assert_eq!(&*boxed_str, name.as_str()); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[cfg(feature = "alloc")] pub fn into_boxed_str(self: alloc::boxed::Box<Self>) -> Box<str> { unsafe { // This is safe because `UriQualifiedName` has the same memory layout as `str` // (thanks to `#[repr(transparent)]`). alloc::boxed::Box::<str>::from_raw(alloc::boxed::Box::<Self>::into_raw(self) as *mut str) } } } impl_traits_for_custom_string_slice!(UriQualifiedName); impl<'a> From<ParsedUriQualifiedName<'a>> for &'a UriQualifiedName { #[inline] fn from(s: ParsedUriQualifiedName<'a>) -> Self { s.content } } impl<'a> TryFrom<&'a str> for &'a UriQualifiedName { type Error = NameError; fn try_from(s: &'a str) -> Result<Self, Self::Error> { UriQualifiedName::validate(s)?; Ok(unsafe { // This is safe because the string is validated. UriQualifiedName::new_unchecked(s) }) } } /// Parsed [`URIQualifiedName`] reference. /// /// [`URIQualifiedName`]: /// https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-URIQualifiedName #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ParsedUriQualifiedName<'a> { /// Content string. content: &'a UriQualifiedName, /// Start position of the local name. local_name_start: NonZeroUsize, } impl<'a> ParsedUriQualifiedName<'a> { /// Creates a new `ParsedUriQualifiedName`. /// /// # Panics /// /// Panics if the `local_name_start` does not point to the start position of /// the local name. #[must_use] fn new(content: &'a UriQualifiedName, local_name_start: NonZeroUsize) -> Self { if content.as_str().as_bytes()[local_name_start.get() - 1] != b'}' { panic!( "`local_name_pos` (={:?}) should point to the next position of the `}}` character in the URIQualifiedName {:?}", local_name_start.get(), content ); } Self { content, local_name_start, } } /// Creates a new `ParsedUriQualifiedName<'_>` from the given string slice. /// /// # Failures /// /// Fails if the given string is not a valid [`URIQualifiedName`]. /// /// # Examples /// /// ``` /// # use xml_string::names::ParsedUriQualifiedName; /// let name = ParsedUriQualifiedName::from_str("Q{http://example.com/}name")?; /// assert_eq!(name, "Q{http://example.com/}name"); /// /// assert_eq!( /// ParsedUriQualifiedName::from_str("Q{}name")?, /// "Q{}name", /// "Empty URI is OK" /// ); /// assert_eq!( /// ParsedUriQualifiedName::from_str("Q{foo}bar")?, /// "Q{foo}bar", /// "URI is not validated" /// ); /// /// assert!( /// ParsedUriQualifiedName::from_str("foo").is_err(), /// "URIQualifiedName has `Q{{uri}}ncname` format" /// ); /// assert!( /// ParsedUriQualifiedName::from_str("Q{http://example.com}foo:bar").is_err(), /// "Colon is not allowed" /// ); /// assert!( /// ParsedUriQualifiedName::from_str("Q{foo{bar}qux").is_err(), /// "URI part cannot have `{{` and `}}`" /// ); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` /// /// [`URIQualifiedName`]: /// https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-URIQualifiedName // `FromStr` can be implemented only for types with static lifetime. #[allow(clippy::should_implement_trait)] #[inline] pub fn from_str(s: &'a str) -> Result<Self, NameError> { Self::try_from(s) } /// Returns the string as `&UriQualifiedName`. /// /// # Exmaples /// /// ``` /// # use xml_string::names::ParsedUriQualifiedName; /// use xml_string::names::UriQualifiedName; /// /// let name = ParsedUriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name, "Q{foo}bar"); /// /// let s: &UriQualifiedName = name.as_uri_qualified_name(); /// assert_eq!(s, "Q{foo}bar"); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[inline] #[must_use] pub fn as_uri_qualified_name(&self) -> &'a UriQualifiedName { self.content } /// Returns the string as `&str`. /// /// # Exmaples /// /// ``` /// # use xml_string::names::ParsedUriQualifiedName; /// let name = ParsedUriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name, "Q{foo}bar"); /// /// let s: &str = name.as_str(); /// assert_eq!(s, "Q{foo}bar"); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[inline] #[must_use] pub fn as_str(&self) -> &'a str { self.content.as_str() } /// Returns the URI. /// /// # Examples /// /// ``` /// # use xml_string::names::ParsedUriQualifiedName; /// let name = ParsedUriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name.uri(), "foo"); /// /// let empty_uri = ParsedUriQualifiedName::from_str("Q{}foo")?; /// assert_eq!(empty_uri.uri(), ""); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[must_use] pub fn uri(&self) -> &'a str { &self.as_str()[2..(self.local_name_start.get() - 1)] } /// Returns the local name. /// /// # Examples /// /// ``` /// # use xml_string::names::ParsedUriQualifiedName; /// let name = ParsedUriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name.local_name(), "bar"); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[must_use] pub fn local_name(&self) -> &'a Ncname { let local_name = &self.as_str()[self.local_name_start.get()..]; unsafe { debug_assert!( Ncname::from_str(local_name).is_ok(), "The local name {:?} must be a valid NCName", local_name ); // This is safe because the local name is a valid NCName. Ncname::new_unchecked(local_name) } } /// Returns a pair of the URI and the local name. /// /// This is efficient version of `(self.uri(), self.local_name())`. /// /// # Examples /// /// ``` /// # use xml_string::names::ParsedUriQualifiedName; /// use std::convert::TryFrom; /// /// let name = ParsedUriQualifiedName::from_str("Q{foo}bar")?; /// assert_eq!(name.uri_and_local(), (name.uri(), name.local_name())); /// # Ok::<_, xml_string::names::NameError>(()) /// ``` #[must_use] pub fn uri_and_local(&self) -> (&'a str, &'a Ncname) { let local_name_start = self.local_name_start.get(); let uri = &self.as_str()[2..(local_name_start - 1)]; let local_name = &self.as_str()[local_name_start..]; let local_name = unsafe { debug_assert!( Ncname::from_str(local_name).is_ok(), "The local name {:?} must be a valid NCName", local_name ); // This is safe because the local name is a valid NCName. Ncname::new_unchecked(local_name) }; (uri, local_name) } } impl PartialEq<str> for ParsedUriQualifiedName<'_> { #[inline] fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl_cmp!(str, ParsedUriQualifiedName<'_>); impl PartialEq<&'_ str> for ParsedUriQualifiedName<'_> { #[inline] fn eq(&self, other: &&str) -> bool { self.as_str() == *other } } impl_cmp!(&str, ParsedUriQualifiedName<'_>); impl PartialEq<str> for &'_ ParsedUriQualifiedName<'_> { #[inline] fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl_cmp!(str, &ParsedUriQualifiedName<'_>); #[cfg(feature = "alloc")] impl PartialEq<alloc::string::String> for ParsedUriQualifiedName<'_> { #[inline] fn eq(&self, other: &alloc::string::String) -> bool { self.as_str() == *other } } #[cfg(feature = "alloc")] impl_cmp!(alloc::string::String, ParsedUriQualifiedName<'_>); #[cfg(feature = "alloc")] impl PartialEq<&alloc::string::String> for ParsedUriQualifiedName<'_> { #[inline] fn eq(&self, other: &&alloc::string::String) -> bool { self.as_str() == **other } } #[cfg(feature = "alloc")] impl_cmp!(&alloc::string::String, ParsedUriQualifiedName<'_>); #[cfg(feature = "alloc")] impl PartialEq<alloc::boxed::Box<str>> for ParsedUriQualifiedName<'_> { #[inline] fn eq(&self, other: &alloc::boxed::Box<str>) -> bool { self.as_str() == other.as_ref() } } #[cfg(feature = "alloc")] impl_cmp!(alloc::boxed::Box<str>, ParsedUriQualifiedName<'_>); #[cfg(feature = "alloc")] impl PartialEq<alloc::borrow::Cow<'_, str>> for ParsedUriQualifiedName<'_> { #[inline] fn eq(&self, other: &alloc::borrow::Cow<'_, str>) -> bool { self.as_str() == *other } } #[cfg(feature = "alloc")] impl_cmp!(alloc::borrow::Cow<'_, str>, ParsedUriQualifiedName<'_>); impl AsRef<str> for ParsedUriQualifiedName<'_> { #[inline] fn as_ref(&self) -> &str { self.as_str() } } impl AsRef<UriQualifiedName> for ParsedUriQualifiedName<'_> { #[inline] fn as_ref(&self) -> &UriQualifiedName { self.content } } impl<'a> From<&'a UriQualifiedName> for ParsedUriQualifiedName<'a> { fn from(s: &'a UriQualifiedName) -> Self { let local_name_start = s.local_name_start(); Self { content: s, local_name_start, } } } impl<'a> TryFrom<&'a str> for ParsedUriQualifiedName<'a> { type Error = NameError; fn try_from(s: &'a str) -> Result<Self, Self::Error> { match UriQualifiedName::parse_as_possible(s) { Ok(local_name_start) => { let content = unsafe { // This is safe because the string is validated by // `UriQualifiedName::parse_as_possible()`. UriQualifiedName::new_unchecked(s) }; Ok(Self { content, local_name_start, }) } Err(e) => Err(NameError::new( TargetNameType::UriQualifiedName, e.map_or(0, |(_local_name_start, valid_up_to)| valid_up_to.get()), )), } } } impl fmt::Debug for ParsedUriQualifiedName<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.as_str()) } } impl fmt::Display for ParsedUriQualifiedName<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.as_str()) } } #[cfg(test)] mod tests { use super::*; fn ncname(s: &str) -> &Ncname { Ncname::from_str(s) .unwrap_or_else(|e| panic!("Failed to cerate Ncname from {:?}: {}", s, e)) } fn uqn(s: &str) -> &UriQualifiedName { UriQualifiedName::from_str(s) .unwrap_or_else(|e| panic!("Failed to create UriQualifiedName from {:?}: {}", s, e)) } fn parsed_uqn(s: &str) -> ParsedUriQualifiedName<'_> { ParsedUriQualifiedName::from_str(s).unwrap_or_else(|e| { panic!( "Failed to create ParsedUriQualifiedName from {:?}: {}", s, e ) }) } fn ensure_eq(s: &str) { assert_eq!( UriQualifiedName::from_str(s).expect("Should not fail"), s, "String: {:?}", s ); } fn ensure_error_at(s: &str, valid_up_to: usize) { let err = UriQualifiedName::from_str(s).expect_err("Should fail"); assert_eq!(err.valid_up_to(), valid_up_to, "String: {:?}", s); } #[test] fn qname_str_valid() { ensure_eq("Q{}local"); ensure_eq("Q{foo}bar"); ensure_eq("Q{http://example.com/}local"); } #[test] fn qname_str_invalid() { ensure_error_at("", 0); ensure_error_at("Q", 0); ensure_error_at("Q{", 0); ensure_error_at("Q{}", 0); ensure_error_at("Q{}:", 0); ensure_error_at("Q{}foo:", 6); ensure_error_at("Q{}foo:bar", 6); ensure_error_at("Q{foo}bar:baz", 9); ensure_error_at("Q{foo}bar}baz", 9); ensure_error_at("Q{foo{bar}baz", 0); } #[test] fn parse_as_possible() { assert_eq!( UriQualifiedName::parse_as_possible("Q{}bar"), Ok(NonZeroUsize::new(3).expect("Should never fail: not zero")) ); assert_eq!( UriQualifiedName::parse_as_possible("Q{foo}bar"), Ok(NonZeroUsize::new(6).expect("Should never fail: not zero")) ); assert_eq!(UriQualifiedName::parse_as_possible(""), Err(None)); assert_eq!( UriQualifiedName::parse_as_possible("Q{}foo:bar"), Err(NonZeroUsize::new(3).zip(NonZeroUsize::new(6))) ); assert_eq!( UriQualifiedName::parse_as_possible("Q{foo}bar:baz"), Err(NonZeroUsize::new(6).zip(NonZeroUsize::new(9))) ); } #[test] fn parsed_uri_qualified_name_from_str() { assert_eq!( ParsedUriQualifiedName::from_str("Q{foo}bar").map(|v| v.as_uri_qualified_name()), Ok(uqn("Q{foo}bar")) ); assert_eq!( ParsedUriQualifiedName::from_str("Q{foo}bar").map(|v| v.as_uri_qualified_name()), Ok(uqn("Q{foo}bar")) ); assert_eq!( ParsedUriQualifiedName::from_str("Q{foo}:bar"), Err(NameError::new(TargetNameType::UriQualifiedName, 0)) ); assert_eq!( ParsedUriQualifiedName::from_str("Q{foo}bar:baz"), Err(NameError::new(TargetNameType::UriQualifiedName, 9)) ); } #[test] fn parsed_uri_qualified_name_uri() { assert_eq!(parsed_uqn("Q{}foo").uri(), ""); assert_eq!(parsed_uqn("Q{foo}bar").uri(), "foo"); } #[test] fn parsed_uri_qualified_name_local_name() { assert_eq!(parsed_uqn("Q{}foo").local_name(), ncname("foo")); assert_eq!(parsed_uqn("Q{foo}bar").local_name(), ncname("bar")); } #[test] fn parsed_uri_qualified_name_uri_and_local() { assert_eq!(parsed_uqn("Q{}foo").uri_and_local(), ("", ncname("foo"))); assert_eq!( parsed_uqn("Q{foo}bar").uri_and_local(), ("foo", ncname("bar")) ); } }