Skip to main content

astrelis_text/
error.rs

1/// Errors that can occur in the text rendering system.
2#[derive(Debug, Clone)]
3pub enum TextError {
4    /// Font loading failed.
5    FontLoadError(String),
6
7    /// Font file not found.
8    FontFileNotFound(std::path::PathBuf),
9
10    /// Invalid font data.
11    InvalidFontData(String),
12
13    /// Lock was poisoned (RwLock/Mutex).
14    LockPoisoned(String),
15
16    /// Text shaping failed.
17    ShapingError(String),
18
19    /// Buffer allocation failed.
20    BufferAllocationFailed(String),
21
22    /// Texture atlas is full.
23    AtlasFull {
24        requested_width: u32,
25        requested_height: u32,
26        atlas_width: u32,
27        atlas_height: u32,
28    },
29
30    /// GPU resource creation failed.
31    GpuResourceError(String),
32
33    /// Invalid text range.
34    InvalidRange {
35        start: usize,
36        end: usize,
37        text_len: usize,
38    },
39
40    /// Generic IO error.
41    IoError(String),
42}
43
44impl std::fmt::Display for TextError {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        match self {
47            TextError::FontLoadError(msg) => write!(f, "Failed to load font: {}", msg),
48            TextError::FontFileNotFound(path) => {
49                write!(f, "Font file not found: {}", path.display())
50            }
51            TextError::InvalidFontData(msg) => write!(f, "Invalid font data: {}", msg),
52            TextError::LockPoisoned(msg) => {
53                write!(
54                    f,
55                    "Lock was poisoned (likely due to panic in another thread): {}",
56                    msg
57                )
58            }
59            TextError::ShapingError(msg) => write!(f, "Text shaping failed: {}", msg),
60            TextError::BufferAllocationFailed(msg) => {
61                write!(f, "Buffer allocation failed: {}", msg)
62            }
63            TextError::AtlasFull {
64                requested_width,
65                requested_height,
66                atlas_width,
67                atlas_height,
68            } => write!(
69                f,
70                "Texture atlas is full: requested {}x{} but atlas is {}x{}",
71                requested_width, requested_height, atlas_width, atlas_height
72            ),
73            TextError::GpuResourceError(msg) => write!(f, "GPU resource error: {}", msg),
74            TextError::InvalidRange {
75                start,
76                end,
77                text_len,
78            } => write!(
79                f,
80                "Invalid text range: {}..{} (text length: {})",
81                start, end, text_len
82            ),
83            TextError::IoError(msg) => write!(f, "IO error: {}", msg),
84        }
85    }
86}
87
88impl std::error::Error for TextError {}
89
90impl From<std::io::Error> for TextError {
91    fn from(err: std::io::Error) -> Self {
92        TextError::IoError(err.to_string())
93    }
94}
95
96impl<T> From<std::sync::PoisonError<T>> for TextError {
97    fn from(err: std::sync::PoisonError<T>) -> Self {
98        TextError::LockPoisoned(err.to_string())
99    }
100}
101
102/// Result type for text operations.
103pub type TextResult<T> = Result<T, TextError>;