pub struct Encoder<W: Write> { /* private fields */ }Expand description
Writes property-list documents to a writer in a chosen Format.
Construction is infallible; requesting a format whose cargo feature is
compiled out fails at encode time with
Error::FeatureDisabled. Each successful
encode call writes exactly one complete document; repeated calls append
documents back to back with no separator.
§Examples
use apple_plist::{Encoder, Value};
let mut out = Vec::new();
Encoder::new(&mut out).encode_value(&Value::from(true))?;
assert!(out.ends_with(b"<true/></plist>"));Implementations§
Source§impl<W: Write> Encoder<W>
impl<W: Write> Encoder<W>
Sourcepub const fn new(writer: W) -> Self
pub const fn new(writer: W) -> Self
Creates an encoder for the default format, XML.
§Examples
use apple_plist::{Encoder, Value};
let mut out = Vec::new();
Encoder::new(&mut out).encode_value(&Value::from("hi"))?;
assert!(out.starts_with(b"<?xml"));Sourcepub const fn for_format(writer: W, format: Format) -> Self
pub const fn for_format(writer: W, format: Format) -> Self
Creates an encoder for an explicit format.
§Examples
use apple_plist::{Encoder, Format, Value};
let mut out = Vec::new();
Encoder::for_format(&mut out, Format::OpenStep).encode_value(&Value::from("hi"))?;
assert_eq!(out, b"hi");Sourcepub const fn binary(writer: W) -> Self
pub const fn binary(writer: W) -> Self
Creates an encoder for the binary (bplist00) format.
§Examples
use apple_plist::{Encoder, Value};
let mut out = Vec::new();
Encoder::binary(&mut out).encode_value(&Value::from(7u8))?;
assert!(out.starts_with(b"bplist00"));Sourcepub const fn automatic(writer: W) -> Self
pub const fn automatic(writer: W) -> Self
Creates an encoder that lets the library pick the format — currently binary, the automatic-format default.
§Examples
use apple_plist::{Encoder, Value};
let mut out = Vec::new();
Encoder::automatic(&mut out).encode_value(&Value::from(7u8))?;
assert!(out.starts_with(b"bplist00"));Sourcepub fn set_indent(&mut self, indent: impl Into<String>)
pub fn set_indent(&mut self, indent: impl Into<String>)
Turns on pretty-printing for the XML and text formats; the binary format ignores it.
The string is written verbatim, repeated per nesting depth, and
re-applied on every encode until changed. A non-empty indent also
switches the text formats’ key delimiter from = to =; the empty
string switches pretty-printing back off.
§Examples
use apple_plist::{Encoder, Format, Value};
let value = Value::from_iter([("a".to_owned(), Value::from("b"))]);
let mut out = Vec::new();
let mut encoder = Encoder::for_format(&mut out, Format::OpenStep);
encoder.set_indent("\t");
encoder.encode_value(&value)?;
assert_eq!(out, b"{\n\ta = b;\n}");Sourcepub fn encode<T>(&mut self, value: &T) -> Result<()>
pub fn encode<T>(&mut self, value: &T) -> Result<()>
Serializes value and writes one complete document.
The value is serialized into a Value tree before the format is
consulted, so serialization failures win over format failures.
§Errors
Returns Error::NoRootElement when the
root serializes to nothing (for example None), any error a custom
Serialize implementation reports, and then everything
encode_value can return.
§Examples
use apple_plist::Encoder;
let mut out = Vec::new();
Encoder::new(&mut out).encode("Hello")?;
assert!(out.ends_with(b"<string>Hello</string></plist>"));Sourcepub fn encode_value(&mut self, value: &Value) -> Result<()>
pub fn encode_value(&mut self, value: &Value) -> Result<()>
Writes one complete document holding value.
§Errors
Returns Error::FeatureDisabled when
this encoder’s format is behind a cargo feature that is compiled out
(xml, binary, or openstep — the latter covers both text
formats), Error::Io when the writer fails, and
Error::Message when a binary-format
container holds a NaN real.
§Examples
use apple_plist::{Encoder, Format, Value};
let mut out = Vec::new();
Encoder::for_format(&mut out, Format::GnuStep).encode_value(&Value::from(3u8))?;
assert_eq!(out, b"<*I3>");