const PART_ROLE: &str = "part.";
const CHUNK_ROLE: &str = "chunk.";
const ON_DEMAND_ROLE: &str = "on-demand";
const HEAD_ROLE: &str = "head";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DocumentSync<'a> {
Eager,
OnDemand,
Part {
base: &'a str,
index: u64,
},
Chunk {
base: &'a str,
number: u64,
},
ChainHead {
base: &'a str,
},
}
#[must_use]
pub fn document_sync(record: &str) -> DocumentSync<'_> {
let Some((role, base)) = record
.strip_prefix("derived:")
.and_then(|derived| derived.split_once('@'))
else {
return DocumentSync::Eager;
};
if role == ON_DEMAND_ROLE {
return DocumentSync::OnDemand;
}
if role == HEAD_ROLE {
return DocumentSync::ChainHead { base };
}
if let Some(index) = role.strip_prefix(PART_ROLE) {
return match numbered(index) {
Some(index) => DocumentSync::Part { base, index },
None => DocumentSync::Eager,
};
}
let Some(number) = role.strip_prefix(CHUNK_ROLE) else {
return DocumentSync::Eager;
};
match numbered(number) {
Some(number) if number > 0 => DocumentSync::Chunk { base, number },
_ => DocumentSync::Eager,
}
}
fn numbered(index: &str) -> Option<u64> {
let canonical = !index.is_empty()
&& index.bytes().all(|byte| byte.is_ascii_digit())
&& (index == "0" || !index.starts_with('0'));
canonical
.then(|| {
tilo_failure::ignored!(
"a probe of the id's shape: only a number past u64 fails, and it names no part",
index.parse().ok()
)
})
.flatten()
}
#[must_use]
pub fn document_part(base: &str, index: u64) -> String {
format!("derived:{PART_ROLE}{index}@{base}")
}
#[must_use]
pub fn chain_head(base: &str) -> String {
format!("derived:{HEAD_ROLE}@{base}")
}
#[must_use]
pub fn document_chunk(base: &str, number: u64) -> String {
format!("derived:{CHUNK_ROLE}{number}@{base}")
}
#[must_use]
pub fn on_demand_document(base: &str) -> String {
format!("derived:{ON_DEMAND_ROLE}@{base}")
}
#[cfg(test)]
mod tests {
use super::*;
tilo_test_env::scenario! {
fn a_record_declares_how_it_syncs;
phase a_record_id_declares_how_it_syncs;
{
assert_eq!(document_sync("general"), DocumentSync::Eager);
assert_eq!(
document_sync("derived:default@tab:0000000000000000"),
DocumentSync::Eager
);
assert_eq!(
document_sync(&on_demand_document("uuid:1")),
DocumentSync::OnDemand
);
assert_eq!(
document_sync(&document_part("chat", 12)),
DocumentSync::Part {
base: "chat",
index: 12
}
);
assert_eq!(
document_sync(&document_part("", 0)),
DocumentSync::Part { base: "", index: 0 }
);
assert_eq!(
document_sync(&chain_head("chat")),
DocumentSync::ChainHead { base: "chat" }
);
for other in [
"derived:part.01@chat",
"derived:part.@chat",
"derived:part.1x@chat",
"derived:part.-1@chat",
] {
assert_eq!(document_sync(other), DocumentSync::Eager, "{other}");
}
}
phase a_chunk_is_a_numbered_record_sent_when_opened;
{
assert_eq!(
document_sync(&document_chunk("general", 3)),
DocumentSync::Chunk {
base: "general",
number: 3
}
);
for other in [
"general",
"derived:chunk.0@general",
"derived:chunk.01@general",
"derived:chunk.@general",
"derived:chunk.1x@general",
] {
assert_eq!(document_sync(other), DocumentSync::Eager, "{other}");
}
}
}
}