pub struct FileSource { /* private fields */ }Expand description
A Source which provides values by reading them from the file-system.
For string and byte types, the source will simply attempt to open the file and load its contents.
If, during de-serialization, the target type is known, the source will attempt to load the file
as a string parse the value into the target type using std::str::FromStr.
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 loaded file contents. The source parses the file contents in following order:
true,false->bool123,42->u64-123,-42->i64-123.0,42.12->f64- any valid UTF-8 string ->
String - ->
Vec<u8>
§Warning:
This source must not be used with untrusted user input, it provides unfiltered access to the filesystem.
Implementations§
Source§impl FileSource
impl FileSource
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a FileSource.
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::FileSource;
let mut source = FileSource::new();
let mut de = serde_json::Deserializer::from_str(r#""${my_file.txt}""#);
let r: String = serde_vars::deserialize(&mut de, &mut source).unwrap();
assert_eq!(r, "some secret value");Sourcepub fn with_base_path<P>(self, path: P) -> Self
pub fn with_base_path<P>(self, path: P) -> Self
Configures the base path to use for relative paths.
The configured path is joined with relative paths. To be independent of the current working directory it is recommended to configure an absolute path.
Note: There is no validation that a final path must be within that base directory.
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:
use serde_vars::FileSource;
let mut source = FileSource::new().with_variable_prefix("${file:");
let mut de = serde_json::Deserializer::from_str(r#""${file:my_file.txt}""#);
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.
Trait Implementations§
Source§impl Default for FileSource
impl Default for FileSource
Source§impl Source for FileSource
impl Source for FileSource
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.