use std::fs;
+use std::error::Error;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
]
}
-fn create_contacts() {
+fn create_contacts() -> Result<(), Box<dyn Error>> {
let contacts = build_contacts();
- // FIXME: Use ? and return Result
- let serialized = serde_json::to_string(&contacts).unwrap();
+ let serialized = serde_json::to_string(&contacts)?;
println!("serialized: {}", serialized);
- let mut f = fs::File::create("/tmp/contacts.json").unwrap_or_else(|e| panic!("Could not create /tmp/contacts.json: {:?}", e));
- serde_json::to_writer_pretty(&mut f, &contacts).unwrap_or_else(|e| panic!("Could not serialize contacts: {:?}", e));
+ let mut f = fs::File::create("/tmp/contacts.json")?;
+ serde_json::to_writer_pretty(&mut f, &contacts)?;
+ Ok(())
}
-fn main() {
- create_contacts();
+fn main() -> Result<(), Box<dyn Error>> {
+ create_contacts()?;
+ Ok(())
}