use core::fmt;
use core::ops::Range;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DecodeReport {
pub entries: Vec<ReportEntry>,
pub complete_reduction: Option<u8>,
reduction: u8,
}
impl Default for DecodeReport {
fn default() -> Self {
DecodeReport {
entries: Vec::new(),
complete_reduction: Some(0),
reduction: 0,
}
}
}
impl DecodeReport {
pub(crate) fn new(
entries: Vec<ReportEntry>,
complete_reduction: Option<u8>,
reduction: u8,
) -> Self {
if entries.is_empty() && complete_reduction == Some(0) {
return DecodeReport::default();
}
DecodeReport {
entries,
complete_reduction,
reduction,
}
}
#[must_use]
pub fn is_complete(&self) -> bool {
self.complete_reduction
.is_some_and(|complete| complete <= self.reduction)
&& !self.entries.iter().any(|entry| {
matches!(
entry,
ReportEntry::MissingTiles { .. } | ReportEntry::LeftoverBytes { .. }
)
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ReportEntry {
#[non_exhaustive]
MissingTiles {
tiles: Range<u32>,
},
#[non_exhaustive]
MissingPackets {
tile: u32,
complete_layers: u16,
complete_reduction: Option<u8>,
},
#[non_exhaustive]
TruncatedTilePart {
tile: u32,
tile_part: u8,
declared: u64,
present: u64,
},
#[non_exhaustive]
TilePartCountMismatch {
tile: u32,
declared: u16,
found: u16,
},
#[non_exhaustive]
LeftoverBytes {
tile: u32,
tile_part: u8,
bytes: u64,
in_packed_headers: bool,
},
#[non_exhaustive]
MissingEoc,
}
impl fmt::Display for ReportEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ReportEntry::MissingTiles { tiles } if tiles.len() == 1 => {
write!(f, "tile {} is missing; its samples are 0", tiles.start)
}
ReportEntry::MissingTiles { tiles } => write!(
f,
"tiles {} to {} are missing; their samples are 0",
tiles.start,
tiles.end.saturating_sub(1)
),
ReportEntry::MissingPackets {
tile,
complete_layers,
complete_reduction,
} => {
write!(
f,
"tile {tile} has packets missing; {complete_layers} layer(s) are whole"
)?;
match complete_reduction {
Some(r) => write!(f, ", and every packet is there at reduction {r}"),
None => write!(f, ", and no reduction has every packet"),
}
}
ReportEntry::TruncatedTilePart {
tile,
tile_part,
declared,
present,
} => write!(
f,
"tile {tile} tile-part {tile_part} is cut short: {present} of its {declared} \
bytes are there"
),
ReportEntry::TilePartCountMismatch {
tile,
declared,
found,
} => write!(
f,
"tile {tile} declares {declared} tile-part(s) and {found} were found"
),
ReportEntry::LeftoverBytes {
tile,
tile_part,
bytes,
in_packed_headers,
} => {
let place = if *in_packed_headers {
"packed packet header"
} else {
"tile data"
};
write!(
f,
"tile {tile} has {bytes} byte(s) of {place} left from tile-part \
{tile_part} on, not read; its samples may be wrong"
)
}
ReportEntry::MissingEoc => write!(f, "the codestream ends without its `EOC` marker"),
}
}
}