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->bool123,42->u64-123,-42->i64-123.0,42.12->f64foobar,"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>
impl<T> StringSource<T>
Sourcepub fn new(lookup: T) -> Self
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");Sourcepub fn with_variable_prefix(self, prefix: impl Into<String>) -> Self
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");Sourcepub fn with_variable_suffix(self, suffix: impl Into<String>) -> Self
pub fn with_variable_suffix(self, suffix: impl Into<String>) -> Self
Changes the variable suffix.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Returns the contained StringLookup.
Trait Implementations§
Source§impl<T: Debug> Debug for StringSource<T>
impl<T: Debug> Debug for StringSource<T>
Source§impl<T> Default for StringSource<T>where
T: Default,
impl<T> Default for StringSource<T>where
T: Default,
Source§impl<T> Source for StringSource<T>where
T: StringLookup,
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,
fn expand_str<'a, E>(
&mut self,
v: Cow<'a, str>,
) -> Result<Expansion<Cow<'a, str>>, E>where
E: Error,
Source§fn expand_bytes<'a, E>(
&mut self,
v: Cow<'a, [u8]>,
) -> Result<Expansion<Cow<'a, [u8]>>, E>where
E: Error,
fn expand_bytes<'a, E>(
&mut self,
v: Cow<'a, [u8]>,
) -> Result<Expansion<Cow<'a, [u8]>>, E>where
E: Error,
Source§fn expand_bool<E>(&mut self, v: &str) -> Result<Option<bool>, E>where
E: Error,
fn expand_bool<E>(&mut self, v: &str) -> Result<Option<bool>, E>where
E: Error,
Source§fn expand_i8<E>(&mut self, v: &str) -> Result<Option<i8>, E>where
E: Error,
fn expand_i8<E>(&mut self, v: &str) -> Result<Option<i8>, E>where
E: Error,
i8.Source§fn expand_i16<E>(&mut self, v: &str) -> Result<Option<i16>, E>where
E: Error,
fn expand_i16<E>(&mut self, v: &str) -> Result<Option<i16>, E>where
E: Error,
i16.Source§fn expand_i32<E>(&mut self, v: &str) -> Result<Option<i32>, E>where
E: Error,
fn expand_i32<E>(&mut self, v: &str) -> Result<Option<i32>, E>where
E: Error,
i32.Source§fn expand_i64<E>(&mut self, v: &str) -> Result<Option<i64>, E>where
E: Error,
fn expand_i64<E>(&mut self, v: &str) -> Result<Option<i64>, E>where
E: Error,
i64.Source§fn expand_u8<E>(&mut self, v: &str) -> Result<Option<u8>, E>where
E: Error,
fn expand_u8<E>(&mut self, v: &str) -> Result<Option<u8>, E>where
E: Error,
u8.Source§fn expand_u16<E>(&mut self, v: &str) -> Result<Option<u16>, E>where
E: Error,
fn expand_u16<E>(&mut self, v: &str) -> Result<Option<u16>, E>where
E: Error,
u16.Source§fn expand_u32<E>(&mut self, v: &str) -> Result<Option<u32>, E>where
E: Error,
fn expand_u32<E>(&mut self, v: &str) -> Result<Option<u32>, E>where
E: Error,
u32.Source§fn expand_u64<E>(&mut self, v: &str) -> Result<Option<u64>, E>where
E: Error,
fn expand_u64<E>(&mut self, v: &str) -> Result<Option<u64>, E>where
E: Error,
u64.Source§fn expand_f32<E>(&mut self, v: &str) -> Result<Option<f32>, E>where
E: Error,
fn expand_f32<E>(&mut self, v: &str) -> Result<Option<f32>, E>where
E: Error,
f32.