use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Computer {
secret: bool,
question: String,
awnser: Option<usize>,
}
#[dbstruct::dbstruct(db=sled)]
pub struct Test {
the_awnser: Option<u8>,
questions: Vec<String>,
computers: HashMap<String, Computer>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let dir = tempdir::TempDir::new("dbstruct_examples")?;
let path = dir.path().join("advanced");
let db = Test::new(path)?;
assert_eq!(None, db.the_awnser().get()?);
db.the_awnser().set(&42)?;
assert_eq!(Some(42u8), db.the_awnser().get()?);
assert_eq!(None, db.questions().pop()?);
db.questions().push("What is Life")?;
db.questions().push("What is the Universe")?;
db.questions().push("What is Everything")?;
db.questions().pop()?;
let second = db.questions().pop()?; assert_eq!(Some("What is the Universe".to_owned()), second);
let deep_thought = Computer {
secret: false,
question: "The Ultimate Question of Life, the Universe, and Everything".to_owned(),
awnser: Some(42),
};
let a_planet = Computer {
secret: true,
question: "What is The Ultimate Question".to_owned(),
awnser: None, };
db.computers().insert("Deep Thought", &deep_thought)?;
db.computers().insert("Earth", &a_planet)?;
let earth = db.computers().get("Earth")?;
assert!(earth.unwrap().secret);
Ok(())
}