impl Value { /// Reads a `Value` from a plist file of any encoding. pubfn from_file<P: AsRef<Path>>(path: P) -> Result<Value, Error> { let file = File::open(path).map_err(error::from_io_without_position)?;
Value::from_reader(BufReader::new(file))
}
/// Reads a `Value` from a seekable byte stream containing a plist of any encoding. pubfn from_reader<R: Read + Seek>(reader: R) -> Result<Value, Error> { let reader = Reader::new(reader);
Value::from_events(reader)
}
/// Reads a `Value` from a seekable byte stream containing an XML encoded plist. pubfn from_reader_xml<R: Read>(reader: R) -> Result<Value, Error> { let reader = XmlReader::new(reader);
Value::from_events(reader)
}
/// Serializes a `Value` to a file as a binary encoded plist. pubfn to_file_binary<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> { letmut file = File::create(path).map_err(error::from_io_without_position)?; self.to_writer_binary(BufWriter::new(&mut file))?;
file.sync_all().map_err(error::from_io_without_position)?;
Ok(())
}
/// Serializes a `Value` to a file as an XML encoded plist. pubfn to_file_xml<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> { letmut file = File::create(path).map_err(error::from_io_without_position)?; self.to_writer_xml(BufWriter::new(&mut file))?;
file.sync_all().map_err(error::from_io_without_position)?;
Ok(())
}
/// Serializes a `Value` to a byte stream as a binary encoded plist. pubfn to_writer_binary<W: Write>(&self, writer: W) -> Result<(), Error> { letmut writer = BinaryWriter::new(writer); self.to_writer_inner(&mut writer)
}
/// Serializes a `Value` to a byte stream as an XML encoded plist. pubfn to_writer_xml<W: Write>(&self, writer: W) -> Result<(), Error> { self.to_writer_xml_with_options(writer, &XmlWriteOptions::default())
}
/// Serializes a `Value` to a stream, using custom [`XmlWriteOptions`]. /// /// If you need to serialize to a file, you must acquire an appropriate /// `Write` handle yourself. /// /// # Examples /// /// ```no_run /// use std::io::{BufWriter, Write}; /// use std::fs::File; /// use plist::{Dictionary, Value, XmlWriteOptions}; /// /// let value: Value = Dictionary::new().into(); /// // .. add some keys & values /// let mut file = File::create("com.example.myPlist.plist").unwrap(); /// let options = XmlWriteOptions::default().indent_string(" "); /// value.to_writer_xml_with_options(BufWriter::new(&mut file), &options).unwrap(); /// file.sync_all().unwrap(); /// ``` pubfn to_writer_xml_with_options<W: Write>(
&self,
writer: W,
options: &XmlWriteOptions,
) -> Result<(), Error> { letmut writer = XmlWriter::new_with_options(writer, options); self.to_writer_inner(&mut writer)
}
fn to_writer_inner(&self, writer: &mutdyn Writer) -> Result<(), Error> { let events = self.events(); for event in events {
writer.write(&event)?;
}
Ok(())
}
/// Builds a single `Value` from an `Event` iterator. /// On success any excess `Event`s will remain in the iterator. #[cfg(feature = "enable_unstable_features_that_may_break_with_minor_version_bumps")] pubfn from_events<T>(events: T) -> Result<Value, Error> where
T: IntoIterator<Item = Result<OwnedEvent, Error>>,
{
Builder::new(events.into_iter()).build()
}
/// Builds a single `Value` from an `Event` iterator. /// On success any excess `Event`s will remain in the iterator. #[cfg(not(feature = "enable_unstable_features_that_may_break_with_minor_version_bumps"))] pub(crate) fn from_events<T>(events: T) -> Result<Value, Error> where
T: IntoIterator<Item = Result<OwnedEvent, Error>>,
{
Builder::new(events.into_iter()).build()
}
/// Converts a `Value` into an `Event` iterator. #[cfg(feature = "enable_unstable_features_that_may_break_with_minor_version_bumps")] #[doc(hidden)] #[deprecated(since = "1.2.0", note = "use Value::events instead")] pubfn into_events(&self) -> Events { self.events()
}
/// Creates an `Event` iterator for this `Value`. #[cfg(not(feature = "enable_unstable_features_that_may_break_with_minor_version_bumps"))] pub(crate) fn events(&self) -> Events {
Events::new(self)
}
/// Creates an `Event` iterator for this `Value`. #[cfg(feature = "enable_unstable_features_that_may_break_with_minor_version_bumps")] pubfn events(&self) -> Events {
Events::new(self)
}
/// If the `Value` is a Array, returns the underlying `Vec`. /// /// Returns `None` otherwise. /// /// This method consumes the `Value`. To get a reference instead, use /// `as_array`. pubfn into_array(self) -> Option<Vec<Value>> { matchself {
Value::Array(dict) => Some(dict),
_ => None,
}
}
/// If the `Value` is an Array, returns the associated `Vec`. /// /// Returns `None` otherwise. pubfn as_array(&self) -> Option<&Vec<Value>> { match *self {
Value::Array(ref array) => Some(array),
_ => None,
}
}
/// If the `Value` is an Array, returns the associated mutable `Vec`. /// /// Returns `None` otherwise. pubfn as_array_mut(&mutself) -> Option<&mut Vec<Value>> { match *self {
Value::Array(refmut array) => Some(array),
_ => None,
}
}
/// If the `Value` is a Dictionary, returns the associated `BTreeMap`. /// /// Returns `None` otherwise. /// /// This method consumes the `Value`. To get a reference instead, use /// `as_dictionary`. pubfn into_dictionary(self) -> Option<Dictionary> { matchself {
Value::Dictionary(dict) => Some(dict),
_ => None,
}
}
/// If the `Value` is a Dictionary, returns the associated `BTreeMap`. /// /// Returns `None` otherwise. pubfn as_dictionary(&self) -> Option<&Dictionary> { match *self {
Value::Dictionary(ref dict) => Some(dict),
_ => None,
}
}
/// If the `Value` is a Dictionary, returns the associated mutable `BTreeMap`. /// /// Returns `None` otherwise. pubfn as_dictionary_mut(&mutself) -> Option<&mut Dictionary> { match *self {
Value::Dictionary(refmut dict) => Some(dict),
_ => None,
}
}
/// If the `Value` is a Boolean, returns the associated `bool`. /// /// Returns `None` otherwise. pubfn as_boolean(&self) -> Option<bool> { match *self {
Value::Boolean(v) => Some(v),
_ => None,
}
}
/// If the `Value` is a Data, returns the underlying `Vec`. /// /// Returns `None` otherwise. /// /// This method consumes the `Value`. If this is not desired, please use /// `as_data` method. pubfn into_data(self) -> Option<Vec<u8>> { matchself {
Value::Data(data) => Some(data),
_ => None,
}
}
/// If the `Value` is a Data, returns the associated `Vec`. /// /// Returns `None` otherwise. pubfn as_data(&self) -> Option<&[u8]> { match *self {
Value::Data(ref data) => Some(data),
_ => None,
}
}
/// If the `Value` is a Date, returns the associated `Date`. /// /// Returns `None` otherwise. pubfn as_date(&self) -> Option<Date> { match *self {
Value::Date(date) => Some(date),
_ => None,
}
}
/// If the `Value` is a Real, returns the associated `f64`. /// /// Returns `None` otherwise. pubfn as_real(&self) -> Option<f64> { match *self {
Value::Real(v) => Some(v),
_ => None,
}
}
/// If the `Value` is a signed Integer, returns the associated `i64`. /// /// Returns `None` otherwise. pubfn as_signed_integer(&self) -> Option<i64> { match *self {
Value::Integer(v) => v.as_signed(),
_ => None,
}
}
/// If the `Value` is an unsigned Integer, returns the associated `u64`. /// /// Returns `None` otherwise. pubfn as_unsigned_integer(&self) -> Option<u64> { match *self {
Value::Integer(v) => v.as_unsigned(),
_ => None,
}
}
/// If the `Value` is a String, returns the underlying `String`. /// /// Returns `None` otherwise. /// /// This method consumes the `Value`. If this is not desired, please use /// `as_string` method. pubfn into_string(self) -> Option<String> { matchself {
Value::String(v) => Some(v),
_ => None,
}
}
/// If the `Value` is a String, returns the associated `str`. /// /// Returns `None` otherwise. pubfn as_string(&self) -> Option<&str> { match *self {
Value::String(ref v) => Some(v),
_ => None,
}
}
/// If the `Value` is a Uid, returns the underlying `Uid`. /// /// Returns `None` otherwise. /// /// This method consumes the `Value`. If this is not desired, please use /// `as_uid` method. pubfn into_uid(self) -> Option<Uid> { matchself {
Value::Uid(u) => Some(u),
_ => None,
}
}
/// If the `Value` is a Uid, returns the associated `Uid`. /// /// Returns `None` otherwise. pubfn as_uid(&self) -> Option<&Uid> { match *self {
Value::Uid(ref u) => Some(u),
_ => None,
}
}
}
#[cfg(feature = "serde")] pubmod serde_impls { use serde::{
de,
de::{EnumAccess, MapAccess, SeqAccess, VariantAccess, Visitor},
ser,
};
fn visit_enum<A>(self, data: A) -> Result<Value, A::Error> where
A: EnumAccess<'de>,
{ let (name, variant) = data.variant::<String>()?; match &*name {
DATE_NEWTYPE_STRUCT_NAME => Ok(Value::Date(variant.newtype_variant()?)),
UID_NEWTYPE_STRUCT_NAME => Ok(Value::Uid(variant.newtype_variant()?)),
_ => Err(de::Error::unknown_variant(
&name,
&[DATE_NEWTYPE_STRUCT_NAME, UID_NEWTYPE_STRUCT_NAME],
)),
}
}
}
// Serde serialisers are encouraged to treat newtype structs as insignificant // wrappers around the data they contain. That means not parsing anything other // than the contained value. Therefore, this should not prevent using `Value` // with other `Serializer`s.
deserializer.deserialize_newtype_struct(VALUE_NEWTYPE_STRUCT_NAME, ValueVisitor)
}
}
}
impl From<Vec<Value>> for Value { fn from(from: Vec<Value>) -> Value {
Value::Array(from)
}
}
impl From<Dictionary> for Value { fn from(from: Dictionary) -> Value {
Value::Dictionary(from)
}
}
impl From<bool> for Value { fn from(from: bool) -> Value {
Value::Boolean(from)
}
}
impl<'a> From<&'a bool> for Value { fn from(from: &'a bool) -> Value {
Value::Boolean(*from)
}
}
impl From<Date> for Value { fn from(from: Date) -> Value {
Value::Date(from)
}
}
impl<'a> From<&'a Date> for Value { fn from(from: &'a Date) -> Value {
Value::Date(*from)
}
}
impl From<f64> for Value { fn from(from: f64) -> Value {
Value::Real(from)
}
}
impl From<f32> for Value { fn from(from: f32) -> Value {
Value::Real(from.into())
}
}
impl From<i64> for Value { fn from(from: i64) -> Value {
Value::Integer(Integer::from(from))
}
}
impl From<i32> for Value { fn from(from: i32) -> Value {
Value::Integer(Integer::from(from))
}
}
impl From<i16> for Value { fn from(from: i16) -> Value {
Value::Integer(Integer::from(from))
}
}
impl From<i8> for Value { fn from(from: i8) -> Value {
Value::Integer(Integer::from(from))
}
}
impl From<u64> for Value { fn from(from: u64) -> Value {
Value::Integer(Integer::from(from))
}
}
impl From<u32> for Value { fn from(from: u32) -> Value {
Value::Integer(Integer::from(from))
}
}
impl From<u16> for Value { fn from(from: u16) -> Value {
Value::Integer(Integer::from(from))
}
}
impl From<u8> for Value { fn from(from: u8) -> Value {
Value::Integer(Integer::from(from))
}
}
impl<'a> From<&'a f64> for Value { fn from(from: &'a f64) -> Value {
Value::Real(*from)
}
}
impl<'a> From<&'a f32> for Value { fn from(from: &'a f32) -> Value {
Value::Real((*from).into())
}
}
impl<'a> From<&'a i64> for Value { fn from(from: &'a i64) -> Value {
Value::Integer(Integer::from(*from))
}
}
impl<'a> From<&'a i32> for Value { fn from(from: &'a i32) -> Value {
Value::Integer(Integer::from(*from))
}
}
impl<'a> From<&'a i16> for Value { fn from(from: &'a i16) -> Value {
Value::Integer(Integer::from(*from))
}
}
impl<'a> From<&'a i8> for Value { fn from(from: &'a i8) -> Value {
Value::Integer(Integer::from(*from))
}
}
impl<'a> From<&'a u64> for Value { fn from(from: &'a u64) -> Value {
Value::Integer(Integer::from(*from))
}
}
impl<'a> From<&'a u32> for Value { fn from(from: &'a u32) -> Value {
Value::Integer(Integer::from(*from))
}
}
impl<'a> From<&'a u16> for Value { fn from(from: &'a u16) -> Value {
Value::Integer((*from).into())
}
}
impl<'a> From<&'a u8> for Value { fn from(from: &'a u8) -> Value {
Value::Integer((*from).into())
}
}
impl From<String> for Value { fn from(from: String) -> Value {
Value::String(from)
}
}
impl<'a> From<&'a str> for Value { fn from(from: &'a str) -> Value {
Value::String(from.into())
}
}
#[test] fn builder() { // Input let events = vec![
StartDictionary(None),
String("Author".into()),
String("William Shakespeare".into()),
String("Lines".into()),
StartArray(None),
String("It is a tale told by an idiot,".into()),
String("Full of sound and fury, signifying nothing.".into()),
EndCollection,
String("Birthdate".into()),
Integer(1564.into()),
String("Height".into()),
Real(1.60),
EndCollection,
];
let builder = Builder::new(events.into_iter().map(|e| Ok(e))); let plist = builder.build();
// Expected output letmut lines = Vec::new();
lines.push(Value::String("It is a tale told by an idiot,".to_owned()));
lines.push(Value::String( "Full of sound and fury, signifying nothing.".to_owned(),
));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.