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