Recreate bridgedb from file.

This commit is contained in:
Vecna 2022-11-16 15:45:27 -05:00
parent cf066b1937
commit 384da7fd3b
1 changed files with 14 additions and 3 deletions

View File

@ -1,14 +1,25 @@
use lox::BridgeDb;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() {
// create new bridgedb (implicitly generates keys)
let bridgedb = BridgeDb::new();
let bridgedb_outfile_name = "bridgedb.json";
// If bridgedb has already been created, recreate it from file.
// Otherwise, create new bridgedb.
let bridgedb = if Path::new(bridgedb_outfile_name).exists() {
// read in file
let bridgedb_infile = File::open(bridgedb_outfile_name).unwrap();
serde_json::from_reader(bridgedb_infile).unwrap()
} else {
// create new bridgedb (implicitly generates keys)
BridgeDb::new()
};
// output full serialized bridgedb
let mut bridgedb_outfile =
File::create("bridgedb.json").expect("Failed to create bridgedb.json");
File::create(bridgedb_outfile_name).expect("Failed to create bridgedb.json");
let bridgedb_outfile_json = serde_json::to_string(&bridgedb).unwrap();
write!(bridgedb_outfile, "{}", bridgedb_outfile_json)
.expect("Failed to write to bridgedb.json");