StringSource

Struct StringSource 

Source
pub struct StringSource<T> { /* private fields */ }
Expand description

A Source which provides values using a string based StringLookup.

This source only works with strings, but since data can be serialized into any type, it implements some basic conversion of data types through std::str::FromStr.

During de-serialization most of the time, the target type is known. For example, when de-serializing a field foo: u32, the target type is known to be u32. In these cases the StringSource, will attempt to parse the requested type from the provided string.

When de-serializing self-describing formats, like JSON or YAML into dynamic containers, like for example:

#[derive(serde::Deserialize)]
#[serde(untagged)]
enum StringOrInt {
    String(String),
    Int(u64),
}

The target type is inferred from the parsed type. In these cases the StringSource, needs to make the type decision.

Dynamic parsing (Source::expand_any), parses in order bool, u64, f64, String and yields the first one which succeeds. To explicitly force a string, the source allows to explicitly wrap the value in an additional pair of ", which will be stripped:

  • true, false -> bool
  • 123, 42 -> u64
  • -123, -42 -> i64
  • -123.0, 42.12 -> f64
  • foobar, "foobar", "true", 123 some more -> String

For consistency reasons, known string expansions use the same parsing logic and require ambiguous values to be explicitly marked as a string.

Implementations§

Source§

impl<T> StringSource<T>

Source

pub fn new(lookup: T) -> Self

Creates a Self using the specified StringLookup.

By default the created source uses ${ and } as variable specifiers. These can be changed using Self::with_variable_prefix and Self::with_variable_suffix.

§Examples:
use serde_vars::StringSource;
use std::collections::HashMap;

let source = HashMap::from([("MY_VAR".to_owned(), "some secret value".to_owned())]);
let mut source = StringSource::new(source);

let mut de = serde_json::Deserializer::from_str(r#""${MY_VAR}""#);
let r: String = serde_vars::deserialize(&mut de, &mut source).unwrap();
assert_eq!(r, "some secret value");
Source

pub fn with_variable_prefix(self, prefix: impl Into<String>) -> Self

Changes the variable prefix.

§Examples:
let source = HashMap::from([("MY_VAR".to_owned(), "some secret value".to_owned())]);
let mut source = StringSource::new(source).with_variable_prefix("${env:");

let mut de = serde_json::Deserializer::from_str(r#""${env:MY_VAR}""#);
let r: String = serde_vars::deserialize(&mut de, &mut source).unwrap();
assert_eq!(r, "some secret value");
Source

pub fn with_variable_suffix(self, suffix: impl Into<String>) -> Self

Changes the variable suffix.

Source

pub fn into_inner(self) -> T

Returns the contained StringLookup.

Trait Implementations§

Source§

impl<T: Debug> Debug for StringSource<T>

Source§

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

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

impl<T> Default for StringSource<T>
where T: Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Source for StringSource<T>
where T: StringLookup,

Source§

fn expand_str<'a, E>( &mut self, v: Cow<'a, str>, ) -> Result<Expansion<Cow<'a, str>>, E>
where E: Error,

Expands a variable string to string.
Source§

fn expand_bytes<'a, E>( &mut self, v: Cow<'a, [u8]>, ) -> Result<Expansion<Cow<'a, [u8]>>, E>
where E: Error,

Expands bytes into other bytes. Read more
Source§

fn expand_bool<E>(&mut self, v: &str) -> Result<Option<bool>, E>
where E: Error,

Expands a variable string to a boolean.
Source§

fn expand_i8<E>(&mut self, v: &str) -> Result<Option<i8>, E>
where E: Error,

Expands a variable string to an i8.
Source§

fn expand_i16<E>(&mut self, v: &str) -> Result<Option<i16>, E>
where E: Error,

Expands a variable string to an i16.
Source§

fn expand_i32<E>(&mut self, v: &str) -> Result<Option<i32>, E>
where E: Error,

Expands a variable string to an i32.
Source§

fn expand_i64<E>(&mut self, v: &str) -> Result<Option<i64>, E>
where E: Error,

Expands a variable string to an i64.
Source§

fn expand_u8<E>(&mut self, v: &str) -> Result<Option<u8>, E>
where E: Error,

Expands a variable string to an u8.
Source§

fn expand_u16<E>(&mut self, v: &str) -> Result<Option<u16>, E>
where E: Error,

Expands a variable string to an u16.
Source§

fn expand_u32<E>(&mut self, v: &str) -> Result<Option<u32>, E>
where E: Error,

Expands a variable string to an u32.
Source§

fn expand_u64<E>(&mut self, v: &str) -> Result<Option<u64>, E>
where E: Error,

Expands a variable string to an u64.
Source§

fn expand_f32<E>(&mut self, v: &str) -> Result<Option<f32>, E>
where E: Error,

Expands a variable string to a f32.
Source§

fn expand_f64<E>(&mut self, v: &str) -> Result<Option<f64>, E>
where E: Error,

Expands a variable string to a f64.
Source§

fn expand_any<'a, E>( &mut self, v: Cow<'a, str>, ) -> Result<Expansion<Any<'a>, Cow<'a, str>>, E>
where E: Error,

Expands a variable string to Any. Read more

Auto Trait Implementations§

§

impl<T> Freeze for StringSource<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for StringSource<T>
where T: RefUnwindSafe,

§

impl<T> Send for StringSource<T>
where T: Send,

§

impl<T> Sync for StringSource<T>
where T: Sync,

§

impl<T> Unpin for StringSource<T>
where T: Unpin,

§

impl<T> UnwindSafe for StringSource<T>
where T: UnwindSafe,

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> 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, 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.