1 use std::mem; 2 3 use rustc_serialize; 4 use Value; 5 use super::{Encoder, Error, State}; 6 use super::Error::*; 7 8 impl Encoder { table<F>(&mut self, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>9 fn table<F>(&mut self, f: F) -> Result<(), Error> 10 where F: FnOnce(&mut Encoder) -> Result<(), Error> 11 { 12 match mem::replace(&mut self.state, State::Start) { 13 State::NextKey(key) => { 14 let mut nested = Encoder::new(); 15 try!(f(&mut nested)); 16 self.toml.insert(key, Value::Table(nested.toml)); 17 Ok(()) 18 } 19 State::NextArray(mut arr) => { 20 let mut nested = Encoder::new(); 21 try!(f(&mut nested)); 22 arr.push(Value::Table(nested.toml)); 23 self.state = State::NextArray(arr); 24 Ok(()) 25 } 26 State::Start => f(self), 27 State::NextMapKey => Err(Error::InvalidMapKeyLocation), 28 } 29 } 30 seq<F>(&mut self, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>31 fn seq<F>(&mut self, f: F) -> Result<(), Error> 32 where F: FnOnce(&mut Encoder) -> Result<(), Error> 33 { 34 let old = try!(self.seq_begin()); 35 try!(f(self)); 36 self.seq_end(old) 37 } 38 } 39 40 impl rustc_serialize::Encoder for Encoder { 41 type Error = Error; 42 emit_nil(&mut self) -> Result<(), Error>43 fn emit_nil(&mut self) -> Result<(), Error> { Ok(()) } emit_usize(&mut self, v: usize) -> Result<(), Error>44 fn emit_usize(&mut self, v: usize) -> Result<(), Error> { 45 self.emit_i64(v as i64) 46 } emit_u8(&mut self, v: u8) -> Result<(), Error>47 fn emit_u8(&mut self, v: u8) -> Result<(), Error> { 48 self.emit_i64(v as i64) 49 } emit_u16(&mut self, v: u16) -> Result<(), Error>50 fn emit_u16(&mut self, v: u16) -> Result<(), Error> { 51 self.emit_i64(v as i64) 52 } emit_u32(&mut self, v: u32) -> Result<(), Error>53 fn emit_u32(&mut self, v: u32) -> Result<(), Error> { 54 self.emit_i64(v as i64) 55 } emit_u64(&mut self, v: u64) -> Result<(), Error>56 fn emit_u64(&mut self, v: u64) -> Result<(), Error> { 57 self.emit_i64(v as i64) 58 } emit_isize(&mut self, v: isize) -> Result<(), Error>59 fn emit_isize(&mut self, v: isize) -> Result<(), Error> { 60 self.emit_i64(v as i64) 61 } emit_i8(&mut self, v: i8) -> Result<(), Error>62 fn emit_i8(&mut self, v: i8) -> Result<(), Error> { 63 self.emit_i64(v as i64) 64 } emit_i16(&mut self, v: i16) -> Result<(), Error>65 fn emit_i16(&mut self, v: i16) -> Result<(), Error> { 66 self.emit_i64(v as i64) 67 } emit_i32(&mut self, v: i32) -> Result<(), Error>68 fn emit_i32(&mut self, v: i32) -> Result<(), Error> { 69 self.emit_i64(v as i64) 70 } emit_i64(&mut self, v: i64) -> Result<(), Error>71 fn emit_i64(&mut self, v: i64) -> Result<(), Error> { 72 self.emit_value(Value::Integer(v)) 73 } emit_bool(&mut self, v: bool) -> Result<(), Error>74 fn emit_bool(&mut self, v: bool) -> Result<(), Error> { 75 self.emit_value(Value::Boolean(v)) 76 } emit_f32(&mut self, v: f32) -> Result<(), Error>77 fn emit_f32(&mut self, v: f32) -> Result<(), Error> { self.emit_f64(v as f64) } emit_f64(&mut self, v: f64) -> Result<(), Error>78 fn emit_f64(&mut self, v: f64) -> Result<(), Error> { 79 self.emit_value(Value::Float(v)) 80 } emit_char(&mut self, v: char) -> Result<(), Error>81 fn emit_char(&mut self, v: char) -> Result<(), Error> { 82 self.emit_str(&v.to_string()) 83 } emit_str(&mut self, v: &str) -> Result<(), Error>84 fn emit_str(&mut self, v: &str) -> Result<(), Error> { 85 self.emit_value(Value::String(v.to_string())) 86 } emit_enum<F>(&mut self, _name: &str, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>87 fn emit_enum<F>(&mut self, _name: &str, f: F) 88 -> Result<(), Error> 89 where F: FnOnce(&mut Encoder) -> Result<(), Error> 90 { 91 f(self) 92 } emit_enum_variant<F>(&mut self, _v_name: &str, _v_id: usize, _len: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>93 fn emit_enum_variant<F>(&mut self, _v_name: &str, _v_id: usize, 94 _len: usize, f: F) -> Result<(), Error> 95 where F: FnOnce(&mut Encoder) -> Result<(), Error> 96 { 97 f(self) 98 } emit_enum_variant_arg<F>(&mut self, _a_idx: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>99 fn emit_enum_variant_arg<F>(&mut self, _a_idx: usize, f: F) 100 -> Result<(), Error> 101 where F: FnOnce(&mut Encoder) -> Result<(), Error> 102 { 103 f(self) 104 } emit_enum_struct_variant<F>(&mut self, _v_name: &str, _v_id: usize, _len: usize, _f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>105 fn emit_enum_struct_variant<F>(&mut self, _v_name: &str, _v_id: usize, 106 _len: usize, 107 _f: F) 108 -> Result<(), Error> 109 where F: FnOnce(&mut Encoder) -> Result<(), Error> 110 { 111 panic!() 112 } emit_enum_struct_variant_field<F>(&mut self, _f_name: &str, _f_idx: usize, _f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>113 fn emit_enum_struct_variant_field<F>(&mut self, 114 _f_name: &str, 115 _f_idx: usize, 116 _f: F) 117 -> Result<(), Error> 118 where F: FnOnce(&mut Encoder) -> Result<(), Error> 119 { 120 panic!() 121 } emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>122 fn emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) 123 -> Result<(), Error> 124 where F: FnOnce(&mut Encoder) -> Result<(), Error> 125 { 126 self.table(f) 127 } emit_struct_field<F>(&mut self, f_name: &str, _f_idx: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>128 fn emit_struct_field<F>(&mut self, f_name: &str, _f_idx: usize, f: F) 129 -> Result<(), Error> 130 where F: FnOnce(&mut Encoder) -> Result<(), Error> 131 { 132 let old = mem::replace(&mut self.state, 133 State::NextKey(f_name.to_string())); 134 try!(f(self)); 135 if self.state != State::Start { 136 return Err(NoValue) 137 } 138 self.state = old; 139 Ok(()) 140 } emit_tuple<F>(&mut self, len: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>141 fn emit_tuple<F>(&mut self, len: usize, f: F) 142 -> Result<(), Error> 143 where F: FnOnce(&mut Encoder) -> Result<(), Error> 144 { 145 self.emit_seq(len, f) 146 } emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>147 fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) 148 -> Result<(), Error> 149 where F: FnOnce(&mut Encoder) -> Result<(), Error> 150 { 151 self.emit_seq_elt(idx, f) 152 } emit_tuple_struct<F>(&mut self, _name: &str, _len: usize, _f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>153 fn emit_tuple_struct<F>(&mut self, _name: &str, _len: usize, _f: F) 154 -> Result<(), Error> 155 where F: FnOnce(&mut Encoder) -> Result<(), Error> 156 { 157 unimplemented!() 158 } emit_tuple_struct_arg<F>(&mut self, _f_idx: usize, _f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>159 fn emit_tuple_struct_arg<F>(&mut self, _f_idx: usize, _f: F) 160 -> Result<(), Error> 161 where F: FnOnce(&mut Encoder) -> Result<(), Error> 162 { 163 unimplemented!() 164 } emit_option<F>(&mut self, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>165 fn emit_option<F>(&mut self, f: F) 166 -> Result<(), Error> 167 where F: FnOnce(&mut Encoder) -> Result<(), Error> 168 { 169 f(self) 170 } emit_option_none(&mut self) -> Result<(), Error>171 fn emit_option_none(&mut self) -> Result<(), Error> { 172 self.emit_none() 173 } emit_option_some<F>(&mut self, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>174 fn emit_option_some<F>(&mut self, f: F) -> Result<(), Error> 175 where F: FnOnce(&mut Encoder) -> Result<(), Error> 176 { 177 f(self) 178 } emit_seq<F>(&mut self, _len: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>179 fn emit_seq<F>(&mut self, _len: usize, f: F) 180 -> Result<(), Error> 181 where F: FnOnce(&mut Encoder) -> Result<(), Error> 182 { 183 self.seq(f) 184 } emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>185 fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) 186 -> Result<(), Error> 187 where F: FnOnce(&mut Encoder) -> Result<(), Error> 188 { 189 f(self) 190 } emit_map<F>(&mut self, len: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>191 fn emit_map<F>(&mut self, len: usize, f: F) 192 -> Result<(), Error> 193 where F: FnOnce(&mut Encoder) -> Result<(), Error> 194 { 195 self.emit_struct("foo", len, f) 196 } emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>197 fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> Result<(), Error> 198 where F: FnOnce(&mut Encoder) -> Result<(), Error> 199 { 200 self.table_key(f) 201 } emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>202 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> Result<(), Error> 203 where F: FnOnce(&mut Encoder) -> Result<(), Error> 204 { 205 f(self) 206 } 207 } 208 209 impl rustc_serialize::Encodable for Value { encode<E>(&self, e: &mut E) -> Result<(), E::Error> where E: rustc_serialize::Encoder210 fn encode<E>(&self, e: &mut E) -> Result<(), E::Error> 211 where E: rustc_serialize::Encoder 212 { 213 match *self { 214 Value::String(ref s) => e.emit_str(s), 215 Value::Integer(i) => e.emit_i64(i), 216 Value::Float(f) => e.emit_f64(f), 217 Value::Boolean(b) => e.emit_bool(b), 218 Value::Datetime(ref s) => e.emit_str(s), 219 Value::Array(ref a) => { 220 e.emit_seq(a.len(), |e| { 221 for item in a { 222 try!(item.encode(e)); 223 } 224 Ok(()) 225 }) 226 } 227 Value::Table(ref t) => { 228 e.emit_map(t.len(), |e| { 229 for (i, (key, value)) in t.iter().enumerate() { 230 try!(e.emit_map_elt_key(i, |e| e.emit_str(key))); 231 try!(e.emit_map_elt_val(i, |e| value.encode(e))); 232 } 233 Ok(()) 234 }) 235 } 236 } 237 } 238 } 239 240 #[cfg(test)] 241 mod tests { 242 use std::collections::{BTreeMap, HashSet}; 243 use rustc_serialize::{self, Encodable, Decodable}; 244 245 use {Encoder, Decoder, DecodeError}; 246 use Value; 247 use Value::{Table, Integer, Array, Float}; 248 249 macro_rules! encode( ($t:expr) => ({ 250 let mut e = Encoder::new(); 251 $t.encode(&mut e).unwrap(); 252 e.toml 253 }) ); 254 255 macro_rules! decode( ($t:expr) => ({ 256 let mut d = Decoder::new($t); 257 Decodable::decode(&mut d).unwrap() 258 }) ); 259 260 macro_rules! map( ($($k:ident, $v:expr),*) => ({ 261 let mut _m = BTreeMap::new(); 262 $(_m.insert(stringify!($k).to_string(), $v);)* 263 _m 264 }) ); 265 266 #[test] smoke()267 fn smoke() { 268 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 269 struct Foo { a: isize } 270 271 let v = Foo { a: 2 }; 272 assert_eq!(encode!(v), map! { a, Integer(2) }); 273 assert_eq!(v, decode!(Table(encode!(v)))); 274 } 275 276 #[test] smoke_hyphen()277 fn smoke_hyphen() { 278 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 279 struct Foo { a_b: isize } 280 281 let v = Foo { a_b: 2 }; 282 assert_eq!(encode!(v), map! { a_b, Integer(2) }); 283 assert_eq!(v, decode!(Table(encode!(v)))); 284 285 let mut m = BTreeMap::new(); 286 m.insert("a-b".to_string(), Integer(2)); 287 assert_eq!(v, decode!(Table(encode!(v)))); 288 } 289 290 #[test] nested()291 fn nested() { 292 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 293 struct Foo { a: isize, b: Bar } 294 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 295 struct Bar { a: String } 296 297 let v = Foo { a: 2, b: Bar { a: "test".to_string() } }; 298 assert_eq!(encode!(v), 299 map! { 300 a, Integer(2), 301 b, Table(map! { 302 a, Value::String("test".to_string()) 303 }) 304 }); 305 assert_eq!(v, decode!(Table(encode!(v)))); 306 } 307 308 #[test] application_decode_error()309 fn application_decode_error() { 310 #[derive(PartialEq, Debug)] 311 struct Range10(usize); 312 impl Decodable for Range10 { 313 fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<Range10, D::Error> { 314 let x: usize = try!(Decodable::decode(d)); 315 if x > 10 { 316 Err(d.error("Value out of range!")) 317 } else { 318 Ok(Range10(x)) 319 } 320 } 321 } 322 let mut d_good = Decoder::new(Integer(5)); 323 let mut d_bad1 = Decoder::new(Value::String("not an isize".to_string())); 324 let mut d_bad2 = Decoder::new(Integer(11)); 325 326 assert_eq!(Ok(Range10(5)), Decodable::decode(&mut d_good)); 327 328 let err1: Result<Range10, _> = Decodable::decode(&mut d_bad1); 329 assert!(err1.is_err()); 330 let err2: Result<Range10, _> = Decodable::decode(&mut d_bad2); 331 assert!(err2.is_err()); 332 } 333 334 #[test] array()335 fn array() { 336 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 337 struct Foo { a: Vec<isize> } 338 339 let v = Foo { a: vec![1, 2, 3, 4] }; 340 assert_eq!(encode!(v), 341 map! { 342 a, Array(vec![ 343 Integer(1), 344 Integer(2), 345 Integer(3), 346 Integer(4) 347 ]) 348 }); 349 assert_eq!(v, decode!(Table(encode!(v)))); 350 } 351 352 #[test] tuple()353 fn tuple() { 354 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 355 struct Foo { a: (isize, isize, isize, isize) } 356 357 let v = Foo { a: (1, 2, 3, 4) }; 358 assert_eq!(encode!(v), 359 map! { 360 a, Array(vec![ 361 Integer(1), 362 Integer(2), 363 Integer(3), 364 Integer(4) 365 ]) 366 }); 367 assert_eq!(v, decode!(Table(encode!(v)))); 368 } 369 370 #[test] inner_structs_with_options()371 fn inner_structs_with_options() { 372 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 373 struct Foo { 374 a: Option<Box<Foo>>, 375 b: Bar, 376 } 377 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 378 struct Bar { 379 a: String, 380 b: f64, 381 } 382 383 let v = Foo { 384 a: Some(Box::new(Foo { 385 a: None, 386 b: Bar { a: "foo".to_string(), b: 4.5 }, 387 })), 388 b: Bar { a: "bar".to_string(), b: 1.0 }, 389 }; 390 assert_eq!(encode!(v), 391 map! { 392 a, Table(map! { 393 b, Table(map! { 394 a, Value::String("foo".to_string()), 395 b, Float(4.5) 396 }) 397 }), 398 b, Table(map! { 399 a, Value::String("bar".to_string()), 400 b, Float(1.0) 401 }) 402 }); 403 assert_eq!(v, decode!(Table(encode!(v)))); 404 } 405 406 #[test] hashmap()407 fn hashmap() { 408 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 409 struct Foo { 410 map: BTreeMap<String, isize>, 411 set: HashSet<char>, 412 } 413 414 let v = Foo { 415 map: { 416 let mut m = BTreeMap::new(); 417 m.insert("foo".to_string(), 10); 418 m.insert("bar".to_string(), 4); 419 m 420 }, 421 set: { 422 let mut s = HashSet::new(); 423 s.insert('a'); 424 s 425 }, 426 }; 427 assert_eq!(encode!(v), 428 map! { 429 map, Table(map! { 430 foo, Integer(10), 431 bar, Integer(4) 432 }), 433 set, Array(vec![Value::String("a".to_string())]) 434 } 435 ); 436 assert_eq!(v, decode!(Table(encode!(v)))); 437 } 438 439 #[test] tuple_struct()440 fn tuple_struct() { 441 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 442 struct Foo(isize, String, f64); 443 444 let v = Foo(1, "foo".to_string(), 4.5); 445 assert_eq!( 446 encode!(v), 447 map! { 448 _field0, Integer(1), 449 _field1, Value::String("foo".to_string()), 450 _field2, Float(4.5) 451 } 452 ); 453 assert_eq!(v, decode!(Table(encode!(v)))); 454 } 455 456 #[test] table_array()457 fn table_array() { 458 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 459 struct Foo { a: Vec<Bar>, } 460 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 461 struct Bar { a: isize } 462 463 let v = Foo { a: vec![Bar { a: 1 }, Bar { a: 2 }] }; 464 assert_eq!( 465 encode!(v), 466 map! { 467 a, Array(vec![ 468 Table(map!{ a, Integer(1) }), 469 Table(map!{ a, Integer(2) }), 470 ]) 471 } 472 ); 473 assert_eq!(v, decode!(Table(encode!(v)))); 474 } 475 476 #[test] type_errors()477 fn type_errors() { 478 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 479 struct Foo { bar: isize } 480 481 let mut d = Decoder::new(Table(map! { 482 bar, Float(1.0) 483 })); 484 let a: Result<Foo, DecodeError> = Decodable::decode(&mut d); 485 match a { 486 Ok(..) => panic!("should not have decoded"), 487 Err(e) => { 488 assert_eq!(e.to_string(), 489 "expected a value of type `integer`, but \ 490 found a value of type `float` for the key `bar`"); 491 } 492 } 493 } 494 495 #[test] missing_errors()496 fn missing_errors() { 497 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 498 struct Foo { bar: isize } 499 500 let mut d = Decoder::new(Table(map! { 501 })); 502 let a: Result<Foo, DecodeError> = Decodable::decode(&mut d); 503 match a { 504 Ok(..) => panic!("should not have decoded"), 505 Err(e) => { 506 assert_eq!(e.to_string(), 507 "expected a value of type `integer` for the key `bar`"); 508 } 509 } 510 } 511 512 #[test] parse_enum()513 fn parse_enum() { 514 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 515 struct Foo { a: E } 516 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 517 enum E { 518 Bar(isize), 519 Baz(f64), 520 Last(Foo2), 521 } 522 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 523 struct Foo2 { 524 test: String, 525 } 526 527 let v = Foo { a: E::Bar(10) }; 528 assert_eq!( 529 encode!(v), 530 map! { a, Integer(10) } 531 ); 532 assert_eq!(v, decode!(Table(encode!(v)))); 533 534 let v = Foo { a: E::Baz(10.2) }; 535 assert_eq!( 536 encode!(v), 537 map! { a, Float(10.2) } 538 ); 539 assert_eq!(v, decode!(Table(encode!(v)))); 540 541 let v = Foo { a: E::Last(Foo2 { test: "test".to_string() }) }; 542 assert_eq!( 543 encode!(v), 544 map! { a, Table(map! { test, Value::String("test".to_string()) }) } 545 ); 546 assert_eq!(v, decode!(Table(encode!(v)))); 547 } 548 549 #[test] unused_fields()550 fn unused_fields() { 551 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 552 struct Foo { a: isize } 553 554 let v = Foo { a: 2 }; 555 let mut d = Decoder::new(Table(map! { 556 a, Integer(2), 557 b, Integer(5) 558 })); 559 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 560 561 assert_eq!(d.toml, Some(Table(map! { 562 b, Integer(5) 563 }))); 564 } 565 566 #[test] unused_fields2()567 fn unused_fields2() { 568 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 569 struct Foo { a: Bar } 570 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 571 struct Bar { a: isize } 572 573 let v = Foo { a: Bar { a: 2 } }; 574 let mut d = Decoder::new(Table(map! { 575 a, Table(map! { 576 a, Integer(2), 577 b, Integer(5) 578 }) 579 })); 580 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 581 582 assert_eq!(d.toml, Some(Table(map! { 583 a, Table(map! { 584 b, Integer(5) 585 }) 586 }))); 587 } 588 589 #[test] unused_fields3()590 fn unused_fields3() { 591 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 592 struct Foo { a: Bar } 593 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 594 struct Bar { a: isize } 595 596 let v = Foo { a: Bar { a: 2 } }; 597 let mut d = Decoder::new(Table(map! { 598 a, Table(map! { 599 a, Integer(2) 600 }) 601 })); 602 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 603 604 assert_eq!(d.toml, None); 605 } 606 607 #[test] unused_fields4()608 fn unused_fields4() { 609 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 610 struct Foo { a: BTreeMap<String, String> } 611 612 let v = Foo { a: map! { a, "foo".to_string() } }; 613 let mut d = Decoder::new(Table(map! { 614 a, Table(map! { 615 a, Value::String("foo".to_string()) 616 }) 617 })); 618 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 619 620 assert_eq!(d.toml, None); 621 } 622 623 #[test] unused_fields5()624 fn unused_fields5() { 625 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 626 struct Foo { a: Vec<String> } 627 628 let v = Foo { a: vec!["a".to_string()] }; 629 let mut d = Decoder::new(Table(map! { 630 a, Array(vec![Value::String("a".to_string())]) 631 })); 632 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 633 634 assert_eq!(d.toml, None); 635 } 636 637 #[test] unused_fields6()638 fn unused_fields6() { 639 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 640 struct Foo { a: Option<Vec<String>> } 641 642 let v = Foo { a: Some(vec![]) }; 643 let mut d = Decoder::new(Table(map! { 644 a, Array(vec![]) 645 })); 646 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 647 648 assert_eq!(d.toml, None); 649 } 650 651 #[test] unused_fields7()652 fn unused_fields7() { 653 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 654 struct Foo { a: Vec<Bar> } 655 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 656 struct Bar { a: isize } 657 658 let v = Foo { a: vec![Bar { a: 1 }] }; 659 let mut d = Decoder::new(Table(map! { 660 a, Array(vec![Table(map! { 661 a, Integer(1), 662 b, Integer(2) 663 })]) 664 })); 665 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 666 667 assert_eq!(d.toml, Some(Table(map! { 668 a, Array(vec![Table(map! { 669 b, Integer(2) 670 })]) 671 }))); 672 } 673 674 #[test] unused_fields8()675 fn unused_fields8() { 676 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 677 struct Foo { a: BTreeMap<String, Bar> } 678 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 679 struct Bar { a: isize } 680 681 let v = Foo { a: map! { a, Bar { a: 2 } } }; 682 let mut d = Decoder::new(Table(map! { 683 a, Table(map! { 684 a, Table(map! { 685 a, Integer(2), 686 b, Integer(2) 687 }) 688 }) 689 })); 690 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 691 692 assert_eq!(d.toml, Some(Table(map! { 693 a, Table(map! { 694 a, Table(map! { 695 b, Integer(2) 696 }) 697 }) 698 }))); 699 } 700 701 #[test] empty_arrays()702 fn empty_arrays() { 703 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 704 struct Foo { a: Vec<Bar> } 705 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 706 struct Bar; 707 708 let v = Foo { a: vec![] }; 709 let mut d = Decoder::new(Table(map! {})); 710 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 711 } 712 713 #[test] empty_arrays2()714 fn empty_arrays2() { 715 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 716 struct Foo { a: Option<Vec<Bar>> } 717 #[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)] 718 struct Bar; 719 720 let v = Foo { a: None }; 721 let mut d = Decoder::new(Table(map! {})); 722 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 723 724 let v = Foo { a: Some(vec![]) }; 725 let mut d = Decoder::new(Table(map! { 726 a, Array(vec![]) 727 })); 728 assert_eq!(v, Decodable::decode(&mut d).unwrap()); 729 } 730 731 #[test] round_trip()732 fn round_trip() { 733 let toml = r#" 734 [test] 735 foo = "bar" 736 737 [[values]] 738 foo = "baz" 739 740 [[values]] 741 foo = "qux" 742 "#; 743 744 let value: Value = toml.parse().unwrap(); 745 let val2 = ::encode_str(&value).parse().unwrap(); 746 assert_eq!(value, val2); 747 } 748 } 749