use super::Db; use crate::config::PrivateConfig; use crate::error::CreddyError; impl Db { pub fn load_private_config(&self) -> Result { let conn = self.connection.lock().unwrap(); let json: String = conn.query_row("SELECT value FROM config", [], |row| row.get(0))?; let config: PrivateConfig = serde_json::from_str(&json)?; Ok(config) } pub fn save_private_config(&self, config: &PrivateConfig) -> Result<(), CreddyError> { let json = serde_json::to_string(&config)?; let conn = self.connection.lock().unwrap(); // the migration seeds the only row, so this is always an update conn.execute("UPDATE config SET value = ?;", [&json])?; Ok(()) } } #[cfg(test)] mod tests { use super::*; use crate::config::AppConfig; use tempfile::TempDir; use testresult::TestResult; fn test_db() -> Result<(Db, TempDir), CreddyError> { let data_dir = tempfile::tempdir()?; let mut config = AppConfig::default(); config.data_dir = data_dir.path().to_path_buf(); let db = Db::connect(&config, "correct horse battery staple")?; Ok((db, data_dir)) } #[test] fn test_save_private_config() -> TestResult { let (db, _data_dir) = test_db()?; let private_config = PrivateConfig::default(); db.save_private_config(&private_config)?; Ok(()) } #[test] fn test_load_private_config() -> TestResult { let (db, _data_dir) = test_db()?; let private_config = PrivateConfig::default(); db.save_private_config(&private_config)?; let loaded = db.load_private_config()?; assert_eq!(loaded, private_config); Ok(()) } }