use std::path::Path;
use anyhow::Result;
use serde_json::{json, Value};
use super::{inbox_path, read_jsonl, str_field, Mode};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct InboxLine {
pub(crate) id: String,
pub(crate) enqueued_utc: String,
pub(crate) mode: Mode,
pub(crate) expires_utc: Option<String>,
}
impl InboxLine {
pub(crate) fn to_json(&self) -> Value {
json!({
"id": self.id,
"enqueued_utc": self.enqueued_utc,
"mode": self.mode.as_str(),
"expires_utc": self.expires_utc,
})
}
pub(crate) fn from_json(v: &Value) -> Option<Self> {
Some(InboxLine {
id: str_field(v, "id")?,
enqueued_utc: str_field(v, "enqueued_utc")?,
mode: Mode::parse(str_field(v, "mode")?.as_str())?,
expires_utc: str_field(v, "expires_utc"),
})
}
}
pub(crate) fn append_inbox(root: &Path, lane: &str, line: &InboxLine) -> Result<()> {
let path = inbox_path(root, lane)?;
super::append_line(&path, &serde_json::to_string(&line.to_json())?)
}
pub(crate) fn read_inbox(root: &Path, lane: &str) -> Result<(Vec<InboxLine>, usize)> {
let path = inbox_path(root, lane)?;
let (values, mut skipped) = read_jsonl(&path)?;
let mut out = Vec::with_capacity(values.len());
for v in &values {
match InboxLine::from_json(v) {
Some(line) => out.push(line),
None => skipped += 1,
}
}
Ok((out, skipped))
}