dream_soft_render/
error.rs1use std::fmt;
4
5use crate::TextureId;
6
7#[derive(Clone, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12 SurfaceSize {
15 width: usize,
17 height: usize,
19 },
20 TextureSize {
23 width: usize,
25 height: usize,
27 },
28 PixelDataLength {
30 expected: usize,
32 actual: usize,
34 },
35 TextureBudget {
37 requested: usize,
39 budget: usize,
41 },
42 UnknownTexture(TextureId),
44 TextureIdsExhausted,
47 TextureUpdateOutOfBounds(TextureId),
49 NonFiniteRect,
51 NonFiniteVertex {
53 index: usize,
55 field: VertexField,
57 },
58 IndexCount(usize),
60 IndexOutOfRange {
62 index: u32,
64 vertex_count: usize,
66 },
67 Raster(String),
69}
70
71impl fmt::Display for Error {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match self {
74 Self::SurfaceSize { width, height } => {
75 write!(
76 f,
77 "surface size {width}x{height} overflows, exceeds the pixel budget, or is \
78 longer than 65535 pixels on a side"
79 )
80 }
81 Self::TextureSize { width, height } => {
82 write!(
83 f,
84 "texture size {width}x{height} is empty, overflows, or is longer than 65536 \
85 texels on a side"
86 )
87 }
88 Self::PixelDataLength { expected, actual } => {
89 write!(f, "pixel data is {actual} bytes, expected {expected}")
90 }
91 Self::TextureBudget { requested, budget } => {
92 write!(
93 f,
94 "texture storage would need {requested} bytes, over the {budget} byte budget"
95 )
96 }
97 Self::UnknownTexture(texture) => write!(f, "unknown or freed texture {texture}"),
98 Self::TextureIdsExhausted => f.write_str("every texture handle has been issued"),
99 Self::TextureUpdateOutOfBounds(texture) => {
100 write!(f, "update region exceeds the bounds of texture {texture}")
101 }
102 Self::NonFiniteRect => f.write_str("rectangle has a non-finite coordinate"),
103 Self::NonFiniteVertex { index, field } => {
104 write!(f, "mesh vertex {index} has a non-finite {field}")
105 }
106 Self::IndexCount(count) => {
107 write!(f, "mesh has {count} indices, not a multiple of three")
108 }
109 Self::IndexOutOfRange {
110 index,
111 vertex_count,
112 } => write!(
113 f,
114 "mesh index {index} is out of range for {vertex_count} vertices"
115 ),
116 Self::Raster(message) => write!(f, "rasterizer error: {message}"),
117 }
118 }
119}
120
121impl std::error::Error for Error {}
122
123#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
125pub enum VertexField {
126 Position,
128 Uv,
130}
131
132impl fmt::Display for VertexField {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 f.write_str(match self {
135 Self::Position => "position",
136 Self::Uv => "texture coordinate",
137 })
138 }
139}
140
141impl From<std::io::Error> for Error {
142 fn from(error: std::io::Error) -> Self {
143 Self::Raster(error.to_string())
144 }
145}