feat: add db state persistence

This commit is contained in:
Jindřich Moravec 2024-01-28 16:19:21 +01:00
parent 6bf4e34006
commit e75ea5d5db
4 changed files with 115 additions and 39 deletions

15
server/src/persistence.rs Normal file
View file

@ -0,0 +1,15 @@
use std::path::PathBuf;
use tokio::{fs, io};
use minisql::interpreter::State;
pub async fn state_from_file(path: &PathBuf) -> io::Result<State> {
let content = fs::read_to_string(path).await?;
let state = serde_json::from_str(&content)?;
Ok(state)
}
pub async fn state_to_file(state: &State, path: &PathBuf) -> io::Result<()> {
let content = serde_json::to_string(state)?;
fs::write(path, content).await?;
Ok(())
}