use std::{cell::RefCell, fmt, rc::Rc};
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
pub enum LayoutDiagnostic {
#[error("{0} did not reach a fixed layout; the last completed layout is kept")]
Unsettled(Convergence),
#[error("layout exceeds {limit} pages; content after the last page is omitted")]
PageLimit { limit: usize },
#[error("layout exceeds {limit} work units; {kept}")]
WorkLimit { limit: usize, kept: WorkFallback },
#[error("a box layout cannot form is omitted: {reason}")]
OmittedBox { reason: String },
#[error("re-laying out failed ({reason}); the last completed layout is kept")]
FailedRelayout { reason: String },
#[error(
"fixed boxes repeated on every page exceed {limit} placements; they repeat on the first \
{pages} pages"
)]
FixedPlacementLimit { limit: usize, pages: usize },
}
impl LayoutDiagnostic {
pub fn loses_content(&self) -> bool {
match self {
Self::PageLimit { .. } | Self::OmittedBox { .. } | Self::FixedPlacementLimit { .. } => {
true
}
Self::WorkLimit { kept, .. } => match kept {
WorkFallback::Truncated => true,
WorkFallback::LastPass => false,
},
Self::Unsettled(_) | Self::FailedRelayout { .. } => false,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Convergence {
AnchorPositioning,
GeneratedContent,
ContainerQueries,
ScrollMarkers,
ElementImages,
PageSizes,
FootnoteDeferrals,
}
impl fmt::Display for Convergence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::AnchorPositioning => "anchor positioning",
Self::GeneratedContent => "generated content",
Self::ContainerQueries => "container query sizing",
Self::ScrollMarkers => "scroll marker activation",
Self::ElementImages => "element() image rendering",
Self::PageSizes => "content-sized pages",
Self::FootnoteDeferrals => "footnote call deferral",
})
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum WorkFallback {
LastPass,
Truncated,
}
impl fmt::Display for WorkFallback {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::LastPass => "the last completed layout is kept",
Self::Truncated => "content from where the budget ran out is omitted",
})
}
}
#[derive(Clone, Default)]
pub(crate) struct Diagnostics(Rc<RefCell<Vec<LayoutDiagnostic>>>);
impl Diagnostics {
pub(crate) fn record(&self, diagnostic: LayoutDiagnostic) {
let mut recorded = self.0.borrow_mut();
if !recorded.contains(&diagnostic) {
recorded.push(diagnostic);
}
}
pub(crate) fn take(&self) -> Vec<LayoutDiagnostic> {
std::mem::take(&mut self.0.borrow_mut())
}
}