1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! The error type shared by the encoder, decoder, and packet utilities.
use std::fmt;
/// Why an encoder, decoder, or packet operation failed.
///
/// The variants mirror libopus' error codes ([`Error::code`] returns the libopus
/// integer), and each carries a short static description of the specific
/// condition. `Display` prints that description, so code that formatted the
/// former `&'static str` errors produces identical text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Error {
/// An argument was outside its valid range (libopus `OPUS_BAD_ARG`).
BadArg(&'static str),
/// The caller's output buffer cannot hold the result
/// (libopus `OPUS_BUFFER_TOO_SMALL`).
BufferTooSmall(&'static str),
/// The input packet is malformed, truncated, or otherwise not valid Opus
/// (libopus `OPUS_INVALID_PACKET`).
InvalidPacket(&'static str),
/// An internal codec stage failed (libopus `OPUS_INTERNAL_ERROR`).
Internal(&'static str),
}
impl Error {
/// The equivalent libopus error code (always negative).
#[must_use]
pub const fn code(&self) -> i32 {
match self {
Self::BadArg(_) => -1,
Self::BufferTooSmall(_) => -2,
Self::Internal(_) => -3,
Self::InvalidPacket(_) => -4,
}
}
/// The description of the specific condition.
#[must_use]
pub const fn message(&self) -> &'static str {
match self {
Self::BadArg(m)
| Self::BufferTooSmall(m)
| Self::InvalidPacket(m)
| Self::Internal(m) => m,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.message())
}
}
impl std::error::Error for Error {}