use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GhSyncState {
pub repo: String,
pub discussion_number: u64,
pub discussion_node_id: String,
#[serde(default)]
pub last_read_cursor: Option<String>,
#[serde(default)]
pub self_login: Option<String>,
#[serde(default)]
pub last_pushed_session: Option<u32>,
}
impl GhSyncState {
pub fn owner_repo(&self) -> Result<(&str, &str)> {
self.repo
.split_once('/')
.context("repo must be in 'owner/repo' format")
}
}
pub fn save_sync_state(path: &Path, state: &GhSyncState) -> Result<()> {
let json = serde_json::to_string_pretty(state)?;
std::fs::write(path, json)?;
Ok(())
}
pub fn load_sync_state(path: &Path) -> Result<Option<GhSyncState>> {
if !path.exists() {
return Ok(None);
}
let contents = std::fs::read_to_string(path)?;
let state: GhSyncState = serde_json::from_str(&contents)?;
Ok(Some(state))
}