1use crate::{pixel::PixelFormat, sample::SampleType};
4
5#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7#[non_exhaustive]
8pub enum BufferError {
9 #[error("output buffer too small: required {required} bytes, have {have}")]
11 OutputTooSmall {
12 required: usize,
14 have: usize,
16 },
17 #[error("input buffer too small: required {required} bytes, have {have}")]
19 InputTooSmall {
20 required: usize,
22 have: usize,
24 },
25 #[error("buffer size overflow while computing {what}")]
27 SizeOverflow {
28 what: &'static str,
30 },
31 #[error("{what} allocation too large: requested {requested} bytes, cap {cap}")]
33 AllocationTooLarge {
34 requested: usize,
36 cap: usize,
38 what: &'static str,
40 },
41 #[error("host allocation failed for {bytes} bytes while allocating {what}")]
43 HostAllocationFailed {
44 bytes: usize,
46 what: &'static str,
48 },
49 #[error("stride {stride} is smaller than row width {row_bytes}")]
51 StrideTooSmall {
52 row_bytes: usize,
54 stride: usize,
56 },
57 #[error("stride {stride} is not aligned to {align}")]
59 StrideNotAligned {
60 stride: usize,
62 align: usize,
64 },
65 #[error("pixel format {fmt:?} does not match sample type {sample_type:?}")]
67 SampleTypeMismatch {
68 fmt: PixelFormat,
70 sample_type: SampleType,
72 },
73}
74
75#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
77pub enum InputError {
78 #[error("input too short: need {need} bytes, have {have}")]
80 TooShort {
81 need: usize,
83 have: usize,
85 },
86 #[error("input truncated at offset {offset} while reading {segment}")]
88 TruncatedAt {
89 offset: usize,
91 segment: &'static str,
93 },
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
98#[error("not yet implemented: {what}")]
99pub struct NotImplemented {
100 pub what: &'static str,
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
106#[error("unsupported: {what}")]
107pub struct Unsupported {
108 pub what: &'static str,
110}
111
112pub trait CodecError: core::error::Error + Send + Sync + 'static {
114 fn is_truncated(&self) -> bool;
116 fn is_not_implemented(&self) -> bool;
118 fn is_unsupported(&self) -> bool;
120 fn is_buffer_error(&self) -> bool;
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126#[non_exhaustive]
127pub enum AdapterErrorKind {
128 Other,
130 Buffer,
132 Unsupported,
134}
135
136pub trait AdapterErrorParts {
138 fn source_codec_error(&self) -> Option<&dyn CodecError>;
140
141 fn adapter_error_kind(&self) -> AdapterErrorKind;
143}
144
145#[doc(hidden)]
147pub fn adapter_error_is_truncated(error: &impl AdapterErrorParts) -> bool {
148 error
149 .source_codec_error()
150 .is_some_and(CodecError::is_truncated)
151}
152
153#[doc(hidden)]
155pub fn adapter_error_is_not_implemented(error: &impl AdapterErrorParts) -> bool {
156 error
157 .source_codec_error()
158 .is_some_and(CodecError::is_not_implemented)
159}
160
161#[doc(hidden)]
163pub fn adapter_error_is_unsupported(error: &impl AdapterErrorParts) -> bool {
164 error.adapter_error_kind() == AdapterErrorKind::Unsupported
165 || error
166 .source_codec_error()
167 .is_some_and(CodecError::is_unsupported)
168}
169
170#[doc(hidden)]
172pub fn adapter_error_is_buffer_error(error: &impl AdapterErrorParts) -> bool {
173 error.adapter_error_kind() == AdapterErrorKind::Buffer
174 || error
175 .source_codec_error()
176 .is_some_and(CodecError::is_buffer_error)
177}