| 1 |
use serde::{ |
| 2 |
de::{Deserialize, DeserializeOwned}, |
| 3 |
ser::Serialize, |
| 4 |
}; |
| 5 |
use std::{collections::BTreeMap, fmt::Debug, fs::File, io::Cursor, path::Path}; |
| 6 |
|
| 7 |
use crate::{ |
| 8 |
stream::{private::Sealed, Event, OwnedEvent, Writer}, |
| 9 |
Date, Deserializer, Dictionary, Error, Integer, Serializer, Uid, Value, |
| 10 |
}; |
| 11 |
|
| 12 |
struct VecWriter { |
| 13 |
events: Vec<OwnedEvent>, |
| 14 |
} |
| 15 |
|
| 16 |
impl VecWriter { |
| 17 |
pub fn new() -> VecWriter { |
| 18 |
VecWriter { events: Vec::new() } |
| 19 |
} |
| 20 |
|
| 21 |
pub fn into_inner(self) -> Vec<OwnedEvent> { |
| 22 |
self.events |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
impl Writer for VecWriter { |
| 27 |
fn write_start_array(&mut self, len: Option<u64>) -> Result<(), Error> { |
| 28 |
self.events.push(Event::StartArray(len)); |
| 29 |
Ok(()) |
| 30 |
} |
| 31 |
|
| 32 |
fn write_start_dictionary(&mut self, len: Option<u64>) -> Result<(), Error> { |
| 33 |
self.events.push(Event::StartDictionary(len)); |
| 34 |
Ok(()) |
| 35 |
} |
| 36 |
|
| 37 |
fn write_end_collection(&mut self) -> Result<(), Error> { |
| 38 |
self.events.push(Event::EndCollection); |
| 39 |
Ok(()) |
| 40 |
} |
| 41 |
|
| 42 |
fn write_boolean(&mut self, value: bool) -> Result<(), Error> { |
| 43 |
self.events.push(Event::Boolean(value)); |
| 44 |
Ok(()) |
| 45 |
} |
| 46 |
|
| 47 |
fn write_data(&mut self, value: &[u8]) -> Result<(), Error> { |
| 48 |
self.events.push(Event::Data(value.to_owned().into())); |
| 49 |
Ok(()) |
| 50 |
} |
| 51 |
|
| 52 |
fn write_date(&mut self, value: Date) -> Result<(), Error> { |
| 53 |
self.events.push(Event::Date(value)); |
| 54 |
Ok(()) |
| 55 |
} |
| 56 |
|
| 57 |
fn write_integer(&mut self, value: Integer) -> Result<(), Error> { |
| 58 |
self.events.push(Event::Integer(value)); |
| 59 |
Ok(()) |
| 60 |
} |
| 61 |
|
| 62 |
fn write_real(&mut self, value: f64) -> Result<(), Error> { |
| 63 |
self.events.push(Event::Real(value)); |
| 64 |
Ok(()) |
| 65 |
} |
| 66 |
|
| 67 |
fn write_string(&mut self, value: &str) -> Result<(), Error> { |
| 68 |
self.events.push(Event::String(value.to_owned().into())); |
| 69 |
Ok(()) |
| 70 |
} |
| 71 |
|
| 72 |
fn write_uid(&mut self, value: Uid) -> Result<(), Error> { |
| 73 |
self.events.push(Event::Uid(value)); |
| 74 |
Ok(()) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl Sealed for VecWriter {} |
| 79 |
|
| 80 |
fn new_serializer() -> Serializer<VecWriter> { |
| 81 |
Serializer::new(VecWriter::new()) |
| 82 |
} |
| 83 |
|
| 84 |
fn new_deserializer(events: Vec<OwnedEvent>) -> Deserializer<Vec<Result<OwnedEvent, Error>>> { |
| 85 |
let result_events = events.into_iter().map(Ok).collect(); |
| 86 |
Deserializer::new(result_events) |
| 87 |
} |
| 88 |
|
| 89 |
fn assert_roundtrip<T>(obj: T, comparison: Option<&[Event]>) |
| 90 |
where |
| 91 |
T: Debug + DeserializeOwned + PartialEq + Serialize, |
| 92 |
{ |
| 93 |
let mut se = new_serializer(); |
| 94 |
|
| 95 |
obj.serialize(&mut se).unwrap(); |
| 96 |
|
| 97 |
let events = se.into_inner().into_inner(); |
| 98 |
|
| 99 |
if let Some(comparison) = comparison { |
| 100 |
assert_eq!(&events[..], comparison); |
| 101 |
} |
| 102 |
|
| 103 |
let mut de = new_deserializer(events); |
| 104 |
|
| 105 |
let new_obj = T::deserialize(&mut de).unwrap(); |
| 106 |
|
| 107 |
assert_eq!(new_obj, obj); |
| 108 |
} |
| 109 |
|
| 110 |
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 111 |
enum Animal { |
| 112 |
Cow, |
| 113 |
Dog(DogOuter), |
| 114 |
Frog(Result<String, bool>, Option<Vec<f64>>), |
| 115 |
Cat { |
| 116 |
age: Integer, |
| 117 |
name: String, |
| 118 |
firmware: Option<Vec<u8>>, |
| 119 |
}, |
| 120 |
} |
| 121 |
|
| 122 |
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 123 |
struct DogOuter { |
| 124 |
inner: Vec<DogInner>, |
| 125 |
} |
| 126 |
|
| 127 |
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 128 |
struct DogInner { |
| 129 |
a: (), |
| 130 |
b: usize, |
| 131 |
c: Vec<String>, |
| 132 |
d: Option<Uid>, |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn cow() { |
| 137 |
let cow = Animal::Cow; |
| 138 |
|
| 139 |
let comparison = &[Event::String("Cow".into())]; |
| 140 |
|
| 141 |
assert_roundtrip(cow, Some(comparison)); |
| 142 |
} |
| 143 |
|
| 144 |
#[test] |
| 145 |
fn dog() { |
| 146 |
let dog = Animal::Dog(DogOuter { |
| 147 |
inner: vec![DogInner { |
| 148 |
a: (), |
| 149 |
b: 12, |
| 150 |
c: vec!["a".to_string(), "b".to_string()], |
| 151 |
d: Some(Uid::new(42)), |
| 152 |
}], |
| 153 |
}); |
| 154 |
|
| 155 |
let comparison = &[ |
| 156 |
Event::StartDictionary(Some(1)), |
| 157 |
Event::String("Dog".into()), |
| 158 |
Event::StartDictionary(None), |
| 159 |
Event::String("inner".into()), |
| 160 |
Event::StartArray(Some(1)), |
| 161 |
Event::StartDictionary(None), |
| 162 |
Event::String("a".into()), |
| 163 |
Event::String("".into()), |
| 164 |
Event::String("b".into()), |
| 165 |
Event::Integer(12.into()), |
| 166 |
Event::String("c".into()), |
| 167 |
Event::StartArray(Some(2)), |
| 168 |
Event::String("a".into()), |
| 169 |
Event::String("b".into()), |
| 170 |
Event::EndCollection, |
| 171 |
Event::String("d".into()), |
| 172 |
Event::Uid(Uid::new(42)), |
| 173 |
Event::EndCollection, |
| 174 |
Event::EndCollection, |
| 175 |
Event::EndCollection, |
| 176 |
Event::EndCollection, |
| 177 |
]; |
| 178 |
|
| 179 |
assert_roundtrip(dog, Some(comparison)); |
| 180 |
} |
| 181 |
|
| 182 |
#[test] |
| 183 |
fn frog() { |
| 184 |
let frog = Animal::Frog( |
| 185 |
Ok("hello".to_owned()), |
| 186 |
Some(vec![1.0, 2.0, 3.14159, 0.000000001, 1.27e31]), |
| 187 |
); |
| 188 |
|
| 189 |
let comparison = &[ |
| 190 |
Event::StartDictionary(Some(1)), |
| 191 |
Event::String("Frog".into()), |
| 192 |
Event::StartArray(Some(2)), |
| 193 |
Event::StartDictionary(Some(1)), |
| 194 |
Event::String("Ok".into()), |
| 195 |
Event::String("hello".into()), |
| 196 |
Event::EndCollection, |
| 197 |
Event::StartDictionary(Some(1)), |
| 198 |
Event::String("Some".into()), |
| 199 |
Event::StartArray(Some(5)), |
| 200 |
Event::Real(1.0), |
| 201 |
Event::Real(2.0), |
| 202 |
Event::Real(3.14159), |
| 203 |
Event::Real(0.000000001), |
| 204 |
Event::Real(1.27e31), |
| 205 |
Event::EndCollection, |
| 206 |
Event::EndCollection, |
| 207 |
Event::EndCollection, |
| 208 |
Event::EndCollection, |
| 209 |
]; |
| 210 |
|
| 211 |
assert_roundtrip(frog, Some(comparison)); |
| 212 |
} |
| 213 |
|
| 214 |
#[test] |
| 215 |
fn cat_with_firmware() { |
| 216 |
let cat = Animal::Cat { |
| 217 |
age: 12.into(), |
| 218 |
name: "Paws".to_owned(), |
| 219 |
firmware: Some(vec![0, 1, 2, 3, 4, 5, 6, 7, 8]), |
| 220 |
}; |
| 221 |
|
| 222 |
let comparison = &[ |
| 223 |
Event::StartDictionary(Some(1)), |
| 224 |
Event::String("Cat".into()), |
| 225 |
Event::StartDictionary(None), |
| 226 |
Event::String("age".into()), |
| 227 |
Event::Integer(12.into()), |
| 228 |
Event::String("name".into()), |
| 229 |
Event::String("Paws".into()), |
| 230 |
Event::String("firmware".into()), |
| 231 |
Event::StartArray(Some(9)), |
| 232 |
Event::Integer(0.into()), |
| 233 |
Event::Integer(1.into()), |
| 234 |
Event::Integer(2.into()), |
| 235 |
Event::Integer(3.into()), |
| 236 |
Event::Integer(4.into()), |
| 237 |
Event::Integer(5.into()), |
| 238 |
Event::Integer(6.into()), |
| 239 |
Event::Integer(7.into()), |
| 240 |
Event::Integer(8.into()), |
| 241 |
Event::EndCollection, |
| 242 |
Event::EndCollection, |
| 243 |
Event::EndCollection, |
| 244 |
]; |
| 245 |
|
| 246 |
assert_roundtrip(cat, Some(comparison)); |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn cat_without_firmware() { |
| 251 |
let cat = Animal::Cat { |
| 252 |
age: Integer::from(-12), |
| 253 |
name: "Paws".to_owned(), |
| 254 |
firmware: None, |
| 255 |
}; |
| 256 |
|
| 257 |
let comparison = &[ |
| 258 |
Event::StartDictionary(Some(1)), |
| 259 |
Event::String("Cat".into()), |
| 260 |
Event::StartDictionary(None), |
| 261 |
Event::String("age".into()), |
| 262 |
Event::Integer(Integer::from(-12)), |
| 263 |
Event::String("name".into()), |
| 264 |
Event::String("Paws".into()), |
| 265 |
Event::EndCollection, |
| 266 |
Event::EndCollection, |
| 267 |
]; |
| 268 |
|
| 269 |
assert_roundtrip(cat, Some(comparison)); |
| 270 |
} |
| 271 |
|
| 272 |
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 273 |
struct NewtypeStruct(NewtypeInner); |
| 274 |
|
| 275 |
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 276 |
struct NewtypeInner(u8, u8, u8); |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn newtype_struct() { |
| 280 |
let newtype = NewtypeStruct(NewtypeInner(34, 32, 13)); |
| 281 |
|
| 282 |
let comparison = &[ |
| 283 |
Event::StartArray(Some(3)), |
| 284 |
Event::Integer(34.into()), |
| 285 |
Event::Integer(32.into()), |
| 286 |
Event::Integer(13.into()), |
| 287 |
Event::EndCollection, |
| 288 |
]; |
| 289 |
|
| 290 |
assert_roundtrip(newtype, Some(comparison)); |
| 291 |
} |
| 292 |
|
| 293 |
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 294 |
struct TypeWithOptions { |
| 295 |
a: Option<String>, |
| 296 |
b: Option<Option<u32>>, |
| 297 |
c: Option<Box<TypeWithOptions>>, |
| 298 |
} |
| 299 |
|
| 300 |
#[test] |
| 301 |
fn type_with_options() { |
| 302 |
let inner = TypeWithOptions { |
| 303 |
a: None, |
| 304 |
b: Some(Some(12)), |
| 305 |
c: None, |
| 306 |
}; |
| 307 |
|
| 308 |
let obj = TypeWithOptions { |
| 309 |
a: Some("hello".to_owned()), |
| 310 |
b: Some(None), |
| 311 |
c: Some(Box::new(inner)), |
| 312 |
}; |
| 313 |
|
| 314 |
let comparison = &[ |
| 315 |
Event::StartDictionary(None), |
| 316 |
Event::String("a".into()), |
| 317 |
Event::String("hello".into()), |
| 318 |
Event::String("b".into()), |
| 319 |
Event::StartDictionary(Some(1)), |
| 320 |
Event::String("None".into()), |
| 321 |
Event::String("".into()), |
| 322 |
Event::EndCollection, |
| 323 |
Event::String("c".into()), |
| 324 |
Event::StartDictionary(None), |
| 325 |
Event::String("b".into()), |
| 326 |
Event::StartDictionary(Some(1)), |
| 327 |
Event::String("Some".into()), |
| 328 |
Event::Integer(12.into()), |
| 329 |
Event::EndCollection, |
| 330 |
Event::EndCollection, |
| 331 |
Event::EndCollection, |
| 332 |
]; |
| 333 |
|
| 334 |
assert_roundtrip(obj, Some(comparison)); |
| 335 |
} |
| 336 |
|
| 337 |
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 338 |
struct TypeWithDate { |
| 339 |
a: Option<i32>, |
| 340 |
b: Option<Date>, |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
fn type_with_date() { |
| 345 |
let date = Date::from_rfc3339("1920-01-01T00:10:00Z").unwrap(); |
| 346 |
|
| 347 |
let obj = TypeWithDate { |
| 348 |
a: Some(28), |
| 349 |
b: Some(date.clone()), |
| 350 |
}; |
| 351 |
|
| 352 |
let comparison = &[ |
| 353 |
Event::StartDictionary(None), |
| 354 |
Event::String("a".into()), |
| 355 |
Event::Integer(28.into()), |
| 356 |
Event::String("b".into()), |
| 357 |
Event::Date(date), |
| 358 |
Event::EndCollection, |
| 359 |
]; |
| 360 |
|
| 361 |
assert_roundtrip(obj, Some(comparison)); |
| 362 |
} |
| 363 |
|
| 364 |
#[test] |
| 365 |
fn option_some() { |
| 366 |
let obj = Some(12); |
| 367 |
|
| 368 |
let comparison = &[Event::Integer(12.into())]; |
| 369 |
|
| 370 |
assert_roundtrip(obj, Some(comparison)); |
| 371 |
} |
| 372 |
|
| 373 |
#[test] |
| 374 |
fn option_none() { |
| 375 |
let obj: Option<u32> = None; |
| 376 |
|
| 377 |
let comparison = &[]; |
| 378 |
|
| 379 |
assert_roundtrip(obj, Some(comparison)); |
| 380 |
} |
| 381 |
|
| 382 |
#[test] |
| 383 |
fn option_some_some() { |
| 384 |
let obj = Some(Some(12)); |
| 385 |
|
| 386 |
let comparison = &[ |
| 387 |
Event::StartDictionary(Some(1)), |
| 388 |
Event::String("Some".into()), |
| 389 |
Event::Integer(12.into()), |
| 390 |
Event::EndCollection, |
| 391 |
]; |
| 392 |
|
| 393 |
assert_roundtrip(obj, Some(comparison)); |
| 394 |
} |
| 395 |
|
| 396 |
#[test] |
| 397 |
fn option_some_none() { |
| 398 |
let obj: Option<Option<u32>> = Some(None); |
| 399 |
|
| 400 |
let comparison = &[ |
| 401 |
Event::StartDictionary(Some(1)), |
| 402 |
Event::String("None".into()), |
| 403 |
Event::String("".into()), |
| 404 |
Event::EndCollection, |
| 405 |
]; |
| 406 |
|
| 407 |
assert_roundtrip(obj, Some(comparison)); |
| 408 |
} |
| 409 |
|
| 410 |
#[test] |
| 411 |
fn option_dictionary_values() { |
| 412 |
let mut obj = BTreeMap::new(); |
| 413 |
obj.insert("a".to_owned(), None); |
| 414 |
obj.insert("b".to_owned(), Some(None)); |
| 415 |
obj.insert("c".to_owned(), Some(Some(144))); |
| 416 |
|
| 417 |
let comparison = &[ |
| 418 |
Event::StartDictionary(Some(3)), |
| 419 |
Event::String("a".into()), |
| 420 |
Event::StartDictionary(Some(1)), |
| 421 |
Event::String("None".into()), |
| 422 |
Event::String("".into()), |
| 423 |
Event::EndCollection, |
| 424 |
Event::String("b".into()), |
| 425 |
Event::StartDictionary(Some(1)), |
| 426 |
Event::String("Some".into()), |
| 427 |
Event::StartDictionary(Some(1)), |
| 428 |
Event::String("None".into()), |
| 429 |
Event::String("".into()), |
| 430 |
Event::EndCollection, |
| 431 |
Event::EndCollection, |
| 432 |
Event::String("c".into()), |
| 433 |
Event::StartDictionary(Some(1)), |
| 434 |
Event::String("Some".into()), |
| 435 |
Event::StartDictionary(Some(1)), |
| 436 |
Event::String("Some".into()), |
| 437 |
Event::Integer(144.into()), |
| 438 |
Event::EndCollection, |
| 439 |
Event::EndCollection, |
| 440 |
Event::EndCollection, |
| 441 |
]; |
| 442 |
|
| 443 |
assert_roundtrip(obj, Some(comparison)); |
| 444 |
} |
| 445 |
|
| 446 |
#[test] |
| 447 |
fn option_dictionary_keys() { |
| 448 |
let mut obj = BTreeMap::new(); |
| 449 |
obj.insert(None, 1); |
| 450 |
obj.insert(Some(None), 2); |
| 451 |
obj.insert(Some(Some(144)), 3); |
| 452 |
|
| 453 |
let comparison = &[ |
| 454 |
Event::StartDictionary(Some(3)), |
| 455 |
Event::StartDictionary(Some(1)), |
| 456 |
Event::String("None".into()), |
| 457 |
Event::String("".into()), |
| 458 |
Event::EndCollection, |
| 459 |
Event::Integer(1.into()), |
| 460 |
Event::StartDictionary(Some(1)), |
| 461 |
Event::String("Some".into()), |
| 462 |
Event::StartDictionary(Some(1)), |
| 463 |
Event::String("None".into()), |
| 464 |
Event::String("".into()), |
| 465 |
Event::EndCollection, |
| 466 |
Event::EndCollection, |
| 467 |
Event::Integer(2.into()), |
| 468 |
Event::StartDictionary(Some(1)), |
| 469 |
Event::String("Some".into()), |
| 470 |
Event::StartDictionary(Some(1)), |
| 471 |
Event::String("Some".into()), |
| 472 |
Event::Integer(144.into()), |
| 473 |
Event::EndCollection, |
| 474 |
Event::EndCollection, |
| 475 |
Event::Integer(3.into()), |
| 476 |
Event::EndCollection, |
| 477 |
]; |
| 478 |
|
| 479 |
assert_roundtrip(obj, Some(comparison)); |
| 480 |
} |
| 481 |
|
| 482 |
#[test] |
| 483 |
fn option_array() { |
| 484 |
let obj = vec![None, Some(None), Some(Some(144))]; |
| 485 |
|
| 486 |
let comparison = &[ |
| 487 |
Event::StartArray(Some(3)), |
| 488 |
Event::StartDictionary(Some(1)), |
| 489 |
Event::String("None".into()), |
| 490 |
Event::String("".into()), |
| 491 |
Event::EndCollection, |
| 492 |
Event::StartDictionary(Some(1)), |
| 493 |
Event::String("Some".into()), |
| 494 |
Event::StartDictionary(Some(1)), |
| 495 |
Event::String("None".into()), |
| 496 |
Event::String("".into()), |
| 497 |
Event::EndCollection, |
| 498 |
Event::EndCollection, |
| 499 |
Event::StartDictionary(Some(1)), |
| 500 |
Event::String("Some".into()), |
| 501 |
Event::StartDictionary(Some(1)), |
| 502 |
Event::String("Some".into()), |
| 503 |
Event::Integer(144.into()), |
| 504 |
Event::EndCollection, |
| 505 |
Event::EndCollection, |
| 506 |
Event::EndCollection, |
| 507 |
]; |
| 508 |
|
| 509 |
assert_roundtrip(obj, Some(comparison)); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn enum_variant_types() { |
| 514 |
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 515 |
enum Foo { |
| 516 |
Unit, |
| 517 |
Newtype(u32), |
| 518 |
Tuple(u32, String), |
| 519 |
Struct { v: u32, s: String }, |
| 520 |
} |
| 521 |
|
| 522 |
let expected = &[Event::String("Unit".into())]; |
| 523 |
assert_roundtrip(Foo::Unit, Some(expected)); |
| 524 |
|
| 525 |
let expected = &[ |
| 526 |
Event::StartDictionary(Some(1)), |
| 527 |
Event::String("Newtype".into()), |
| 528 |
Event::Integer(42.into()), |
| 529 |
Event::EndCollection, |
| 530 |
]; |
| 531 |
assert_roundtrip(Foo::Newtype(42), Some(expected)); |
| 532 |
|
| 533 |
let expected = &[ |
| 534 |
Event::StartDictionary(Some(1)), |
| 535 |
Event::String("Tuple".into()), |
| 536 |
Event::StartArray(Some(2)), |
| 537 |
Event::Integer(42.into()), |
| 538 |
Event::String("bar".into()), |
| 539 |
Event::EndCollection, |
| 540 |
Event::EndCollection, |
| 541 |
]; |
| 542 |
assert_roundtrip(Foo::Tuple(42, "bar".into()), Some(expected)); |
| 543 |
|
| 544 |
let expected = &[ |
| 545 |
Event::StartDictionary(Some(1)), |
| 546 |
Event::String("Struct".into()), |
| 547 |
Event::StartDictionary(None), |
| 548 |
Event::String("v".into()), |
| 549 |
Event::Integer(42.into()), |
| 550 |
Event::String("s".into()), |
| 551 |
Event::String("bar".into()), |
| 552 |
Event::EndCollection, |
| 553 |
Event::EndCollection, |
| 554 |
]; |
| 555 |
assert_roundtrip( |
| 556 |
Foo::Struct { |
| 557 |
v: 42, |
| 558 |
s: "bar".into(), |
| 559 |
}, |
| 560 |
Some(expected), |
| 561 |
); |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
fn deserialise_old_enum_unit_variant_encoding() { |
| 566 |
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 567 |
enum Foo { |
| 568 |
Bar, |
| 569 |
Baz, |
| 570 |
} |
| 571 |
|
| 572 |
// `plist` before v1.1 serialised unit enum variants as if they were newtype variants |
| 573 |
// containing an empty string. |
| 574 |
let events = &[ |
| 575 |
Event::StartDictionary(Some(1)), |
| 576 |
Event::String("Baz".into()), |
| 577 |
Event::String("".into()), |
| 578 |
Event::EndCollection, |
| 579 |
]; |
| 580 |
|
| 581 |
let mut de = new_deserializer(events.to_vec()); |
| 582 |
let obj = Foo::deserialize(&mut de).unwrap(); |
| 583 |
|
| 584 |
assert_eq!(obj, Foo::Baz); |
| 585 |
} |
| 586 |
|
| 587 |
#[test] |
| 588 |
fn deserialize_dictionary_xml() { |
| 589 |
let reader = File::open(&Path::new("./tests/data/xml.plist")).unwrap(); |
| 590 |
let dict: Dictionary = crate::from_reader(reader).unwrap(); |
| 591 |
|
| 592 |
check_common_plist(&dict); |
| 593 |
|
| 594 |
// xml.plist has this member, but binary.plist does not. |
| 595 |
assert_eq!( |
| 596 |
dict.get("HexademicalNumber") // sic |
| 597 |
.unwrap() |
| 598 |
.as_unsigned_integer() |
| 599 |
.unwrap(), |
| 600 |
0xDEADBEEF |
| 601 |
); |
| 602 |
} |
| 603 |
|
| 604 |
#[test] |
| 605 |
fn deserialize_dictionary_binary() { |
| 606 |
let reader = File::open(&Path::new("./tests/data/binary.plist")).unwrap(); |
| 607 |
let dict: Dictionary = crate::from_reader(reader).unwrap(); |
| 608 |
|
| 609 |
check_common_plist(&dict); |
| 610 |
} |
| 611 |
|
| 612 |
// Shared checks used by the tests deserialize_dictionary_xml() and |
| 613 |
// deserialize_dictionary_binary(), which load files with different formats |
| 614 |
// but the same data elements. |
| 615 |
fn check_common_plist(dict: &Dictionary) { |
| 616 |
// Array elements |
| 617 |
|
| 618 |
let lines = dict.get("Lines").unwrap().as_array().unwrap(); |
| 619 |
assert_eq!(lines.len(), 2); |
| 620 |
assert_eq!( |
| 621 |
lines[0].as_string().unwrap(), |
| 622 |
"It is a tale told by an idiot," |
| 623 |
); |
| 624 |
assert_eq!( |
| 625 |
lines[1].as_string().unwrap(), |
| 626 |
"Full of sound and fury, signifying nothing." |
| 627 |
); |
| 628 |
|
| 629 |
// Dictionary |
| 630 |
// |
| 631 |
// There is no embedded dictionary in this plist. See |
| 632 |
// deserialize_dictionary_binary_nskeyedarchiver() below for an example |
| 633 |
// of that. |
| 634 |
|
| 635 |
// Boolean elements |
| 636 |
|
| 637 |
assert_eq!(dict.get("IsTrue").unwrap().as_boolean().unwrap(), true); |
| 638 |
|
| 639 |
assert_eq!(dict.get("IsNotFalse").unwrap().as_boolean().unwrap(), false); |
| 640 |
|
| 641 |
// Data |
| 642 |
|
| 643 |
let data = dict.get("Data").unwrap().as_data().unwrap(); |
| 644 |
assert_eq!(data.len(), 15); |
| 645 |
assert_eq!( |
| 646 |
data, |
| 647 |
&[0, 0, 0, 0xbe, 0, 0, 0, 0x03, 0, 0, 0, 0x1e, 0, 0, 0] |
| 648 |
); |
| 649 |
|
| 650 |
// Date |
| 651 |
|
| 652 |
assert_eq!( |
| 653 |
dict.get("Birthdate").unwrap().as_date().unwrap(), |
| 654 |
Date::from_rfc3339("1981-05-16T11:32:06Z").unwrap() |
| 655 |
); |
| 656 |
|
| 657 |
// Real |
| 658 |
|
| 659 |
assert_eq!(dict.get("Height").unwrap().as_real().unwrap(), 1.6); |
| 660 |
|
| 661 |
// Integer |
| 662 |
|
| 663 |
assert_eq!( |
| 664 |
dict.get("BiggestNumber") |
| 665 |
.unwrap() |
| 666 |
.as_unsigned_integer() |
| 667 |
.unwrap(), |
| 668 |
18446744073709551615 |
| 669 |
); |
| 670 |
|
| 671 |
assert_eq!( |
| 672 |
dict.get("Death").unwrap().as_unsigned_integer().unwrap(), |
| 673 |
1564 |
| 674 |
); |
| 675 |
|
| 676 |
assert_eq!( |
| 677 |
dict.get("SmallestNumber") |
| 678 |
.unwrap() |
| 679 |
.as_signed_integer() |
| 680 |
.unwrap(), |
| 681 |
-9223372036854775808 |
| 682 |
); |
| 683 |
|
| 684 |
// String |
| 685 |
|
| 686 |
assert_eq!( |
| 687 |
dict.get("Author").unwrap().as_string().unwrap(), |
| 688 |
"William Shakespeare" |
| 689 |
); |
| 690 |
|
| 691 |
assert_eq!(dict.get("Blank").unwrap().as_string().unwrap(), ""); |
| 692 |
|
| 693 |
// Uid |
| 694 |
// |
| 695 |
// No checks for Uid value type in this test. See |
| 696 |
// deserialize_dictionary_binary_nskeyedarchiver() below for an example |
| 697 |
// of that. |
| 698 |
} |
| 699 |
|
| 700 |
#[test] |
| 701 |
fn deserialize_dictionary_binary_nskeyedarchiver() { |
| 702 |
let reader = File::open(&Path::new("./tests/data/binary_NSKeyedArchiver.plist")).unwrap(); |
| 703 |
let dict: Dictionary = crate::from_reader(reader).unwrap(); |
| 704 |
|
| 705 |
assert_eq!( |
| 706 |
dict.get("$archiver").unwrap().as_string().unwrap(), |
| 707 |
"NSKeyedArchiver" |
| 708 |
); |
| 709 |
|
| 710 |
let objects = dict.get("$objects").unwrap().as_array().unwrap(); |
| 711 |
assert_eq!(objects.len(), 5); |
| 712 |
|
| 713 |
assert_eq!(objects[0].as_string().unwrap(), "$null"); |
| 714 |
|
| 715 |
let objects_1 = objects[1].as_dictionary().unwrap(); |
| 716 |
assert_eq!( |
| 717 |
*objects_1.get("$class").unwrap().as_uid().unwrap(), |
| 718 |
Uid::new(4) |
| 719 |
); |
| 720 |
assert_eq!( |
| 721 |
objects_1 |
| 722 |
.get("NSRangeCount") |
| 723 |
.unwrap() |
| 724 |
.as_unsigned_integer() |
| 725 |
.unwrap(), |
| 726 |
42 |
| 727 |
); |
| 728 |
assert_eq!( |
| 729 |
*objects_1.get("NSRangeData").unwrap().as_uid().unwrap(), |
| 730 |
Uid::new(2) |
| 731 |
); |
| 732 |
|
| 733 |
let objects_2 = objects[2].as_dictionary().unwrap(); |
| 734 |
assert_eq!( |
| 735 |
*objects_2.get("$class").unwrap().as_uid().unwrap(), |
| 736 |
Uid::new(3) |
| 737 |
); |
| 738 |
let objects_2_nsdata = objects_2.get("NS.data").unwrap().as_data().unwrap(); |
| 739 |
assert_eq!(objects_2_nsdata.len(), 103); |
| 740 |
assert_eq!(objects_2_nsdata[0], 0x03); |
| 741 |
assert_eq!(objects_2_nsdata[102], 0x01); |
| 742 |
|
| 743 |
let objects_3 = objects[3].as_dictionary().unwrap(); |
| 744 |
let objects_3_classes = objects_3.get("$classes").unwrap().as_array().unwrap(); |
| 745 |
assert_eq!(objects_3_classes.len(), 3); |
| 746 |
assert_eq!(objects_3_classes[0].as_string().unwrap(), "NSMutableData"); |
| 747 |
assert_eq!(objects_3_classes[1].as_string().unwrap(), "NSData"); |
| 748 |
assert_eq!(objects_3_classes[2].as_string().unwrap(), "NSObject"); |
| 749 |
assert_eq!( |
| 750 |
objects_3.get("$classname").unwrap().as_string().unwrap(), |
| 751 |
"NSMutableData" |
| 752 |
); |
| 753 |
|
| 754 |
let objects_4 = objects[4].as_dictionary().unwrap(); |
| 755 |
let objects_4_classes = objects_4.get("$classes").unwrap().as_array().unwrap(); |
| 756 |
assert_eq!(objects_4_classes.len(), 3); |
| 757 |
assert_eq!( |
| 758 |
objects_4_classes[0].as_string().unwrap(), |
| 759 |
"NSMutableIndexSet" |
| 760 |
); |
| 761 |
assert_eq!(objects_4_classes[1].as_string().unwrap(), "NSIndexSet"); |
| 762 |
assert_eq!(objects_4_classes[2].as_string().unwrap(), "NSObject"); |
| 763 |
assert_eq!( |
| 764 |
objects_4.get("$classname").unwrap().as_string().unwrap(), |
| 765 |
"NSMutableIndexSet" |
| 766 |
); |
| 767 |
|
| 768 |
let top = dict.get("$top").unwrap().as_dictionary().unwrap(); |
| 769 |
assert_eq!( |
| 770 |
*top.get("foundItems").unwrap().as_uid().unwrap(), |
| 771 |
Uid::new(1) |
| 772 |
); |
| 773 |
|
| 774 |
let version = dict.get("$version").unwrap().as_unsigned_integer().unwrap(); |
| 775 |
assert_eq!(version, 100000); |
| 776 |
} |
| 777 |
|
| 778 |
#[test] |
| 779 |
fn dictionary_deserialize_dictionary_in_struct() { |
| 780 |
// Example from <https://github.com/ebarnard/rust-plist/issues/54> |
| 781 |
#[derive(Deserialize)] |
| 782 |
struct LayerinfoData { |
| 783 |
color: Option<String>, |
| 784 |
lib: Option<Dictionary>, |
| 785 |
} |
| 786 |
|
| 787 |
let lib_dict: LayerinfoData = crate::from_bytes(r#" |
| 788 |
<?xml version="1.0" encoding="UTF-8"?> |
| 789 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 790 |
<plist version="1.0"> |
| 791 |
<dict> |
| 792 |
<key>color</key> |
| 793 |
<string>1,0.75,0,0.7</string> |
| 794 |
<key>lib</key> |
| 795 |
<dict> |
| 796 |
<key>com.typemytype.robofont.segmentType</key> |
| 797 |
<string>curve</string> |
| 798 |
</dict> |
| 799 |
</dict> |
| 800 |
</plist> |
| 801 |
"#.as_bytes()).unwrap(); |
| 802 |
|
| 803 |
assert_eq!(lib_dict.color.unwrap(), "1,0.75,0,0.7"); |
| 804 |
assert_eq!( |
| 805 |
lib_dict |
| 806 |
.lib |
| 807 |
.unwrap() |
| 808 |
.get("com.typemytype.robofont.segmentType") |
| 809 |
.unwrap() |
| 810 |
.as_string() |
| 811 |
.unwrap(), |
| 812 |
"curve" |
| 813 |
); |
| 814 |
} |
| 815 |
|
| 816 |
#[test] |
| 817 |
fn dictionary_serialize_xml() { |
| 818 |
// Dictionary to be embedded in dict, below. |
| 819 |
let mut inner_dict = Dictionary::new(); |
| 820 |
inner_dict.insert( |
| 821 |
"FirstKey".to_owned(), |
| 822 |
Value::String("FirstValue".to_owned()), |
| 823 |
); |
| 824 |
inner_dict.insert("SecondKey".to_owned(), Value::Data(vec![10, 20, 30, 40])); |
| 825 |
inner_dict.insert("ThirdKey".to_owned(), Value::Real(1.234)); |
| 826 |
inner_dict.insert( |
| 827 |
"FourthKey".to_owned(), |
| 828 |
Value::Date(Date::from_rfc3339("1981-05-16T11:32:06Z").unwrap()), |
| 829 |
); |
| 830 |
|
| 831 |
// Top-level dictionary. |
| 832 |
let mut dict = Dictionary::new(); |
| 833 |
dict.insert( |
| 834 |
"AnArray".to_owned(), |
| 835 |
Value::Array(vec![ |
| 836 |
Value::String("Hello, world!".to_owned()), |
| 837 |
Value::Integer(Integer::from(345)), |
| 838 |
]), |
| 839 |
); |
| 840 |
dict.insert("ADictionary".to_owned(), Value::Dictionary(inner_dict)); |
| 841 |
dict.insert("AnInteger".to_owned(), Value::Integer(Integer::from(123))); |
| 842 |
dict.insert("ATrueBoolean".to_owned(), Value::Boolean(true)); |
| 843 |
dict.insert("AFalseBoolean".to_owned(), Value::Boolean(false)); |
| 844 |
|
| 845 |
// Serialize dictionary as an XML plist. |
| 846 |
let mut buf = Cursor::new(Vec::new()); |
| 847 |
crate::to_writer_xml(&mut buf, &dict).unwrap(); |
| 848 |
let buf = buf.into_inner(); |
| 849 |
let xml = std::str::from_utf8(&buf).unwrap(); |
| 850 |
|
| 851 |
let comparison = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> |
| 852 |
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> |
| 853 |
<plist version=\"1.0\"> |
| 854 |
<dict> |
| 855 |
\t<key>AnArray</key> |
| 856 |
\t<array> |
| 857 |
\t\t<string>Hello, world!</string> |
| 858 |
\t\t<integer>345</integer> |
| 859 |
\t</array> |
| 860 |
\t<key>ADictionary</key> |
| 861 |
\t<dict> |
| 862 |
\t\t<key>FirstKey</key> |
| 863 |
\t\t<string>FirstValue</string> |
| 864 |
\t\t<key>SecondKey</key> |
| 865 |
\t\t<data>\n\t\tChQeKA==\n\t\t</data> |
| 866 |
\t\t<key>ThirdKey</key> |
| 867 |
\t\t<real>1.234</real> |
| 868 |
\t\t<key>FourthKey</key> |
| 869 |
\t\t<date>1981-05-16T11:32:06Z</date> |
| 870 |
\t</dict> |
| 871 |
\t<key>AnInteger</key> |
| 872 |
\t<integer>123</integer> |
| 873 |
\t<key>ATrueBoolean</key> |
| 874 |
\t<true/> |
| 875 |
\t<key>AFalseBoolean</key> |
| 876 |
\t<false/> |
| 877 |
</dict> |
| 878 |
</plist>"; |
| 879 |
|
| 880 |
assert_eq!(xml, comparison); |
| 881 |
} |
| 882 |
|
| 883 |
#[test] |
| 884 |
fn serde_yaml_to_value() { |
| 885 |
let value: Value = serde_yaml::from_str("true").unwrap(); |
| 886 |
assert_eq!(value, Value::Boolean(true)); |
| 887 |
} |