mirror of
https://github.com/Drop-OSS/dropbreak.git
synced 2026-02-14 19:30:53 +01:00
58 lines
1.3 KiB
Rust
58 lines
1.3 KiB
Rust
|
|
#[macro_use] extern crate serde_derive;
|
|
use failure;
|
|
|
|
use rustbreak::FileDatabase;
|
|
use rustbreak::deser::Ron;
|
|
|
|
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize, Clone)]
|
|
enum Country {
|
|
Italy, UnitedKingdom
|
|
}
|
|
|
|
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize, Clone)]
|
|
struct Person {
|
|
name: String,
|
|
country: Country,
|
|
}
|
|
|
|
fn do_main() -> Result<(), failure::Error> {
|
|
use std::collections::HashMap;
|
|
|
|
let db = FileDatabase::<HashMap<String, Person>, Ron>::from_path("test.ron", HashMap::new())?;
|
|
|
|
println!("Writing to Database");
|
|
db.write(|db| {
|
|
db.insert("john".into(), Person {
|
|
name: String::from("John Andersson"),
|
|
country: Country::Italy
|
|
});
|
|
db.insert("fred".into(), Person {
|
|
name: String::from("Fred Johnson"),
|
|
country: Country::UnitedKingdom
|
|
});
|
|
println!("Entries: \n{:#?}", db);
|
|
})?;
|
|
|
|
println!("Syncing Database");
|
|
db.save()?;
|
|
|
|
println!("Loading Database");
|
|
db.load()?;
|
|
|
|
println!("Reading from Database");
|
|
db.read(|db| {
|
|
println!("Results:");
|
|
println!("{:#?}", db);
|
|
})?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn main() {
|
|
if let Err(e) = do_main() {
|
|
eprintln!("An error has occurred at: \n{}", e.backtrace());
|
|
std::process::exit(1);
|
|
}
|
|
}
|