use crate::tier2::WorkRefusal;
fn factor(needed: u64, default: u64) -> u32 {
let n = needed.div_ceil(default.max(1)).max(1);
u32::try_from(n).unwrap_or(u32::MAX)
}
const TRUSTED: &str = "or with `WorkLimit::Unlimited` for trusted input";
pub(crate) fn packet(
work: WorkRefusal,
tile: usize,
spent: u64,
allowed: u64,
default: u64,
) -> String {
const OPTION: &str = "(see `DecodeOptions::packet_work_limit`)";
let left = allowed.saturating_sub(spent);
match work {
WorkRefusal::PacketWalk => {
let n = factor(allowed.saturating_add(1), default);
format!(
"reading the packet headers of tile {tile} goes past the packet work bound of \
{allowed} steps {OPTION}; retry with at least `WorkLimit::Raised({n})` (the \
file may need more), {TRUSTED}"
)
}
WorkRefusal::PocReach { reached } => {
let n = factor(spent.saturating_add(reached), default);
format!(
"the `POC` volumes of tile {tile} reach {reached} packets, a step each, more than the {left} \
steps left of the packet work bound of {allowed} {OPTION}; retry with at \
least `WorkLimit::Raised({n})` (later tiles may need more), {TRUSTED}"
)
}
WorkRefusal::DroppedBlocks { blocks } => {
let n = factor(spent.saturating_add(blocks), default);
format!(
"the resolutions the reduction drops hold {blocks} code-blocks in one \
component of tile {tile}, a step each, more than the {left} steps left of the \
packet work bound of {allowed} {OPTION}; retry with at least \
`WorkLimit::Raised({n})` (later tiles may need more), {TRUSTED}"
)
}
}
}
pub(crate) fn missing_tiles(cost: u64, allowed: u64, default: u64) -> String {
let n = factor(cost, default);
format!(
"the missing tiles cost {cost} steps, one for each tile-component, more than the \
packet work bound of {allowed} steps (see `DecodeOptions::packet_work_limit`); retry \
with at least `WorkLimit::Raised({n})`, {TRUSTED}"
)
}
pub(crate) fn samples(total: u64, default: u64) -> String {
match u32::try_from(total.div_ceil(default.max(1))) {
Ok(n) => format!("retry with at least `WorkLimit::Raised({n})`, {TRUSTED}"),
Err(_) => "only `WorkLimit::Unlimited` admits it, for trusted input".into(),
}
}
pub(crate) fn code_blocks(index: usize, work: u64, left: u64, spent: u64, default: u64) -> String {
let n = factor(spent.saturating_add(work), default);
format!(
"tile {index} needs {work} sample-passes to decode its code-blocks, more than the \
{left} left of the code-block work bound (see \
`DecodeOptions::code_block_work_limit`); retry with at least \
`WorkLimit::Raised({n})` (later tiles may need more), {TRUSTED}"
)
}
#[cfg(test)]
mod tests {
use super::*;
fn named(message: &str) -> u32 {
let (_, rest) = message.split_once("`WorkLimit::Raised(").unwrap();
rest.split_once(')').unwrap().0.parse().unwrap()
}
#[test]
fn code_blocks_add_the_work_already_spent() {
assert_eq!(named(&code_blocks(1, 10, 5, 100, 50)), 3);
assert_eq!(named(&code_blocks(1, 100, 0, 100, 50)), 4);
assert_eq!(named(&code_blocks(0, 100, 0, 0, 50)), 2);
}
#[test]
fn poc_volumes_add_the_visits_already_spent() {
let reach = WorkRefusal::PocReach { reached: 100 };
assert_eq!(named(&packet(reach, 0, 100, 150, 50)), 4);
assert_eq!(named(&packet(reach, 0, 0, 50, 50)), 2);
}
#[test]
fn dropped_blocks_add_the_visits_already_spent() {
let dropped = WorkRefusal::DroppedBlocks { blocks: 100 };
assert_eq!(named(&packet(dropped, 0, 100, 150, 50)), 4);
}
#[test]
fn the_packet_walk_names_the_next_factor_up() {
assert_eq!(named(&packet(WorkRefusal::PacketWalk, 0, 0, 50, 50)), 2);
assert_eq!(named(&packet(WorkRefusal::PacketWalk, 0, 0, 100, 50)), 3);
}
}