pub struct ValueTemplate { /* private fields */ }Expand description
Encapsulates a JSON template and allows injecting and extracting values from it.
Implementations§
Source§impl ValueTemplate
impl ValueTemplate
Sourcepub fn new(
template: Value,
placeholder_string: &str,
repeat_string: &str,
) -> Result<Self, TemplateParsingError>
pub fn new( template: Value, placeholder_string: &str, repeat_string: &str, ) -> Result<Self, TemplateParsingError>
Prepare a template for injection or extraction.
§Parameters
template: JSON value that acts a template. Its placeholder values will be replaced by actual values during injection, and actual values will be recovered from their location during extraction.placeholder_string: Value that a JSON string should assume to act as a placeholder value that can be injected into or extracted from.repeat_string: Sentinel value that can be placed as the second value in an array to indicate that the first value can be repeated any number of times. The first value should contain exactly one placeholder string.
§Errors
TemplateParsingError: refer to the documentation of this type
Sourcepub fn has_array_value(&self) -> bool
pub fn has_array_value(&self) -> bool
Whether there is a placeholder that can be repeated.
- During injection, all values are injected in the array placeholder,
- During extraction, all repeatable placeholders are extracted from the array.
Sourcepub fn inject(
&self,
values: impl IntoIterator<Item = Value>,
) -> Result<Value, MissingValue>
pub fn inject( &self, values: impl IntoIterator<Item = Value>, ) -> Result<Value, MissingValue>
Render a value from the template and context values.
§Error
MissingValue: if the number of injected values is 0.
Sourcepub fn extract<T>(&self, value: Value) -> Result<Vec<T>, ExtractionError>where
T: for<'de> Deserialize<'de>,
pub fn extract<T>(&self, value: Value) -> Result<Vec<T>, ExtractionError>where
T: for<'de> Deserialize<'de>,
Extract sub values from the template and a value.
§Errors
- if a single placeholder is missing.
- if there is no value corresponding to an array placeholder
- if the value corresponding to an array placeholder is not an array
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ValueTemplate
impl RefUnwindSafe for ValueTemplate
impl Send for ValueTemplate
impl Sync for ValueTemplate
impl Unpin for ValueTemplate
impl UnwindSafe for ValueTemplate
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
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more