use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZulipSyncState {
pub site: String,
pub stream: String,
pub stream_id: u64,
pub self_email: String,
#[serde(default)]
pub topic: Option<String>,
#[serde(default)]
pub last_message_id: Option<u64>,
#[serde(default)]
pub last_pushed_session: Option<u32>,
}
impl ZulipSyncState {
pub fn topic_name(&self) -> &str {
self.topic.as_deref().unwrap_or("cryochamber")
}
}
pub fn save_sync_state(path: &Path, state: &ZulipSyncState) -> Result<()> {
let json = serde_json::to_string_pretty(state)?;
std::fs::write(path, json)?;
Ok(())
}
pub fn load_sync_state(path: &Path) -> Result<Option<ZulipSyncState>> {
if !path.exists() {
return Ok(None);
}
let contents = std::fs::read_to_string(path)?;
let state: ZulipSyncState = serde_json::from_str(&contents)?;
Ok(Some(state))
}