use serde::{Deserialize, Serialize};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Checkpoint {
pub last_committed_sequence: u64,
pub last_event_id: Option<String>,
}
impl Checkpoint {
#[must_use]
pub fn new(last_committed_sequence: u64, last_event_id: Option<String>) -> Self {
Self {
last_committed_sequence,
last_event_id,
}
}
}
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::panic,
reason = "test code: unwrap and panic on unexpected variant are the standard test diagnostics"
)]
mod tests {
use super::Checkpoint;
#[test]
fn serde_roundtrip_with_id() {
let cp = Checkpoint::new(42, Some("mars@42".into()));
let json = serde_json::to_string(&cp).unwrap();
let back: Checkpoint = serde_json::from_str(&json).unwrap();
assert_eq!(cp, back);
}
#[test]
fn serde_roundtrip_without_id() {
let cp = Checkpoint::new(7, None);
let json = serde_json::to_string(&cp).unwrap();
let back: Checkpoint = serde_json::from_str(&json).unwrap();
assert_eq!(cp, back);
}
#[test]
fn new_sets_fields() {
let cp = Checkpoint::new(1, Some("e@1".into()));
assert_eq!(cp.last_committed_sequence, 1);
assert_eq!(cp.last_event_id.as_deref(), Some("e@1"));
}
}