Skip to main content

ExpandStr

Struct ExpandStr 

Source
pub struct ExpandStr(/* private fields */);
Expand description

A template string with ${name} placeholders, awaiting expansion.

Construct with new (or new_static for a 'static literal), then resolve against an ExpandLookup with expand_to_string or write_expanded. Deserialises transparently from a string, so an ExpandStr field in a serde config is just a plain string in TOML / JSON / YAML.

Construction performs no validation: a template with an unterminated ${ or an unresolved placeholder is held verbatim until expansion time and only then errors. The trade-off is that new / new_static are infallible — and new_static is const, so an ExpandStr can live in a const or static binding.

§Example

use std::collections::HashMap;
use zenops_expand::ExpandStr;

let path = ExpandStr::new_static("${home}/.config");

let mut env = HashMap::new();
env.insert("home", "/home/ada");

assert_eq!(path.expand_to_string(&env).unwrap(), "/home/ada/.config");

Implementations§

Source§

impl ExpandStr

Source

pub fn new(raw: SmolStr) -> Self

Wrap a template string.

Source

pub const fn new_static(raw: &'static str) -> Self

Wrap a 'static template string without allocating.

const, so suitable for const and static bindings.

Source

pub fn expand_to_string( &self, lookup: &(impl ExpandLookup + ?Sized), ) -> Result<String, ExpandError>

Expand the template into a new String.

Each ${name} is replaced with the value lookup writes for that name. Literal characters pass through unchanged.

§Example
use std::collections::HashMap;
use zenops_expand::ExpandStr;

let t = ExpandStr::new_static("${greeting}, ${name}!");

let mut lookup: HashMap<&str, &str> = HashMap::new();
lookup.insert("greeting", "hi");
lookup.insert("name", "Ada");

assert_eq!(t.expand_to_string(&lookup).unwrap(), "hi, Ada!");
Source

pub fn write_expanded( &self, lookup: &(impl ExpandLookup + ?Sized), f: &mut impl Write, ) -> Result<(), ExpandError>

Expand the template into an existing fmt::Write sink.

Equivalent to expand_to_string but writes into a caller-supplied buffer, so multiple templates can be concatenated without intermediate allocations. On error the sink may have been written to partially.

§Example
use std::collections::HashMap;
use std::fmt::Write;
use zenops_expand::ExpandStr;

let mut lookup: HashMap<&str, &str> = HashMap::new();
lookup.insert("user", "ada");

let mut out = String::from("path=");
let t = ExpandStr::new_static("/home/${user}");
t.write_expanded(&lookup, &mut out).unwrap();
write!(out, ";").unwrap();

assert_eq!(out, "path=/home/ada;");
Source

pub fn as_template(&self) -> &str

Get the raw template string.

Trait Implementations§

Source§

impl Clone for ExpandStr

Source§

fn clone(&self) -> ExpandStr

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ExpandStr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ExpandStr

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for ExpandStr

Source§

impl PartialEq for ExpandStr

Source§

fn eq(&self, other: &ExpandStr) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ExpandStr

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.