use crate::types::MixLane;
use crate::{Error, Result};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Operation {
Enumerate,
Start,
Normalize,
Flush,
Reopen,
Rollback,
Stop,
Join,
Link,
}
impl fmt::Display for Operation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Enumerate => "enumerate",
Self::Start => "start",
Self::Normalize => "normalize",
Self::Flush => "flush",
Self::Reopen => "reopen",
Self::Rollback => "rollback",
Self::Stop => "stop",
Self::Join => "join",
Self::Link => "link",
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum NativeStatus {
HResult {
call: &'static str,
bits: u32,
},
OsStatus {
call: &'static str,
value: i32,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ErrorContext {
operation: Operation,
lane: Option<MixLane>,
native_status: Option<NativeStatus>,
}
impl ErrorContext {
pub fn new(operation: Operation) -> Self {
Self {
operation,
lane: None,
native_status: None,
}
}
pub fn with_lane(mut self, lane: MixLane) -> Self {
self.lane = Some(lane);
self
}
pub fn with_native_status(mut self, status: NativeStatus) -> Self {
self.native_status = Some(status);
self
}
pub fn operation(&self) -> Operation {
self.operation
}
pub fn lane(&self) -> Option<MixLane> {
self.lane
}
pub fn native_status(&self) -> Option<NativeStatus> {
self.native_status
}
}
impl fmt::Display for ErrorContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "during {}", self.operation)?;
if let Some(lane) = self.lane {
f.write_str(match lane {
MixLane::Microphone => " (microphone)",
MixLane::SystemAudio => " (system audio)",
})?;
}
match self.native_status {
Some(NativeStatus::HResult { bits, .. }) => write!(f, " (HRESULT 0x{bits:08X})"),
Some(NativeStatus::OsStatus { value, .. }) => write!(f, " (OSStatus {value})"),
None => Ok(()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ErrorGroup {
primary: Box<Error>,
first_secondary: Box<Error>,
remaining_secondary: Vec<Error>,
}
impl ErrorGroup {
pub fn new(primary: Error, first_secondary: Error, remaining_secondary: Vec<Error>) -> Self {
Self {
primary: Box::new(primary),
first_secondary: Box::new(first_secondary),
remaining_secondary,
}
}
pub fn primary(&self) -> &Error {
&self.primary
}
pub fn secondary(&self) -> impl Iterator<Item = &Error> {
std::iter::once(self.first_secondary.as_ref()).chain(self.remaining_secondary.iter())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ShutdownReport {
primary: Option<Error>,
cleanup: Vec<Error>,
}
impl ShutdownReport {
pub fn new(primary: Option<Error>, cleanup: Vec<Error>) -> Self {
Self { primary, cleanup }
}
pub fn primary(&self) -> Option<&Error> {
self.primary.as_ref()
}
pub fn cleanup(&self) -> &[Error] {
&self.cleanup
}
pub fn result(&self) -> Result<()> {
let mut causes = self.primary.iter().chain(self.cleanup.iter()).cloned();
match (causes.next(), causes.next()) {
(None, _) => Ok(()),
(Some(error), None) => Err(error),
(Some(primary), Some(first)) => Err(Error::Multiple(ErrorGroup::new(
primary,
first,
causes.collect(),
))),
}
}
}