pub struct UriTemplateString { /* private fields */ }
alloc
only.Expand description
Implementations§
source§impl UriTemplateString
impl UriTemplateString
sourcepub unsafe fn new_unchecked(s: String) -> Self
pub unsafe fn new_unchecked(s: String) -> 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 shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the inner buffer to match its length.
sourcepub fn as_slice(&self) -> &UriTemplateStr
pub fn as_slice(&self) -> &UriTemplateStr
Returns the borrowed IRI string slice.
This is equivalent to &*self
.
sourcepub fn append(&mut self, other: &UriTemplateStr)
pub fn append(&mut self, other: &UriTemplateStr)
Appends the template string.
Methods from Deref<Target = UriTemplateStr>§
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}");
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the template string length.
§Examples
use iri_string::template::UriTemplateStr;
let template = UriTemplateStr::new("/users/{username}")?;
assert_eq!(template.len(), "/users/{username}".len());
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the string is empty.
§Examples
use iri_string::template::UriTemplateStr;
let template = UriTemplateStr::new("/users/{username}")?;
assert!(!template.is_empty());
let empty = UriTemplateStr::new("")?;
assert!(empty.is_empty());
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>
pub fn expand_dynamic_to_string<S: Spec, C: DynamicContext>( &self, context: &mut C, ) -> Result<String, Error>
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 UriTemplateString
impl AsRef<UriTemplateStr> for UriTemplateString
source§fn as_ref(&self) -> &UriTemplateStr
fn as_ref(&self) -> &UriTemplateStr
source§impl AsRef<str> for UriTemplateString
impl AsRef<str> for UriTemplateString
source§impl Borrow<UriTemplateStr> for UriTemplateString
impl Borrow<UriTemplateStr> for UriTemplateString
source§fn borrow(&self) -> &UriTemplateStr
fn borrow(&self) -> &UriTemplateStr
source§impl Borrow<str> for UriTemplateString
impl Borrow<str> for UriTemplateString
source§impl Clone for UriTemplateString
impl Clone for UriTemplateString
source§fn clone(&self) -> UriTemplateString
fn clone(&self) -> UriTemplateString
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for UriTemplateString
impl Debug for UriTemplateString
source§impl Default for UriTemplateString
impl Default for UriTemplateString
source§fn default() -> UriTemplateString
fn default() -> UriTemplateString
source§impl Deref for UriTemplateString
impl Deref for UriTemplateString
source§type Target = UriTemplateStr
type Target = UriTemplateStr
source§fn deref(&self) -> &UriTemplateStr
fn deref(&self) -> &UriTemplateStr
source§impl<'de> Deserialize<'de> for UriTemplateString
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for UriTemplateString
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 UriTemplateString
impl Display for UriTemplateString
source§impl From<&UriTemplateStr> for UriTemplateString
impl From<&UriTemplateStr> for UriTemplateString
source§fn from(s: &UriTemplateStr) -> Self
fn from(s: &UriTemplateStr) -> Self
source§impl From<UriTemplateString> for Box<UriTemplateStr>
impl From<UriTemplateString> for Box<UriTemplateStr>
source§fn from(s: UriTemplateString) -> Box<UriTemplateStr>
fn from(s: UriTemplateString) -> Box<UriTemplateStr>
source§impl<'a> From<UriTemplateString> for Cow<'a, UriTemplateStr>
impl<'a> From<UriTemplateString> for Cow<'a, UriTemplateStr>
source§fn from(s: UriTemplateString) -> Cow<'a, UriTemplateStr>
fn from(s: UriTemplateString) -> Cow<'a, UriTemplateStr>
source§impl From<UriTemplateString> for String
impl From<UriTemplateString> for String
source§fn from(s: UriTemplateString) -> Self
fn from(s: UriTemplateString) -> Self
source§impl FromStr for UriTemplateString
impl FromStr for UriTemplateString
source§impl Hash for UriTemplateString
impl Hash for UriTemplateString
source§impl Ord for UriTemplateString
impl Ord for UriTemplateString
source§fn cmp(&self, other: &UriTemplateString) -> Ordering
fn cmp(&self, other: &UriTemplateString) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq<&str> for UriTemplateString
impl PartialEq<&str> for UriTemplateString
source§impl PartialEq<String> for UriTemplateString
impl PartialEq<String> for UriTemplateString
source§impl PartialEq<UriTemplateString> for &str
impl PartialEq<UriTemplateString> for &str
source§impl PartialEq<UriTemplateString> for String
impl PartialEq<UriTemplateString> for String
source§impl PartialEq<UriTemplateString> for str
impl PartialEq<UriTemplateString> for str
source§impl PartialEq<str> for UriTemplateString
impl PartialEq<str> for UriTemplateString
source§impl PartialEq for UriTemplateString
impl PartialEq for UriTemplateString
source§impl PartialOrd<&str> for UriTemplateString
impl PartialOrd<&str> for UriTemplateString
source§impl PartialOrd<Cow<'_, str>> for UriTemplateString
impl PartialOrd<Cow<'_, str>> for UriTemplateString
source§impl PartialOrd<String> for UriTemplateString
impl PartialOrd<String> for UriTemplateString
source§impl PartialOrd<UriTemplateString> for &str
impl PartialOrd<UriTemplateString> for &str
source§impl PartialOrd<UriTemplateString> for Cow<'_, str>
impl PartialOrd<UriTemplateString> for Cow<'_, str>
source§impl PartialOrd<UriTemplateString> for String
impl PartialOrd<UriTemplateString> for String
source§impl PartialOrd<UriTemplateString> for str
impl PartialOrd<UriTemplateString> for str
source§impl PartialOrd<str> for UriTemplateString
impl PartialOrd<str> for UriTemplateString
source§impl PartialOrd for UriTemplateString
impl PartialOrd for UriTemplateString
source§impl Serialize for UriTemplateString
impl Serialize for UriTemplateString
source§impl TryFrom<&[u8]> for UriTemplateString
impl TryFrom<&[u8]> for UriTemplateString
source§impl TryFrom<&str> for UriTemplateString
impl TryFrom<&str> for UriTemplateString
source§impl TryFrom<String> for UriTemplateString
impl TryFrom<String> for UriTemplateString
impl Eq for UriTemplateString
impl StructuralPartialEq for UriTemplateString
Auto Trait Implementations§
impl Freeze for UriTemplateString
impl RefUnwindSafe for UriTemplateString
impl Send for UriTemplateString
impl Sync for UriTemplateString
impl Unpin for UriTemplateString
impl UnwindSafe for UriTemplateString
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
source§fn try_to_string(&self) -> Result<String, TryReserveError>
Available on crate feature alloc
only.
fn try_to_string(&self) -> Result<String, TryReserveError>
alloc
only.ToString::to_string
, but without panic on OOM.