use serde_json::{Value, json};
use super::{Header, TurnRecord};
use crate::conversation::Refused;
use crate::limits::RECORD_TURN_BYTES;
#[derive(Debug, Default)]
pub struct Write {
pub draft: String,
pub records: Vec<TurnRecord>,
pub turn_bytes: Option<u64>,
pub seal: bool,
pub discard: bool,
pub actor: Option<String>,
pub stamp: Option<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum State {
None,
Open,
Sealed,
Discarded,
}
impl State {
pub fn as_str(self) -> &'static str {
match self {
Self::None => "none",
Self::Open => "open",
Self::Sealed => "sealed",
Self::Discarded => "discarded",
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Written {
pub draft: String,
pub state: State,
pub next_seq: u64,
pub turn: Option<Header>,
pub stored_bytes: u64,
pub freed_bytes: u64,
}
impl Written {
pub fn to_json(&self) -> Value {
json!({
"draft": self.draft,
"state": self.state.as_str(),
"next_seq": self.next_seq,
"turn": self.turn.as_ref().map(Header::to_json),
"stored_bytes": self.stored_bytes,
"freed_bytes": self.freed_bytes,
})
}
}
pub fn check_draft_id(id: &str) -> Result<(), String> {
check_id("draft", id)
}
pub fn check_id(what: &str, id: &str) -> Result<(), String> {
let fits = (1..=64).contains(&id.len())
&& id
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_' || b == b'-');
if fits {
Ok(())
} else {
let id = super::cut(id);
Err(format!("{what} id {id:?} must be 1 to 64 of [a-z0-9_-]"))
}
}
impl Write {
pub fn opening_actor(&self) -> Option<String> {
self.actor.clone().or_else(|| self.stamp.clone())
}
pub fn check(&self) -> Result<(), String> {
check_draft_id(&self.draft)?;
if self.discard && (self.seal || !self.records.is_empty()) {
return Err("a discard carries no records and no seal".into());
}
if self.records.len() > 2_048 {
return Err("a part carries at most 2048 records".into());
}
let consecutive = self
.records
.windows(2)
.all(|pair| pair[0].seq.checked_add(1) == Some(pair[1].seq));
if !consecutive {
return Err("a part's records carry consecutive seq".into());
}
Ok(())
}
}
pub fn conflict(conversation_id: &str, reason: &str, details: Value) -> Refused {
let mut refused = Refused::conflict(conversation_id, reason);
if let (Value::Object(extra), Some(map)) = (details, refused.details.as_object_mut()) {
map.extend(extra);
}
refused
}
pub fn size_refusal(bytes: u64, maximum: u64) -> String {
format!(
"resource limit {RECORD_TURN_BYTES} exceeded: {bytes} > {maximum}; this store does not keep a turn this large"
)
}