Skip to main content

dioxus_audio/
error.rs

1//! Audio errors shared by platform adapters.
2
3use 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    Overconstrained,
13    InvalidConfiguration,
14    RecorderFailure,
15    PlaybackFailure,
16    Backend,
17}
18
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub struct AudioError {
21    kind: AudioErrorKind,
22    message: String,
23    overconstrained_constraint: Option<String>,
24}
25
26impl AudioError {
27    pub fn new(kind: AudioErrorKind, message: impl Into<String>) -> Self {
28        Self {
29            kind,
30            message: message.into(),
31            overconstrained_constraint: None,
32        }
33    }
34
35    /// Create an exact-constraint acquisition failure.
36    ///
37    /// An empty browser constraint name is treated as unavailable detail.
38    pub fn overconstrained(constraint: impl Into<String>, message: impl Into<String>) -> Self {
39        let constraint = constraint.into();
40        Self {
41            kind: AudioErrorKind::Overconstrained,
42            message: message.into(),
43            overconstrained_constraint: (!constraint.is_empty()).then_some(constraint),
44        }
45    }
46
47    pub fn unsupported() -> Self {
48        Self::new(
49            AudioErrorKind::UnsupportedPlatform,
50            "audio capture and playback require the wasm32 web backend",
51        )
52    }
53
54    pub fn kind(&self) -> AudioErrorKind {
55        self.kind
56    }
57
58    pub fn message(&self) -> &str {
59        &self.message
60    }
61
62    /// Browser constraint name associated with an overconstraint failure.
63    pub fn overconstrained_constraint(&self) -> Option<&str> {
64        self.overconstrained_constraint.as_deref()
65    }
66}
67
68impl fmt::Display for AudioError {
69    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70        formatter.write_str(&self.message)
71    }
72}
73
74impl std::error::Error for AudioError {}