1use std::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum AudioErrorKind {
8 UnsupportedPlatform,
9 PermissionDenied,
10 DeviceNotFound,
11 DeviceUnavailable,
12 InvalidConfiguration,
13 RecorderFailure,
14 PlaybackFailure,
15 Backend,
16}
17
18#[derive(Clone, Debug, PartialEq, Eq)]
19pub struct AudioError {
20 kind: AudioErrorKind,
21 message: String,
22}
23
24impl AudioError {
25 pub fn new(kind: AudioErrorKind, message: impl Into<String>) -> Self {
26 Self {
27 kind,
28 message: message.into(),
29 }
30 }
31
32 pub fn unsupported() -> Self {
33 Self::new(
34 AudioErrorKind::UnsupportedPlatform,
35 "audio capture and playback require the wasm32 web backend",
36 )
37 }
38
39 pub fn kind(&self) -> AudioErrorKind {
40 self.kind
41 }
42
43 pub fn message(&self) -> &str {
44 &self.message
45 }
46}
47
48impl fmt::Display for AudioError {
49 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
50 formatter.write_str(&self.message)
51 }
52}
53
54impl std::error::Error for AudioError {}