pub struct FileSource<F> { /* 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<StdFileSystem>
impl FileSource<StdFileSystem>
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");Source§impl<F> FileSource<F>
impl<F> FileSource<F>
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_file_system<T>(self, filesystem: T) -> FileSource<T>
pub fn with_file_system<T>(self, filesystem: T) -> FileSource<T>
Configures a custom loader to read files.
The loader is passed the path to read from the file system and is supposed to return the file’s contents.
The loader defaults to std::fs::read.
§Examples:
use serde_vars::source::{FileSource, FileSystem};
struct CustomFs;
impl FileSystem for CustomFs {
fn read(&mut self, path: &std::path::Path) -> std::io::Result<Vec<u8>> {
Ok(b"some secret value".to_vec())
}
}
let mut source = FileSource::new().with_file_system(CustomFs);
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_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<StdFileSystem>
impl Default for FileSource<StdFileSystem>
Source§impl<F: FileSystem> Source for FileSource<F>
impl<F: FileSystem> Source for FileSource<F>
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.