Don't write Lox context to database in simulation

This commit is contained in:
Vecna 2024-05-31 10:58:26 -04:00
parent 43aff6c750
commit a19a8bb3c2
2 changed files with 28 additions and 22 deletions

View File

@ -45,3 +45,6 @@ sha1 = "0.10"
[dependencies.chrono]
version = "0.4.38"
features = ["serde"]
[features]
simulation = []

View File

@ -29,28 +29,31 @@ impl DB {
// Writes the Lox context to the lox database with "context_%Y-%m-%d_%H:%M:%S" as the
// database key
pub fn write_context(&mut self, context: lox_context::LoxServerContext) {
let date = Local::now().format("context_%Y-%m-%d_%H:%M:%S").to_string();
/* Uncomment to generate test file for this function after making changes to lox library
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open("db_test_file.json").unwrap();
serde_json::to_writer(&file, &context).unwrap();
*/
let json_result = serde_json::to_vec(&context).unwrap();
println!("Writing context to the db with key: {:?}", date);
let _new_ivec = self.db.insert(
IVec::from(date.as_bytes().to_vec()),
IVec::from(json_result.clone()),
);
assert_eq!(
self.db
.get(IVec::from(date.as_bytes().to_vec()))
.unwrap()
.unwrap(),
IVec::from(json_result)
);
#[cfg(not(feature = "simulation"))]
{
let date = Local::now().format("context_%Y-%m-%d_%H:%M:%S").to_string();
/* Uncomment to generate test file for this function after making changes to lox library
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open("db_test_file.json").unwrap();
serde_json::to_writer(&file, &context).unwrap();
*/
let json_result = serde_json::to_vec(&context).unwrap();
println!("Writing context to the db with key: {:?}", date);
let _new_ivec = self.db.insert(
IVec::from(date.as_bytes().to_vec()),
IVec::from(json_result.clone()),
);
assert_eq!(
self.db
.get(IVec::from(date.as_bytes().to_vec()))
.unwrap()
.unwrap(),
IVec::from(json_result)
);
}
}
// If roll_back_date is empty, opens the most recent entry in the lox database or if none exists, creates a