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