Skip to main content

sim_logic/text/
error.rs

1use std::{collections::TryReserveError, error::Error, fmt};
2
3use sim_engine::{FontError, LogicalScreenPosition, ShapedLineError};
4
5use crate::screen::ScreenVisualError;
6
7/// Rejected font registration or label change; prior published values survive.
8///
9/// Font parsing, shaping, and owned vector reservation happen before a new
10/// label or registration is installed. As elsewhere in Rust, shared-handle
11/// control-block allocation and dependency internals are not an OOM sandbox.
12#[derive(Debug)]
13#[non_exhaustive]
14pub enum TextError {
15    /// Invalid atlas dimensions/cache count or run count/byte limits.
16    InvalidAtlasLimits,
17    /// A style was requested from a font registered by another application.
18    ForeignFont,
19    /// Engine rejected trusted font bytes, single-line input, or font geometry.
20    Font(FontError),
21    /// Prepared shaping state belongs to a different font/style or exceeds limits.
22    Prepared(ShapedLineError),
23    /// The application has reached its fixed registration-count limit.
24    FontLimitExceeded {
25        /// Maximum distinct font registrations.
26        limit: usize,
27    },
28    /// Source Vec capacity would exceed the application's total font budget.
29    FontByteLimitExceeded {
30        /// Aggregate permitted font source capacity in bytes.
31        limit: usize,
32        /// Capacity already retained by successful registrations.
33        retained: usize,
34        /// Capacity of the rejected incoming font source.
35        incoming: usize,
36    },
37    /// Shaped glyphs cannot fit the configured desktop run even before rasterization.
38    RunBudgetExceeded {
39        /// Number of shaped glyphs, conservatively including whitespace.
40        glyphs: usize,
41        /// Configured maximum glyph count for one retained run.
42        max_glyphs: usize,
43        /// Conservative retained per-glyph storage bytes required by this line.
44        required_bytes: usize,
45        /// Configured per-run retained-byte ceiling.
46        max_retained_bytes: usize,
47    },
48    /// Position, tint, or draw-order depth is invalid.
49    Screen(ScreenVisualError),
50    /// Aligned baseline or typographic bounds overflow or lose nonzero extent.
51    InvalidBounds {
52        /// Requested logical baseline anchor before alignment.
53        position: LogicalScreenPosition,
54    },
55    /// Owned string or registry metadata could not be reserved before publication.
56    AllocationFailed {
57        /// Original fallible allocator error.
58        source: TryReserveError,
59    },
60}
61
62impl fmt::Display for TextError {
63    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
64        match self {
65            Self::InvalidAtlasLimits => formatter.write_str("invalid text atlas/run limits"),
66            Self::ForeignFont => formatter.write_str("font is not registered by this application"),
67            Self::Font(error) => write!(formatter, "text font: {error}"),
68            Self::Prepared(error) => write!(formatter, "text preparation: {error}"),
69            Self::FontLimitExceeded { limit } => {
70                write!(
71                    formatter,
72                    "text font registry reached its {limit}-font limit"
73                )
74            }
75            Self::FontByteLimitExceeded {
76                limit,
77                retained,
78                incoming,
79            } => write!(
80                formatter,
81                "font source capacity {retained} plus {incoming} bytes exceeds limit {limit}"
82            ),
83            Self::RunBudgetExceeded {
84                glyphs,
85                max_glyphs,
86                required_bytes,
87                max_retained_bytes,
88            } => write!(
89                formatter,
90                "text run needs {glyphs} glyphs and {required_bytes} retained bytes; limits are {max_glyphs} and {max_retained_bytes}"
91            ),
92            Self::Screen(error) => write!(formatter, "screen text: {error}"),
93            Self::InvalidBounds { position } => write!(
94                formatter,
95                "text baseline and typographic bounds are not representable at {position:?}"
96            ),
97            Self::AllocationFailed { source } => {
98                write!(formatter, "text allocation failed: {source}")
99            }
100        }
101    }
102}
103
104impl Error for TextError {
105    fn source(&self) -> Option<&(dyn Error + 'static)> {
106        match self {
107            Self::Font(error) => Some(error),
108            Self::Prepared(error) => Some(error),
109            Self::Screen(error) => Some(error),
110            Self::AllocationFailed { source } => Some(source),
111            _ => None,
112        }
113    }
114}
115
116impl From<FontError> for TextError {
117    fn from(error: FontError) -> Self {
118        Self::Font(error)
119    }
120}
121
122impl From<ShapedLineError> for TextError {
123    fn from(error: ShapedLineError) -> Self {
124        Self::Prepared(error)
125    }
126}
127
128impl From<ScreenVisualError> for TextError {
129    fn from(error: ScreenVisualError) -> Self {
130        Self::Screen(error)
131    }
132}