use std::error::Error;
use serde::{Serialize, Deserialize};
+const DB_PATH: &str = "/tmp/contacts.json";
+
#[derive(Serialize, Deserialize, Debug)]
enum Social {
Twitter(String),
let contacts = build_contacts();
let serialized = serde_json::to_string(&contacts)?;
println!("serialized: {}", serialized);
- let mut f = fs::File::create("/tmp/contacts.json")?;
+ let mut f = fs::File::create(DB_PATH)?;
serde_json::to_writer_pretty(&mut f, &contacts)?;
Ok(())
}
+fn load_contacts() -> Result<Contacts, Box<dyn Error>> {
+ let f = fs::File::open(DB_PATH)?;
+ Ok(serde_json::from_reader(f)?)
+}
+
fn main() -> Result<(), Box<dyn Error>> {
create_contacts()?;
+ let contacts = load_contacts()?;
+ println!("deserialized: {:?}", contacts);
Ok(())
}