pub struct Qname(/* private fields */);
Expand description
String slice for QName
.
Implementations§
Source§impl Qname
impl Qname
Sourcepub fn from_str(s: &str) -> Result<&Self, NameError>
pub fn from_str(s: &str) -> Result<&Self, NameError>
Creates a new &Qname
.
§Failures
Fails if the given string is not a valid QName
.
§Examples
let noprefix = Qname::from_str("hello")?;
assert_eq!(noprefix, "hello");
let prefixed = Qname::from_str("foo:bar")?;
assert_eq!(prefixed, "foo:bar");
assert!(Qname::from_str("").is_err(), "Empty string is not a QName");
assert!(Qname::from_str("foo bar").is_err(), "Whitespace is not allowed");
assert!(Qname::from_str("foo:bar:baz").is_err(), "Two or more colons are not allowed");
assert!(Qname::from_str("0foo").is_err(), "ASCII digit at the beginning is not allowed");
Sourcepub unsafe fn new_unchecked(s: &str) -> &Self
pub unsafe fn new_unchecked(s: &str) -> &Self
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the string as &str
.
§Examples
let name = Qname::from_str("foo:bar")?;
assert_eq!(name, "foo:bar");
let s: &str = name.as_str();
assert_eq!(s, "foo:bar");
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the string in bytes.
§Examples
let name = Qname::from_str("foo:bar")?;
assert_eq!(name.len(), 7);
Sourcepub fn parse_next(s: &str) -> Result<(&Self, &str), NameError>
pub fn parse_next(s: &str) -> Result<(&Self, &str), NameError>
Parses the leading Qname
and returns the value and the rest input.
§Exmaples
let input = "hello:012";
let expected = Qname::from_str("hello").expect("valid Qname");
assert_eq!(
Qname::parse_next(input),
Ok((expected, ":012"))
);
let input = "hello:world:foo";
let expected = Qname::from_str("hello:world").expect("valid Qname");
assert_eq!(
Qname::parse_next(input),
Ok((expected, ":foo"))
);
let input = "012";
assert!(Qname::parse_next(input).is_err());
Sourcepub fn has_prefix(&self) -> bool
pub fn has_prefix(&self) -> bool
Returns whether the QName has a prefix.
Note that this is O(length) operation.
Consider using ParsedQname::has_prefix
method if possible.
§Examples
let local_only = Qname::from_str("hello")?;
assert!(!local_only.has_prefix());
let prefixed = Qname::from_str("foo:bar")?;
assert!(prefixed.has_prefix());
Sourcepub fn prefix(&self) -> Option<&Ncname>
pub fn prefix(&self) -> Option<&Ncname>
Returns the prefix if available.
Note that this is O(length) operation.
Consider using ParsedQname::prefix
method if possible.
§Examples
let prefixed = Qname::from_str("foo:bar")?;
assert_eq!(prefixed.prefix().map(|s| s.as_str()), Some("foo"));
let noprefix = Qname::from_str("foo")?;
assert_eq!(noprefix.prefix().map(|s| s.as_str()), None);
Sourcepub fn local_part(&self) -> &Ncname
pub fn local_part(&self) -> &Ncname
Returns the local part.
Note that this is O(length) operation.
Consider using ParsedQname::local_part
method if possible.
§Examples
let prefixed = Qname::from_str("foo:bar")?;
assert_eq!(prefixed.local_part(), "bar");
let noprefix = Qname::from_str("foo")?;
assert_eq!(noprefix.local_part(), "foo");
Sourcepub fn prefix_and_local(&self) -> (Option<&Ncname>, &Ncname)
pub fn prefix_and_local(&self) -> (Option<&Ncname>, &Ncname)
Returns a pair of the prefix (if available) and the local part.
Note that this is O(length) operation.
Consider using ParsedQname::prefix_and_local
method if possible.
§Examples
use std::convert::TryFrom;
let noprefix = Qname::from_str("hello")?;
assert_eq!(noprefix.prefix_and_local(), (noprefix.prefix(), noprefix.local_part()));
let prefixed = Qname::from_str("foo:bar")?;
assert_eq!(prefixed.prefix_and_local(), (prefixed.prefix(), prefixed.local_part()));
Sourcepub fn into_boxed_str(self: Box<Self>) -> Box<str>
pub fn into_boxed_str(self: Box<Self>) -> Box<str>
Converts a Box<Qname>
into a Box<str>
without copying or allocating.
§Examples
let name = Qname::from_str("q:name")?;
let boxed_name: Box<Qname> = name.into();
assert_eq!(&*boxed_name, name);
let boxed_str: Box<str> = boxed_name.into_boxed_str();
assert_eq!(&*boxed_str, name.as_str());