1use core::fmt;
2
3use crate::Fourcc;
4
5pub type Result<T> = core::result::Result<T, Error>;
7
8#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error {
12 #[cfg(feature = "std")]
14 Io(std::io::Error),
15 NotRiff {
17 found: [u8; 4],
19 },
20 TruncatedHeader {
23 offset: u64,
25 available: u64,
27 },
28 Truncated {
30 id: Fourcc,
32 declared: u64,
34 available: u64,
36 },
37 ChildExceedsParent {
39 id: Fourcc,
41 declared: u64,
43 remaining: u64,
45 },
46 MissingPad {
48 id: Fourcc,
50 offset: u64,
52 },
53 ListTooShort {
55 id: Fourcc,
57 size: u64,
59 },
60 TooDeep {
62 max: usize,
64 },
65 MissingDs64 {
67 found: Fourcc,
69 },
70 BadDs64 {
72 size: u64,
74 },
75 UnresolvedSize {
77 id: Fourcc,
79 },
80 NotAList {
82 id: Fourcc,
84 },
85 NotInList,
87 NoOpenChunk,
89 BufferTooSmall {
91 needed: u64,
93 },
94 TooLarge {
96 size: u64,
98 },
99 Unbalanced {
101 open: usize,
103 },
104 Unsupported(Feature),
106}
107
108#[derive(Clone, Copy, PartialEq, Eq, Debug)]
110#[non_exhaustive]
111pub enum Feature {
112 Rf64Write,
114}
115
116impl fmt::Display for Error {
117 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
118 match self {
119 #[cfg(feature = "std")]
120 Self::Io(error) => write!(formatter, "stream error: {error}"),
121 Self::NotRiff { found } => write!(formatter, "unsupported form id: {}", Fourcc(*found)),
122 Self::TruncatedHeader { offset, available } => write!(
123 formatter,
124 "incomplete header at offset {offset}: {available} bytes available"
125 ),
126 Self::Truncated {
127 id,
128 declared,
129 available,
130 } => write!(
131 formatter,
132 "chunk {id} declares {declared} bytes with {available} available"
133 ),
134 Self::ChildExceedsParent {
135 id,
136 declared,
137 remaining,
138 } => write!(
139 formatter,
140 "chunk {id} declares {declared} bytes with {remaining} bytes left in its list"
141 ),
142 Self::MissingPad { id, offset } => {
143 write!(
144 formatter,
145 "chunk {id} is missing its pad byte at offset {offset}"
146 )
147 }
148 Self::ListTooShort { id, size } => {
149 write!(formatter, "list {id} has a {size}-byte payload")
150 }
151 Self::TooDeep { max } => write!(formatter, "list nesting exceeds {max} levels"),
152 Self::MissingDs64 { found } => {
153 write!(formatter, "64-bit form starts with {found} instead of ds64")
154 }
155 Self::BadDs64 { size } => write!(formatter, "invalid ds64 payload size {size}"),
156 Self::UnresolvedSize { id } => write!(formatter, "no 64-bit size for chunk {id}"),
157 Self::NotAList { id } => write!(formatter, "chunk {id} is not a list"),
158 Self::NotInList => formatter.write_str("the reader is at the top level"),
159 Self::NoOpenChunk => formatter.write_str("no writable chunk is open"),
160 Self::BufferTooSmall { needed } => {
161 write!(formatter, "payload buffer needs {needed} bytes")
162 }
163 Self::TooLarge { size } => write!(formatter, "size {size} exceeds a 32-bit field"),
164 Self::Unbalanced { open } => write!(formatter, "writer has {open} open scopes"),
165 Self::Unsupported(Feature::Rf64Write) => {
166 formatter.write_str("RF64 and BW64 writing is unsupported")
167 }
168 }
169 }
170}
171
172#[cfg(feature = "std")]
173impl std::error::Error for Error {
174 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
175 match self {
176 Self::Io(error) => Some(error),
177 _ => None,
178 }
179 }
180}
181
182#[cfg(feature = "std")]
183impl From<std::io::Error> for Error {
184 fn from(error: std::io::Error) -> Self {
185 Self::Io(error)
186 }
187}
188
189#[derive(Clone, Copy, Debug)]
190pub(crate) enum Failure {
191 #[cfg(feature = "std")]
192 Io(std::io::ErrorKind),
193 TruncatedHeader(u64, u64),
194 Truncated(Fourcc, u64, u64),
195 ChildExceedsParent(Fourcc, u64, u64),
196 MissingPad(Fourcc, u64),
197 ListTooShort(Fourcc, u64),
198 #[cfg(feature = "std")]
199 TooDeep,
200 #[cfg(feature = "std")]
201 UnresolvedSize(Fourcc),
202}
203
204impl Failure {
205 pub(crate) fn into_error(self) -> Error {
206 match self {
207 #[cfg(feature = "std")]
208 Self::Io(kind) => Error::Io(std::io::Error::from(kind)),
209 Self::TruncatedHeader(offset, available) => {
210 Error::TruncatedHeader { offset, available }
211 }
212 Self::Truncated(id, declared, available) => Error::Truncated {
213 id,
214 declared,
215 available,
216 },
217 Self::ChildExceedsParent(id, declared, remaining) => Error::ChildExceedsParent {
218 id,
219 declared,
220 remaining,
221 },
222 Self::MissingPad(id, offset) => Error::MissingPad { id, offset },
223 Self::ListTooShort(id, size) => Error::ListTooShort { id, size },
224 #[cfg(feature = "std")]
225 Self::TooDeep => Error::TooDeep {
226 max: crate::MAX_DEPTH,
227 },
228 #[cfg(feature = "std")]
229 Self::UnresolvedSize(id) => Error::UnresolvedSize { id },
230 }
231 }
232}