pub struct Decoder<R> { /* private fields */ }Expand description
Reads one property-list document from a reader, auto-detecting its
Format.
The first decode call buffers the reader to end of input; decoding works
over that buffer, so the reader needs no Seek bound and repeated decode
calls re-run detection over the same bytes, returning equal values for
every format. Decode memory is proportional to the input size.
§Examples
use apple_plist::{Decoder, Format};
let mut decoder = Decoder::new(&b"(1,2,3)"[..]);
assert_eq!(decoder.format(), None);
let value = decoder.decode_value()?;
assert_eq!(value.as_array().map(Vec::len), Some(3));
assert_eq!(decoder.format(), Some(Format::OpenStep));Implementations§
Source§impl<R: Read> Decoder<R>
impl<R: Read> Decoder<R>
Sourcepub const fn new(reader: R) -> Self
pub const fn new(reader: R) -> Self
Creates a decoder over reader; no I/O happens until the first
decode call.
Sourcepub const fn format(&self) -> Option<Format>
pub const fn format(&self) -> Option<Format>
The format detected by the most recent successful parse, or None
if no parse has succeeded yet.
The format is recorded before the value maps into the target type,
so a failed decode whose document parsed still
reports it.
Sourcepub fn decode_value(&mut self) -> Result<Value>
pub fn decode_value(&mut self) -> Result<Value>
Decodes the buffered document into a Value tree.
§Errors
Returns Error::Io when buffering the reader
fails, and otherwise whatever the detection ladder reports:
Error::Parse for malformed documents,
Error::MaxDepthExceeded for
hostile nesting, and
Error::InvalidPlist /
Error::FeatureDisabled in builds
whose codec features are compiled out.
Sourcepub fn decode<T: DeserializeOwned>(&mut self) -> Result<T>
pub fn decode<T: DeserializeOwned>(&mut self) -> Result<T>
Decodes the buffered document into any DeserializeOwned type.
When detection reports Format::OpenStep — a format that can only
store strings — the mapping coerces strings into requested integers,
floats, booleans, and dates, the codec’s lax mode.
§Errors
Everything decode_value can return, plus the
mapping failures of from_value.
§Examples
use apple_plist::Decoder;
let document = b"<?xml version=\"1.0\"?><plist><integer>42</integer></plist>";
let answer: i64 = Decoder::new(&document[..]).decode()?;
assert_eq!(answer, 42);