FileSource

Struct FileSource 

Source
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 -> bool
  • 123, 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

Source

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

pub fn with_base_path<P>(self, path: P) -> Self
where P: Into<PathBuf>,

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.

Source

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");
Source

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

Changes the variable suffix.

Trait Implementations§

Source§

impl Default for FileSource

Source§

fn default() -> Self

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

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,

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§

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.