Skip to main content

pulsectl/
error.rs

1use pulse::error::PAErr;
2
3use crate::controllers::error::ControllerError;
4
5/// Error thrown when PulseAudio throws an error code.
6#[derive(Debug, Clone)]
7pub enum Error {
8    /// An error that may occur while establishing a connection.
9    Connect(String),
10    /// The requested operation is cancelled or quits unexpectedly.
11    Operation(String),
12    /// PulseAudio returns an error code in any circumstance.
13    PulseAudio(String),
14    /// A problem occurs while fetching data from pulseaudio.
15    Controller(ControllerError),
16}
17
18impl std::error::Error for Error {}
19
20impl std::fmt::Display for Error {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            Self::Connect(e) => f.write_str(&format!("ConnectError: {}", e)),
24            Self::Operation(e) => f.write_str(&format!("OperationError: {}", e)),
25            Self::PulseAudio(e) => f.write_str(&format!("PulseAudioError: {}", e)),
26            Self::Controller(e) => f.write_str(&format!("ControllerError: {}", e)),
27        }
28    }
29}
30
31impl From<PAErr> for Error {
32    fn from(error: PAErr) -> Self {
33        Self::PulseAudio(
34            error
35                .to_string()
36                .unwrap_or_else(|| "Unknown error".to_string()),
37        )
38    }
39}