use super::state::ProState;
use super::wire::GuardEvent;
use super::{ProError, ProResult, ProjectScope, guard};
fn trail(scope: &ProjectScope) -> std::path::PathBuf {
scope.data_dir.join("audit").join("trail.jsonl")
}
pub(crate) fn pending(scope: &ProjectScope) -> ProResult<Option<(Vec<GuardEvent>, u64)>> {
let state = ProState::new(scope);
if !state.alert_settings().enabled {
return Ok(None);
}
let trail = trail(scope);
let Some(cursor) = state.guard_cursor() else {
let end = guard::scan_lines(&trail, 0, |_| {})?;
state.put_guard_cursor(end)?;
return Ok(None);
};
let mut fresh = Vec::new();
let end = guard::scan_lines(&trail, cursor, |line| {
if let Some(event) = guard::parse_event(line) {
fresh.push(event);
}
})?;
if fresh.is_empty() {
if end != cursor {
state.put_guard_cursor(end)?;
}
return Ok(None);
}
Ok(Some((fresh, end)))
}
pub(crate) fn advance(scope: &ProjectScope, offset: u64) -> ProResult<()> {
if offset > guard::trail_len(&trail(scope)) {
return Err(ProError::bad("guard_cursor is past the audit trail"));
}
ProState::new(scope).put_guard_cursor(offset)
}