Skip to main content

Encoder

Struct Encoder 

Source
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>

Source

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

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

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

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

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

pub fn encode<T>(&mut self, value: &T) -> Result<()>
where T: Serialize + ?Sized,

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

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

Trait Implementations§

Source§

impl<W: Write> Debug for Encoder<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<W> Freeze for Encoder<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for Encoder<W>
where W: RefUnwindSafe,

§

impl<W> Send for Encoder<W>
where W: Send,

§

impl<W> Sync for Encoder<W>
where W: Sync,

§

impl<W> Unpin for Encoder<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for Encoder<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for Encoder<W>
where W: UnwindSafe,

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.