pub struct Deserializer<'a> { /* private fields */ }Expand description
Deserializes INI files.
Most of the time the from_str and
from_slice 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_path::{Path, PathLayer};
use deser_ini::Deserializer;
#[derive(Debug, deser::Deserialize)]
struct Config {
server: Server,
}
#[derive(Debug, deser::Deserialize)]
struct Server {
port: u16,
}
let err = Deserializer::from_str("[server]\nport = http\n")
.deserialize_with::<Config, _>(|driver| {
driver.push_layer(PathLayer::new())
})
.unwrap_err();
assert_eq!(err.message(), "invalid value \"http\", expected u16");
assert_eq!(err.attachment::<Path>().unwrap().to_string(), "server.port");
assert_eq!(err.line(), Some(2));Implementations§
Source§impl<'a> Deserializer<'a>
impl<'a> Deserializer<'a>
Sourcepub fn from_str(input: &'a str) -> Deserializer<'a>
pub fn from_str(input: &'a str) -> Deserializer<'a>
Creates a new deserializer for a string.
Sourcepub fn from_str_with_config(
input: &'a str,
config: DeserializerConfig,
) -> Deserializer<'a>
pub fn from_str_with_config( input: &'a str, config: DeserializerConfig, ) -> Deserializer<'a>
Creates a new deserializer for a string with the given configuration.
Sourcepub fn from_slice(input: &'a [u8]) -> Deserializer<'a>
pub fn from_slice(input: &'a [u8]) -> Deserializer<'a>
Creates a new deserializer for a byte slice.
The input must be UTF-8 (a byte order mark is skipped), otherwise deserializing fails.
Sourcepub fn from_slice_with_config(
input: &'a [u8],
config: DeserializerConfig,
) -> Deserializer<'a>
pub fn from_slice_with_config( input: &'a [u8], config: DeserializerConfig, ) -> Deserializer<'a>
Creates a new deserializer for a byte slice with 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 input.
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 input 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>
Parses the input and feeds the events into the given driver.
The whole input is parsed before the first event is emitted, so
malformed input is reported before any value is deserialized. Keys
and values that are not changed (by quotes, escapes, continuation
lines or lowercasing) are passed on borrowed from the input (see
emit_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).