pub struct UriTemplateStr { /* private fields */ }
Expand description
A borrowed slice of a URI template.
URI Template is defined by RFC 6570.
Note that “URI Template” can also be used for IRI.
§Valid values
This type can have a URI template string.
§Applied errata
Errata ID 6937 is applied, so single quotes are allowed to appear in an URI template.
use iri_string::template::UriTemplateStr;
let template = UriTemplateStr::new("'quoted'")?;
Implementations§
source§impl UriTemplateStr
impl UriTemplateStr
sourcepub fn new(s: &str) -> Result<&Self, Error>
pub fn new(s: &str) -> Result<&Self, Error>
Creates a new string.
§Examples
use iri_string::template::UriTemplateStr;
let template = UriTemplateStr::new("/users/{username}")?;
sourcepub unsafe fn new_unchecked(s: &str) -> &Self
pub unsafe fn new_unchecked(s: &str) -> &Self
Creates a new string without validation.
This does not validate the given string, so it is caller’s responsibility to ensure the given string is valid.
§Safety
The given string must be syntactically valid as Self
type.
If not, any use of the returned value or the call of this
function itself may result in undefined behavior.
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the template as a plain &str
.
§Examples
use iri_string::template::UriTemplateStr;
let template = UriTemplateStr::new("/users/{username}")?;
assert_eq!(template.as_str(), "/users/{username}");
source§impl UriTemplateStr
impl UriTemplateStr
sourcepub fn expand<'a, S: Spec, C: Context>(
&'a self,
context: &'a C,
) -> Result<Expanded<'a, S, C>, Error>
pub fn expand<'a, S: Spec, C: Context>( &'a self, context: &'a C, ) -> Result<Expanded<'a, S, C>, Error>
Expands the template with the given context.
§Examples
use iri_string::spec::UriSpec;
use iri_string::template::UriTemplateStr;
use iri_string::template::simple_context::SimpleContext;
let mut context = SimpleContext::new();
context.insert("username", "foo");
let template = UriTemplateStr::new("/users/{username}")?;
let expanded = template.expand::<UriSpec, _>(&context)?;
assert_eq!(
expanded.to_string(),
"/users/foo"
);
You can control allowed characters in the output by changing spec type.
use iri_string::spec::{IriSpec, UriSpec};
use iri_string::template::UriTemplateStr;
use iri_string::template::simple_context::SimpleContext;
let mut context = SimpleContext::new();
context.insert("alpha", "\u{03B1}");
let template = UriTemplateStr::new("{?alpha}")?;
assert_eq!(
template.expand::<UriSpec, _>(&context)?.to_string(),
"?alpha=%CE%B1",
"a URI cannot contain Unicode alpha (U+03B1), so it should be escaped"
);
assert_eq!(
template.expand::<IriSpec, _>(&context)?.to_string(),
"?alpha=\u{03B1}",
"an IRI can contain Unicode alpha (U+03B1), so it written as is"
);
sourcepub fn expand_dynamic<S: Spec, W: Write, C: DynamicContext>(
&self,
writer: &mut W,
context: &mut C,
) -> Result<(), Error>
pub fn expand_dynamic<S: Spec, W: Write, C: DynamicContext>( &self, writer: &mut W, context: &mut C, ) -> Result<(), Error>
Expands the template with the given dynamic context.
If you need the allocated String
, useexpand_dynamic_to_string
.
See the documentation for DynamicContext
for usage.
sourcepub fn expand_dynamic_to_string<S: Spec, C: DynamicContext>(
&self,
context: &mut C,
) -> Result<String, Error>
Available on crate feature alloc
only.
pub fn expand_dynamic_to_string<S: Spec, C: DynamicContext>( &self, context: &mut C, ) -> Result<String, Error>
alloc
only.Expands the template into a string, with the given dynamic context.
This is basically expand_dynamic
method
that returns an owned string instead of writing to the given writer.
See the documentation for DynamicContext
for usage.
§Examples
use iri_string::template::UriTemplateStr;
use iri_string::spec::UriSpec;
struct MyContext<'a> {
// See the documentation for `DynamicContext`.
}
let mut context = MyContext {
target: "/posts/1",
username: Some("the_admin"),
username_visited: false,
};
// No access to the variable `username`.
let template = UriTemplateStr::new("{+target}{?username}")?;
let s = template.expand_dynamic_to_string::<UriSpec, _>(&mut context)?;
assert_eq!(s, "/posts/1?username=the_admin");
assert!(context.username_visited);
sourcepub fn variables(&self) -> UriTemplateVariables<'_> ⓘ
pub fn variables(&self) -> UriTemplateVariables<'_> ⓘ
Returns an iterator of variables in the template.
§Examples
use iri_string::template::UriTemplateStr;
let template = UriTemplateStr::new("foo{/bar*,baz:4}{?qux}{&bar*}")?;
let mut vars = template.variables();
assert_eq!(vars.next().map(|var| var.as_str()), Some("bar"));
assert_eq!(vars.next().map(|var| var.as_str()), Some("baz"));
assert_eq!(vars.next().map(|var| var.as_str()), Some("qux"));
assert_eq!(vars.next().map(|var| var.as_str()), Some("bar"));
Trait Implementations§
source§impl AsRef<UriTemplateStr> for UriTemplateStr
impl AsRef<UriTemplateStr> for UriTemplateStr
source§fn as_ref(&self) -> &UriTemplateStr
fn as_ref(&self) -> &UriTemplateStr
source§impl AsRef<UriTemplateStr> for UriTemplateString
Available on crate feature alloc
only.
impl AsRef<UriTemplateStr> for UriTemplateString
alloc
only.source§fn as_ref(&self) -> &UriTemplateStr
fn as_ref(&self) -> &UriTemplateStr
source§impl AsRef<str> for UriTemplateStr
impl AsRef<str> for UriTemplateStr
source§impl Borrow<UriTemplateStr> for UriTemplateString
Available on crate feature alloc
only.
impl Borrow<UriTemplateStr> for UriTemplateString
alloc
only.source§fn borrow(&self) -> &UriTemplateStr
fn borrow(&self) -> &UriTemplateStr
source§impl Debug for UriTemplateStr
impl Debug for UriTemplateStr
source§impl<'a, 'de: 'a> Deserialize<'de> for &'a UriTemplateStr
Available on crate feature serde
only.
impl<'a, 'de: 'a> Deserialize<'de> for &'a UriTemplateStr
serde
only.source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl Display for UriTemplateStr
impl Display for UriTemplateStr
source§impl<'a> From<&'a UriTemplateStr> for &'a str
impl<'a> From<&'a UriTemplateStr> for &'a str
source§fn from(s: &'a UriTemplateStr) -> &'a str
fn from(s: &'a UriTemplateStr) -> &'a str
source§impl From<&UriTemplateStr> for Arc<UriTemplateStr>
Available on crate feature alloc
only.
impl From<&UriTemplateStr> for Arc<UriTemplateStr>
alloc
only.source§fn from(s: &UriTemplateStr) -> Self
fn from(s: &UriTemplateStr) -> Self
source§impl From<&UriTemplateStr> for Box<UriTemplateStr>
Available on crate feature alloc
only.
impl From<&UriTemplateStr> for Box<UriTemplateStr>
alloc
only.source§fn from(s: &UriTemplateStr) -> Self
fn from(s: &UriTemplateStr) -> Self
source§impl<'a> From<&'a UriTemplateStr> for Cow<'a, UriTemplateStr>
Available on crate feature alloc
only.
impl<'a> From<&'a UriTemplateStr> for Cow<'a, UriTemplateStr>
alloc
only.source§fn from(s: &'a UriTemplateStr) -> Self
fn from(s: &'a UriTemplateStr) -> Self
source§impl From<&UriTemplateStr> for Rc<UriTemplateStr>
Available on crate feature alloc
only.
impl From<&UriTemplateStr> for Rc<UriTemplateStr>
alloc
only.source§fn from(s: &UriTemplateStr) -> Self
fn from(s: &UriTemplateStr) -> Self
source§impl From<&UriTemplateStr> for UriTemplateString
Available on crate feature alloc
only.
impl From<&UriTemplateStr> for UriTemplateString
alloc
only.source§fn from(s: &UriTemplateStr) -> Self
fn from(s: &UriTemplateStr) -> Self
source§impl From<UriTemplateString> for Box<UriTemplateStr>
Available on crate feature alloc
only.
impl From<UriTemplateString> for Box<UriTemplateStr>
alloc
only.source§fn from(s: UriTemplateString) -> Box<UriTemplateStr>
fn from(s: UriTemplateString) -> Box<UriTemplateStr>
source§impl Hash for UriTemplateStr
impl Hash for UriTemplateStr
source§impl Ord for UriTemplateStr
impl Ord for UriTemplateStr
source§impl PartialEq<&UriTemplateStr> for str
impl PartialEq<&UriTemplateStr> for str
source§impl PartialEq<&str> for UriTemplateStr
impl PartialEq<&str> for UriTemplateStr
source§impl PartialEq<UriTemplateStr> for &str
impl PartialEq<UriTemplateStr> for &str
source§impl PartialEq<UriTemplateStr> for str
impl PartialEq<UriTemplateStr> for str
source§impl PartialEq<str> for &UriTemplateStr
impl PartialEq<str> for &UriTemplateStr
source§impl PartialEq<str> for UriTemplateStr
impl PartialEq<str> for UriTemplateStr
source§impl PartialEq for UriTemplateStr
impl PartialEq for UriTemplateStr
source§impl PartialOrd<&UriTemplateStr> for Cow<'_, str>
Available on crate feature alloc
only.
impl PartialOrd<&UriTemplateStr> for Cow<'_, str>
alloc
only.source§impl PartialOrd<&UriTemplateStr> for str
impl PartialOrd<&UriTemplateStr> for str
source§impl PartialOrd<&str> for UriTemplateStr
impl PartialOrd<&str> for UriTemplateStr
source§impl PartialOrd<Cow<'_, str>> for &UriTemplateStr
Available on crate feature alloc
only.
impl PartialOrd<Cow<'_, str>> for &UriTemplateStr
alloc
only.source§impl PartialOrd<Cow<'_, str>> for UriTemplateStr
Available on crate feature alloc
only.
impl PartialOrd<Cow<'_, str>> for UriTemplateStr
alloc
only.source§impl PartialOrd<UriTemplateStr> for &str
impl PartialOrd<UriTemplateStr> for &str
source§impl PartialOrd<UriTemplateStr> for Cow<'_, str>
Available on crate feature alloc
only.
impl PartialOrd<UriTemplateStr> for Cow<'_, str>
alloc
only.source§impl PartialOrd<UriTemplateStr> for str
impl PartialOrd<UriTemplateStr> for str
source§impl PartialOrd<str> for &UriTemplateStr
impl PartialOrd<str> for &UriTemplateStr
source§impl PartialOrd<str> for UriTemplateStr
impl PartialOrd<str> for UriTemplateStr
source§impl PartialOrd for UriTemplateStr
impl PartialOrd for UriTemplateStr
source§impl Serialize for UriTemplateStr
impl Serialize for UriTemplateStr
source§impl ToOwned for UriTemplateStr
Available on crate feature alloc
only.
impl ToOwned for UriTemplateStr
alloc
only.