pub struct Deserializer<'a> { /* private fields */ }Expand description
Deserializes environment variables.
The variables are read when the deserializer is created. Most of the
time the from_env and
from_vars functions (or the methods of the same
name on DeserializerConfig) are all that is needed. The deserializer
is useful to configure the driver, for instance to add layers, or to
update a value (see update):
use deser::de::Deserializer as _;
use deser_env::Deserializer;
#[derive(Debug, deser::Deserialize)]
struct Config {
host: String,
port: u16,
}
let mut config = Config { host: "localhost".into(), port: 80 };
Deserializer::from_vars("APP_", [("APP_PORT", "8080")])
.update(&mut config)
.unwrap();
assert_eq!(config.host, "localhost");
assert_eq!(config.port, 8080);Implementations§
Source§impl Deserializer<'static>
impl Deserializer<'static>
Sourcepub fn from_env(prefix: &str) -> Deserializer<'static>
pub fn from_env(prefix: &str) -> Deserializer<'static>
Creates a deserializer for the environment variables with a prefix.
Sourcepub fn from_env_with_config(
prefix: &str,
config: DeserializerConfig,
) -> Deserializer<'static>
pub fn from_env_with_config( prefix: &str, config: DeserializerConfig, ) -> Deserializer<'static>
Creates a deserializer for the environment variables with a prefix and the given configuration.
Variables with names that are not valid unicode are skipped unless they start with the prefix, which is an error. Values that are not valid unicode are passed on as bytes on Unix and are an error on other platforms.
Source§impl<'a> Deserializer<'a>
impl<'a> Deserializer<'a>
Sourcepub fn from_vars<I, K, V>(prefix: &str, vars: I) -> Deserializer<'a>
pub fn from_vars<I, K, V>(prefix: &str, vars: I) -> Deserializer<'a>
Creates a deserializer for the given variables with a prefix.
The variables are name-value pairs, for instance from
std::env::vars or a file. Values that are given borrowed are
passed on borrowed.
Sourcepub fn from_vars_with_config<I, K, V>(
prefix: &str,
vars: I,
config: DeserializerConfig,
) -> Deserializer<'a>
pub fn from_vars_with_config<I, K, V>( prefix: &str, vars: I, config: DeserializerConfig, ) -> Deserializer<'a>
Creates a deserializer for the given variables with a prefix and the given configuration.
Sourcepub fn config(&self) -> &DeserializerConfig
pub fn config(&self) -> &DeserializerConfig
Returns the configuration.
Sourcepub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>
pub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>
Deserializes the variables.
To configure the deserialization (for instance to add layers) use
deserialize_with.
Sourcepub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
pub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
Deserializes the variables with a configured driver.
The callback is invoked with the driver before the value is
deserialized, for instance to add Layers.
Sourcepub fn drive(
&mut self,
driver: &mut DeserializeDriver<'_, 'a>,
) -> Result<(), Error>
pub fn drive( &mut self, driver: &mut DeserializeDriver<'_, 'a>, ) -> Result<(), Error>
Feeds the events of the variables into the given driver.
The variables are a map (see the crate documentation). All
names are checked before the first event is emitted. The name of
the variable an event comes from is attached to it and to the errors
it causes (see EnvVar). Values that are given borrowed are passed
on borrowed.
The context of the configuration is given to the driver (values that
the context of the driver has take precedence, see
DeserializeDriver::set_default_context).