plist/
ser.rs

1use serde::ser;
2use std::{
3    borrow::Cow,
4    fmt::Display,
5    fs::File,
6    io::{BufWriter, Write},
7    mem,
8    path::Path,
9};
10
11use crate::{
12    date::serde_impls::DATE_NEWTYPE_STRUCT_NAME,
13    error::{self, Error, ErrorKind},
14    stream::{self, Writer},
15    uid::serde_impls::UID_NEWTYPE_STRUCT_NAME,
16    Date, Integer, Uid, Value, XmlWriteOptions,
17};
18
19#[doc(hidden)]
20impl ser::Error for Error {
21    fn custom<T: Display>(msg: T) -> Self {
22        ErrorKind::Serde(msg.to_string()).without_position()
23    }
24}
25
26enum OptionMode {
27    Root,
28    StructField(&'static str),
29    StructFieldNameWritten,
30    Explicit,
31}
32
33/// A structure that serializes Rust values plist event streams.
34pub struct Serializer<W: Writer> {
35    writer: W,
36    option_mode: OptionMode,
37}
38
39impl<W: Writer> Serializer<W> {
40    pub fn new(writer: W) -> Serializer<W> {
41        Serializer {
42            writer,
43            option_mode: OptionMode::Root,
44        }
45    }
46
47    pub fn into_inner(self) -> W {
48        self.writer
49    }
50
51    fn serialize_with_option_mode<T: ?Sized + ser::Serialize>(
52        &mut self,
53        option_mode: OptionMode,
54        value: &T,
55    ) -> Result<(), Error> {
56        let prev_option_mode = mem::replace(&mut self.option_mode, option_mode);
57        let result = value.serialize(&mut *self);
58        self.option_mode = prev_option_mode;
59        result
60    }
61
62    fn maybe_write_pending_struct_field_name(&mut self) -> Result<(), Error> {
63        if let OptionMode::StructField(field_name) = self.option_mode {
64            self.option_mode = OptionMode::StructFieldNameWritten;
65            self.writer.write_string(Cow::Borrowed(field_name))?;
66        }
67        Ok(())
68    }
69
70    fn write_start_array(&mut self, len: Option<u64>) -> Result<(), Error> {
71        self.maybe_write_pending_struct_field_name()?;
72        self.writer.write_start_array(len)
73    }
74
75    fn write_start_dictionary(&mut self, len: Option<u64>) -> Result<(), Error> {
76        self.maybe_write_pending_struct_field_name()?;
77        self.writer.write_start_dictionary(len)
78    }
79
80    fn write_end_collection(&mut self) -> Result<(), Error> {
81        self.maybe_write_pending_struct_field_name()?;
82        self.writer.write_end_collection()
83    }
84
85    fn write_boolean(&mut self, value: bool) -> Result<(), Error> {
86        self.maybe_write_pending_struct_field_name()?;
87        self.writer.write_boolean(value)
88    }
89
90    fn write_data(&mut self, value: Cow<[u8]>) -> Result<(), Error> {
91        self.maybe_write_pending_struct_field_name()?;
92        self.writer.write_data(value)
93    }
94
95    fn write_date(&mut self, value: Date) -> Result<(), Error> {
96        self.maybe_write_pending_struct_field_name()?;
97        self.writer.write_date(value)
98    }
99
100    fn write_integer(&mut self, value: Integer) -> Result<(), Error> {
101        self.maybe_write_pending_struct_field_name()?;
102        self.writer.write_integer(value)
103    }
104
105    fn write_real(&mut self, value: f64) -> Result<(), Error> {
106        self.maybe_write_pending_struct_field_name()?;
107        self.writer.write_real(value)
108    }
109
110    fn write_string<'a, T: Into<Cow<'a, str>>>(&mut self, value: T) -> Result<(), Error> {
111        self.maybe_write_pending_struct_field_name()?;
112        self.writer.write_string(value.into())
113    }
114
115    fn write_uid(&mut self, value: Uid) -> Result<(), Error> {
116        self.maybe_write_pending_struct_field_name()?;
117        self.writer.write_uid(value)
118    }
119}
120
121impl<'a, W: Writer> ser::Serializer for &'a mut Serializer<W> {
122    type Ok = ();
123    type Error = Error;
124
125    type SerializeSeq = Compound<'a, W>;
126    type SerializeTuple = Compound<'a, W>;
127    type SerializeTupleStruct = Compound<'a, W>;
128    type SerializeTupleVariant = Compound<'a, W>;
129    type SerializeMap = Compound<'a, W>;
130    type SerializeStruct = Compound<'a, W>;
131    type SerializeStructVariant = Compound<'a, W>;
132
133    fn serialize_bool(self, v: bool) -> Result<(), Self::Error> {
134        self.write_boolean(v)
135    }
136
137    fn serialize_i8(self, v: i8) -> Result<(), Error> {
138        self.serialize_i64(v.into())
139    }
140
141    fn serialize_i16(self, v: i16) -> Result<(), Error> {
142        self.serialize_i64(v.into())
143    }
144
145    fn serialize_i32(self, v: i32) -> Result<(), Error> {
146        self.serialize_i64(v.into())
147    }
148
149    fn serialize_i64(self, v: i64) -> Result<(), Self::Error> {
150        self.write_integer(v.into())
151    }
152
153    fn serialize_u8(self, v: u8) -> Result<(), Error> {
154        self.serialize_u64(v.into())
155    }
156
157    fn serialize_u16(self, v: u16) -> Result<(), Error> {
158        self.serialize_u64(v.into())
159    }
160
161    fn serialize_u32(self, v: u32) -> Result<(), Error> {
162        self.serialize_u64(v.into())
163    }
164
165    fn serialize_u64(self, v: u64) -> Result<(), Self::Error> {
166        self.write_integer(v.into())
167    }
168
169    fn serialize_f32(self, v: f32) -> Result<(), Error> {
170        self.serialize_f64(v.into())
171    }
172
173    fn serialize_f64(self, v: f64) -> Result<(), Error> {
174        self.write_real(v)
175    }
176
177    fn serialize_char(self, v: char) -> Result<(), Self::Error> {
178        let mut buf = [0; 4];
179        let v = v.encode_utf8(&mut buf);
180        self.write_string(&*v)
181    }
182
183    fn serialize_str(self, v: &str) -> Result<(), Error> {
184        self.write_string(v)
185    }
186
187    fn serialize_bytes(self, v: &[u8]) -> Result<(), Error> {
188        self.write_data(Cow::Borrowed(v))
189    }
190
191    fn serialize_none(self) -> Result<(), Error> {
192        match self.option_mode {
193            OptionMode::Root | OptionMode::StructField(_) => (),
194            OptionMode::StructFieldNameWritten => unreachable!(),
195            OptionMode::Explicit => {
196                self.write_start_dictionary(Some(1))?;
197                self.write_string("None")?;
198                self.serialize_unit()?;
199                self.write_end_collection()?;
200            }
201        }
202        Ok(())
203    }
204
205    fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<(), Error> {
206        match self.option_mode {
207            OptionMode::Root => self.serialize_with_option_mode(OptionMode::Explicit, value)?,
208            OptionMode::StructField(field_name) => {
209                self.option_mode = OptionMode::StructFieldNameWritten;
210                self.write_string(field_name)?;
211                self.serialize_with_option_mode(OptionMode::Explicit, value)?;
212            }
213            OptionMode::StructFieldNameWritten => unreachable!(),
214            OptionMode::Explicit => {
215                self.write_start_dictionary(Some(1))?;
216                self.write_string("Some")?;
217                value.serialize(&mut *self)?;
218                self.write_end_collection()?;
219            }
220        }
221        Ok(())
222    }
223
224    fn serialize_unit(self) -> Result<(), Error> {
225        self.write_string("")
226    }
227
228    fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Error> {
229        self.serialize_unit()
230    }
231
232    fn serialize_unit_variant(
233        self,
234        _name: &'static str,
235        _variant_index: u32,
236        variant: &'static str,
237    ) -> Result<(), Error> {
238        // `plist` since v1.1 serialises unit enum variants as plain strings.
239        self.write_string(variant)
240    }
241
242    fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
243        self,
244        name: &'static str,
245        value: &T,
246    ) -> Result<(), Error> {
247        match name {
248            DATE_NEWTYPE_STRUCT_NAME => value.serialize(DateSerializer { ser: &mut *self }),
249            UID_NEWTYPE_STRUCT_NAME => value.serialize(UidSerializer { ser: &mut *self }),
250            _ => value.serialize(self),
251        }
252    }
253
254    fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
255        self,
256        _name: &'static str,
257        _variant_index: u32,
258        variant: &'static str,
259        value: &T,
260    ) -> Result<(), Error> {
261        self.write_start_dictionary(Some(1))?;
262        self.write_string(variant)?;
263        value.serialize(&mut *self)?;
264        self.write_end_collection()
265    }
266
267    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
268        let len = len.map(|len| len as u64);
269        self.write_start_array(len)?;
270        Ok(Compound { ser: self })
271    }
272
273    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Error> {
274        self.serialize_seq(Some(len))
275    }
276
277    fn serialize_tuple_struct(
278        self,
279        _name: &'static str,
280        len: usize,
281    ) -> Result<Self::SerializeTupleStruct, Error> {
282        self.serialize_tuple(len)
283    }
284
285    fn serialize_tuple_variant(
286        self,
287        _name: &'static str,
288        _variant_index: u32,
289        variant: &'static str,
290        len: usize,
291    ) -> Result<Self::SerializeTupleVariant, Error> {
292        self.write_start_dictionary(Some(1))?;
293        self.write_string(variant)?;
294        self.serialize_tuple(len)
295    }
296
297    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Error> {
298        let len = len.map(|len| len as u64);
299        self.write_start_dictionary(len)?;
300        Ok(Compound { ser: self })
301    }
302
303    fn serialize_struct(
304        self,
305        _name: &'static str,
306        _len: usize,
307    ) -> Result<Self::SerializeStruct, Error> {
308        // The number of struct fields is not known as fields with None values are ignored.
309        self.serialize_map(None)
310    }
311
312    fn serialize_struct_variant(
313        self,
314        name: &'static str,
315        _variant_index: u32,
316        variant: &'static str,
317        len: usize,
318    ) -> Result<Self::SerializeStructVariant, Error> {
319        self.write_start_dictionary(Some(1))?;
320        self.write_string(variant)?;
321        self.serialize_struct(name, len)
322    }
323}
324
325struct DateSerializer<'a, W: 'a + Writer> {
326    ser: &'a mut Serializer<W>,
327}
328
329impl<'a, W: Writer> DateSerializer<'a, W> {
330    fn expecting_date_error(&self) -> Error {
331        ser::Error::custom("plist date string expected")
332    }
333}
334
335impl<'a, W: Writer> ser::Serializer for DateSerializer<'a, W> {
336    type Ok = ();
337    type Error = Error;
338
339    type SerializeSeq = ser::Impossible<(), Error>;
340    type SerializeTuple = ser::Impossible<(), Error>;
341    type SerializeTupleStruct = ser::Impossible<(), Error>;
342    type SerializeTupleVariant = ser::Impossible<(), Error>;
343    type SerializeMap = ser::Impossible<(), Error>;
344    type SerializeStruct = ser::Impossible<(), Error>;
345    type SerializeStructVariant = ser::Impossible<(), Error>;
346
347    fn serialize_bool(self, _: bool) -> Result<(), Error> {
348        Err(self.expecting_date_error())
349    }
350
351    fn serialize_i8(self, _: i8) -> Result<(), Error> {
352        Err(self.expecting_date_error())
353    }
354
355    fn serialize_i16(self, _: i16) -> Result<(), Error> {
356        Err(self.expecting_date_error())
357    }
358
359    fn serialize_i32(self, _: i32) -> Result<(), Error> {
360        Err(self.expecting_date_error())
361    }
362
363    fn serialize_i64(self, _: i64) -> Result<(), Error> {
364        Err(self.expecting_date_error())
365    }
366
367    fn serialize_u8(self, _: u8) -> Result<(), Error> {
368        Err(self.expecting_date_error())
369    }
370
371    fn serialize_u16(self, _: u16) -> Result<(), Error> {
372        Err(self.expecting_date_error())
373    }
374
375    fn serialize_u32(self, _: u32) -> Result<(), Error> {
376        Err(self.expecting_date_error())
377    }
378
379    fn serialize_u64(self, _: u64) -> Result<(), Error> {
380        Err(self.expecting_date_error())
381    }
382
383    fn serialize_f32(self, _: f32) -> Result<(), Error> {
384        Err(self.expecting_date_error())
385    }
386
387    fn serialize_f64(self, _: f64) -> Result<(), Error> {
388        Err(self.expecting_date_error())
389    }
390
391    fn serialize_char(self, _: char) -> Result<(), Error> {
392        Err(self.expecting_date_error())
393    }
394
395    fn serialize_str(self, v: &str) -> Result<(), Error> {
396        let date = Date::from_xml_format(v).map_err(|_| self.expecting_date_error())?;
397        self.ser.write_date(date)
398    }
399
400    fn serialize_bytes(self, _: &[u8]) -> Result<(), Error> {
401        Err(self.expecting_date_error())
402    }
403
404    fn serialize_none(self) -> Result<(), Error> {
405        Err(self.expecting_date_error())
406    }
407
408    fn serialize_some<T: ?Sized + ser::Serialize>(self, _: &T) -> Result<(), Error> {
409        Err(self.expecting_date_error())
410    }
411
412    fn serialize_unit(self) -> Result<(), Error> {
413        Err(self.expecting_date_error())
414    }
415
416    fn serialize_unit_struct(self, _: &'static str) -> Result<(), Error> {
417        Err(self.expecting_date_error())
418    }
419
420    fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<(), Error> {
421        Err(self.expecting_date_error())
422    }
423
424    fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
425        self,
426        _: &'static str,
427        _: &T,
428    ) -> Result<(), Error> {
429        Err(self.expecting_date_error())
430    }
431
432    fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
433        self,
434        _: &'static str,
435        _: u32,
436        _: &'static str,
437        _: &T,
438    ) -> Result<(), Error> {
439        Err(self.expecting_date_error())
440    }
441
442    fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Error> {
443        Err(self.expecting_date_error())
444    }
445
446    fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Error> {
447        Err(self.expecting_date_error())
448    }
449
450    fn serialize_tuple_struct(
451        self,
452        _: &'static str,
453        _: usize,
454    ) -> Result<Self::SerializeTupleStruct, Error> {
455        Err(self.expecting_date_error())
456    }
457
458    fn serialize_tuple_variant(
459        self,
460        _: &'static str,
461        _: u32,
462        _: &'static str,
463        _: usize,
464    ) -> Result<Self::SerializeTupleVariant, Error> {
465        Err(self.expecting_date_error())
466    }
467
468    fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Error> {
469        Err(self.expecting_date_error())
470    }
471
472    fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct, Error> {
473        Err(self.expecting_date_error())
474    }
475
476    fn serialize_struct_variant(
477        self,
478        _: &'static str,
479        _: u32,
480        _: &'static str,
481        _: usize,
482    ) -> Result<Self::SerializeStructVariant, Error> {
483        Err(self.expecting_date_error())
484    }
485}
486
487struct UidSerializer<'a, W: 'a + Writer> {
488    ser: &'a mut Serializer<W>,
489}
490
491impl<'a, W: Writer> UidSerializer<'a, W> {
492    fn expecting_uid_error(&self) -> Error {
493        ser::Error::custom("plist uid expected")
494    }
495}
496
497impl<'a, W: Writer> ser::Serializer for UidSerializer<'a, W> {
498    type Ok = ();
499    type Error = Error;
500
501    type SerializeSeq = ser::Impossible<(), Error>;
502    type SerializeTuple = ser::Impossible<(), Error>;
503    type SerializeTupleStruct = ser::Impossible<(), Error>;
504    type SerializeTupleVariant = ser::Impossible<(), Error>;
505    type SerializeMap = ser::Impossible<(), Error>;
506    type SerializeStruct = ser::Impossible<(), Error>;
507    type SerializeStructVariant = ser::Impossible<(), Error>;
508
509    fn serialize_bool(self, _: bool) -> Result<(), Error> {
510        Err(self.expecting_uid_error())
511    }
512
513    fn serialize_i8(self, _: i8) -> Result<(), Error> {
514        Err(self.expecting_uid_error())
515    }
516
517    fn serialize_i16(self, _: i16) -> Result<(), Error> {
518        Err(self.expecting_uid_error())
519    }
520
521    fn serialize_i32(self, _: i32) -> Result<(), Error> {
522        Err(self.expecting_uid_error())
523    }
524
525    fn serialize_i64(self, _: i64) -> Result<(), Error> {
526        Err(self.expecting_uid_error())
527    }
528
529    fn serialize_u8(self, _: u8) -> Result<(), Error> {
530        Err(self.expecting_uid_error())
531    }
532
533    fn serialize_u16(self, _: u16) -> Result<(), Error> {
534        Err(self.expecting_uid_error())
535    }
536
537    fn serialize_u32(self, _: u32) -> Result<(), Error> {
538        Err(self.expecting_uid_error())
539    }
540
541    fn serialize_u64(self, v: u64) -> Result<(), Error> {
542        self.ser.write_uid(Uid::new(v))
543    }
544
545    fn serialize_f32(self, _: f32) -> Result<(), Error> {
546        Err(self.expecting_uid_error())
547    }
548
549    fn serialize_f64(self, _: f64) -> Result<(), Error> {
550        Err(self.expecting_uid_error())
551    }
552
553    fn serialize_char(self, _: char) -> Result<(), Error> {
554        Err(self.expecting_uid_error())
555    }
556
557    fn serialize_str(self, _: &str) -> Result<(), Error> {
558        Err(self.expecting_uid_error())
559    }
560
561    fn serialize_bytes(self, _: &[u8]) -> Result<(), Error> {
562        Err(self.expecting_uid_error())
563    }
564
565    fn serialize_none(self) -> Result<(), Error> {
566        Err(self.expecting_uid_error())
567    }
568
569    fn serialize_some<T: ?Sized + ser::Serialize>(self, _: &T) -> Result<(), Error> {
570        Err(self.expecting_uid_error())
571    }
572
573    fn serialize_unit(self) -> Result<(), Error> {
574        Err(self.expecting_uid_error())
575    }
576
577    fn serialize_unit_struct(self, _: &'static str) -> Result<(), Error> {
578        Err(self.expecting_uid_error())
579    }
580
581    fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<(), Error> {
582        Err(self.expecting_uid_error())
583    }
584
585    fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
586        self,
587        _: &'static str,
588        _: &T,
589    ) -> Result<(), Error> {
590        Err(self.expecting_uid_error())
591    }
592
593    fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
594        self,
595        _: &'static str,
596        _: u32,
597        _: &'static str,
598        _: &T,
599    ) -> Result<(), Error> {
600        Err(self.expecting_uid_error())
601    }
602
603    fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Error> {
604        Err(self.expecting_uid_error())
605    }
606
607    fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Error> {
608        Err(self.expecting_uid_error())
609    }
610
611    fn serialize_tuple_struct(
612        self,
613        _: &'static str,
614        _: usize,
615    ) -> Result<Self::SerializeTupleStruct, Error> {
616        Err(self.expecting_uid_error())
617    }
618
619    fn serialize_tuple_variant(
620        self,
621        _: &'static str,
622        _: u32,
623        _: &'static str,
624        _: usize,
625    ) -> Result<Self::SerializeTupleVariant, Error> {
626        Err(self.expecting_uid_error())
627    }
628
629    fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Error> {
630        Err(self.expecting_uid_error())
631    }
632
633    fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct, Error> {
634        Err(self.expecting_uid_error())
635    }
636
637    fn serialize_struct_variant(
638        self,
639        _: &'static str,
640        _: u32,
641        _: &'static str,
642        _: usize,
643    ) -> Result<Self::SerializeStructVariant, Error> {
644        Err(self.expecting_uid_error())
645    }
646}
647
648#[doc(hidden)]
649pub struct Compound<'a, W: 'a + Writer> {
650    ser: &'a mut Serializer<W>,
651}
652
653impl<'a, W: Writer> ser::SerializeSeq for Compound<'a, W> {
654    type Ok = ();
655    type Error = Error;
656
657    fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
658        self.ser
659            .serialize_with_option_mode(OptionMode::Explicit, value)
660    }
661
662    fn end(self) -> Result<Self::Ok, Self::Error> {
663        self.ser.write_end_collection()
664    }
665}
666
667impl<'a, W: Writer> ser::SerializeTuple for Compound<'a, W> {
668    type Ok = ();
669    type Error = Error;
670
671    fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
672        self.ser
673            .serialize_with_option_mode(OptionMode::Explicit, value)
674    }
675
676    fn end(self) -> Result<(), Error> {
677        self.ser.write_end_collection()
678    }
679}
680
681impl<'a, W: Writer> ser::SerializeTupleStruct for Compound<'a, W> {
682    type Ok = ();
683    type Error = Error;
684
685    fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
686        self.ser
687            .serialize_with_option_mode(OptionMode::Explicit, value)
688    }
689
690    fn end(self) -> Result<(), Error> {
691        self.ser.write_end_collection()
692    }
693}
694
695impl<'a, W: Writer> ser::SerializeTupleVariant for Compound<'a, W> {
696    type Ok = ();
697    type Error = Error;
698
699    fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
700        self.ser
701            .serialize_with_option_mode(OptionMode::Explicit, value)
702    }
703
704    fn end(self) -> Result<Self::Ok, Self::Error> {
705        self.ser.write_end_collection()?;
706        self.ser.write_end_collection()
707    }
708}
709
710impl<'a, W: Writer> ser::SerializeMap for Compound<'a, W> {
711    type Ok = ();
712    type Error = Error;
713
714    fn serialize_key<T: ?Sized + ser::Serialize>(&mut self, key: &T) -> Result<(), Error> {
715        self.ser
716            .serialize_with_option_mode(OptionMode::Explicit, key)
717    }
718
719    fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
720        self.ser
721            .serialize_with_option_mode(OptionMode::Explicit, value)
722    }
723
724    fn end(self) -> Result<Self::Ok, Self::Error> {
725        self.ser.write_end_collection()
726    }
727}
728
729impl<'a, W: Writer> ser::SerializeStruct for Compound<'a, W> {
730    type Ok = ();
731    type Error = Error;
732
733    fn serialize_field<T: ?Sized + ser::Serialize>(
734        &mut self,
735        key: &'static str,
736        value: &T,
737    ) -> Result<(), Error> {
738        // We don't want to serialize None if the Option is a struct field as this is how null
739        // fields are represented in plists.
740        self.ser
741            .serialize_with_option_mode(OptionMode::StructField(key), value)
742    }
743
744    fn end(self) -> Result<(), Error> {
745        self.ser.write_end_collection()
746    }
747}
748
749impl<'a, W: Writer> ser::SerializeStructVariant for Compound<'a, W> {
750    type Ok = ();
751    type Error = Error;
752
753    fn serialize_field<T: ?Sized + ser::Serialize>(
754        &mut self,
755        key: &'static str,
756        value: &T,
757    ) -> Result<(), Error> {
758        self.ser
759            .serialize_with_option_mode(OptionMode::StructField(key), value)
760    }
761
762    fn end(self) -> Result<(), Error> {
763        self.ser.write_end_collection()?;
764        self.ser.write_end_collection()
765    }
766}
767
768/// Serializes the given data structure to a file as a binary encoded plist.
769pub fn to_file_binary<P: AsRef<Path>, T: ser::Serialize>(path: P, value: &T) -> Result<(), Error> {
770    let mut file = File::create(path).map_err(error::from_io_without_position)?;
771    to_writer_binary(BufWriter::new(&mut file), value)?;
772    file.sync_all().map_err(error::from_io_without_position)?;
773    Ok(())
774}
775
776/// Serializes the given data structure to a file as an XML encoded plist.
777pub fn to_file_xml<P: AsRef<Path>, T: ser::Serialize>(path: P, value: &T) -> Result<(), Error> {
778    let mut file = File::create(path).map_err(error::from_io_without_position)?;
779    to_writer_xml(BufWriter::new(&mut file), value)?;
780    file.sync_all().map_err(error::from_io_without_position)?;
781    Ok(())
782}
783
784/// Serializes the given data structure to a byte stream as a binary encoded plist.
785pub fn to_writer_binary<W: Write, T: ser::Serialize>(writer: W, value: &T) -> Result<(), Error> {
786    let writer = stream::BinaryWriter::new(writer);
787    let mut ser = Serializer::new(writer);
788    value.serialize(&mut ser)
789}
790
791/// Serializes the given data structure to a byte stream as an XML encoded plist.
792pub fn to_writer_xml<W: Write, T: ser::Serialize>(writer: W, value: &T) -> Result<(), Error> {
793    to_writer_xml_with_options(writer, value, &XmlWriteOptions::default())
794}
795
796/// Serializes to a byte stream as an XML encoded plist, using custom [`XmlWriteOptions`].
797pub fn to_writer_xml_with_options<W: Write, T: ser::Serialize>(
798    writer: W,
799    value: &T,
800    options: &XmlWriteOptions,
801) -> Result<(), Error> {
802    let writer = stream::XmlWriter::new_with_options(writer, options);
803    let mut ser = Serializer::new(writer);
804    value.serialize(&mut ser)
805}
806
807/// Converts a `T` into a [`Value`] which can represent any valid plist.
808pub fn to_value<T: ser::Serialize>(value: &T) -> Result<Value, Error> {
809    let writer = crate::value::Builder::default();
810    let mut ser = Serializer::new(writer);
811    value.serialize(&mut ser)?;
812    ser.into_inner().finish()
813}