creator_plist/
ser.rs

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