use serde::{Deserialize, Serialize};
use std::ops::Sub;
use std::time::{Duration, SystemTime};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct User {
pub name: String,
pub age: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct MyIndex {
pub categories: Vec<String>,
pub starting_date: SystemTime,
}
fn main() {
let mut db = sfsdb::new_indexed("db", Some(20), 100);
db.save_with_index(
"justin",
&User {
name: String::from("Justin Evens"),
age: 22,
},
MyIndex {
categories: vec!["employee".into(), "programmers".into()],
starting_date: SystemTime::now(),
},
)
.unwrap();
db.save_with_index(
"keth",
&User {
name: String::from("Keth Stone"),
age: 31,
},
MyIndex {
categories: vec!["employee".into(), "support team".into()],
starting_date: SystemTime::now().sub(Duration::from_secs(400)),
},
)
.unwrap();
println!("Index of \"justin\": {:?}", db.get_index("justin").unwrap());
println!("Index of \"keth\": {:?}", db.get_index("keth").unwrap());
let programmers = db.search_with(|index| index.categories.contains(&"programmers".to_owned()));
println!("All programmers: {:?}", programmers);
let recent =
db.search_with(|index| index.starting_date.elapsed().unwrap() > Duration::from_secs(300));
println!("These have existed for more than 300 seconds: {:?}", recent);
}