2 use serde::{Serialize, Deserialize};
4 #[derive(Serialize, Deserialize, Debug)]
11 #[derive(Serialize, Deserialize, Debug)]
18 type Contacts = Vec<Contact>;
20 fn build_contacts() -> Contacts {
22 Contact { name: "John".to_string(), phone: 12345, social: Social::Twitter("@the_john".to_string()) },
23 Contact { name: "Mary".to_string(), phone: 9876543, social: Social::ICQ(111234) },
24 Contact { name: "Jane".to_string(), phone: 555555, social: Social::Nothing },
28 fn create_contacts() {
29 let contacts = build_contacts();
30 // FIXME: Use ? and return Result
31 let serialized = serde_json::to_string(&contacts).unwrap();
32 println!("serialized: {}", serialized);
33 let mut f = fs::File::create("/tmp/contacts.json").unwrap_or_else(|e| panic!("Could not create /tmp/contacts.json: {:?}", e));
34 serde_json::to_writer_pretty(&mut f, &contacts).unwrap_or_else(|e| panic!("Could not serialize contacts: {:?}", e));