1use std::{collections::TryReserveError, error::Error, fmt};
2
3use sim_engine::{FontError, LogicalScreenPosition, ShapedLineError};
4
5use crate::screen::ScreenVisualError;
6
7#[derive(Debug)]
13#[non_exhaustive]
14pub enum TextError {
15 InvalidAtlasLimits,
17 ForeignFont,
19 Font(FontError),
21 Prepared(ShapedLineError),
23 FontLimitExceeded {
25 limit: usize,
27 },
28 FontByteLimitExceeded {
30 limit: usize,
32 retained: usize,
34 incoming: usize,
36 },
37 RunBudgetExceeded {
39 glyphs: usize,
41 max_glyphs: usize,
43 required_bytes: usize,
45 max_retained_bytes: usize,
47 },
48 Screen(ScreenVisualError),
50 InvalidBounds {
52 position: LogicalScreenPosition,
54 },
55 AllocationFailed {
57 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}