Add server database interaction

This commit is contained in:
Michael Mikovsky
2025-12-01 16:38:33 -07:00
parent 97bd45571b
commit d1a0050f45
10 changed files with 229 additions and 38 deletions
+34 -2
View File
@@ -1,5 +1,37 @@
// Calc 3 ends Tuesday next week
use unshell_lib::error;
#[derive(Clone)]
pub struct Database {
// db:
db: sled::Db,
}
impl Database {
pub fn new(database: String) -> Self {
Self {
db: sled::open(database).expect("Failed to open database"),
}
}
pub fn put_value(&self, key: &str, value: &str) -> Result<(), String> {
match self.db.insert(key, value) {
Ok(_) => Ok(()),
Err(e) => {
error!("Failed to load '{}' from database: {}", key, e);
Err(e.to_string())
}
}
}
pub fn get_value(&self, key: &str) -> Result<String, String> {
match self.db.get(key) {
Ok(v) => match v {
Some(v) => Ok(String::from_utf8_lossy(&v.to_vec()).to_string()),
None => Err(format!("Could not find key '{}'", key)),
},
Err(e) => {
error!("Failed to load '{}' from database: {}", key, e);
Err(e.to_string())
}
}
}
}