Skip to content

Commit

Permalink
backend: Improve error types
Browse files Browse the repository at this point in the history
Implement Display and Error for error types
  • Loading branch information
dimtpap committed Mar 21, 2024
1 parent a416867 commit 29d5077
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 58 deletions.
38 changes: 26 additions & 12 deletions src/backend/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ use pipewire::{

use super::{util, Event, ObjectMethod};

#[derive(Debug)]
pub enum Error {
Unimplemented(ObjectType),
PipeWire(pw::Error),
}

impl From<pw::Error> for Error {
fn from(value: pw::Error) -> Self {
Self::PipeWire(value)
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unimplemented(object_type) => f.write_fmt(format_args!(
"Unsupported PipeWire object type {object_type}"
)),
Self::PipeWire(e) => f.write_fmt(format_args!("PipeWire error: {e}")),
}
}
}

impl std::error::Error for Error {}

// Objects whose methods aren't used get upcasted to a proxy
pub enum Global {
Client(pw::client::Client),
Expand Down Expand Up @@ -54,17 +79,6 @@ pub struct BoundGlobal {
_proxy_listener: pw::proxy::ProxyListener,
}

pub enum Error {
Unimplemented,
PipeWire(pw::Error),
}

impl From<pw::Error> for Error {
fn from(value: pw::Error) -> Self {
Self::PipeWire(value)
}
}

impl BoundGlobal {
pub fn bind_to<P: AsRef<DictRef>>(
registry: &pw::registry::Registry,
Expand Down Expand Up @@ -104,7 +118,7 @@ impl BoundGlobal {
listeners::metadata(registry.bind::<pw::metadata::Metadata, _>(global)?, id, sx)
}
_ => {
return Err(Error::Unimplemented);
return Err(Error::Unimplemented(global.type_.clone()));
}
};

Expand Down
61 changes: 39 additions & 22 deletions src/backend/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,45 @@ use pipewire as pw;

use super::{util, RemoteInfo};

#[derive(Debug)]
pub enum Error {
PipeWire(pw::Error),

#[cfg(feature = "xdg_desktop_portals")]
PortalUnavailable,
#[cfg(feature = "xdg_desktop_portals")]
Ashpd(ashpd::Error),
}

impl From<pw::Error> for Error {
fn from(value: pw::Error) -> Self {
Self::PipeWire(value)
}
}

#[cfg(feature = "xdg_desktop_portals")]
impl From<ashpd::Error> for Error {
fn from(value: ashpd::Error) -> Self {
Self::Ashpd(value)
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::PipeWire(e) => f.write_fmt(format_args!("Connecting to PipeWire failed: {e}")),

#[cfg(feature = "xdg_desktop_portals")]
Self::PortalUnavailable => f.write_str("Portal is unavailable"),

#[cfg(feature = "xdg_desktop_portals")]
Self::Ashpd(e) => f.write_fmt(format_args!("Connecting to the portal failed: {e}")),
}
}
}

impl std::error::Error for Error {}

#[cfg(feature = "xdg_desktop_portals")]
mod portals {
use std::os::fd::{FromRawFd, OwnedFd};
Expand Down Expand Up @@ -67,28 +106,6 @@ mod portals {
}
}

pub enum Error {
PipeWire(pw::Error),

#[cfg(feature = "xdg_desktop_portals")]
PortalUnavailable,
#[cfg(feature = "xdg_desktop_portals")]
Ashpd(ashpd::Error),
}

impl From<pw::Error> for Error {
fn from(value: pw::Error) -> Self {
Self::PipeWire(value)
}
}

#[cfg(feature = "xdg_desktop_portals")]
impl From<ashpd::Error> for Error {
fn from(value: ashpd::Error) -> Self {
Self::Ashpd(value)
}
}

#[cfg(not(feature = "xdg_desktop_portals"))]
pub struct Connection(pw::core::Core);
#[cfg(not(feature = "xdg_desktop_portals"))]
Expand Down
32 changes: 8 additions & 24 deletions src/backend/pipewire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::mpsc};

use crate::backend::connection;

use super::{
bind::{BoundGlobal, Error},
bind::BoundGlobal,
pw::{self, proxy::ProxyT, types::ObjectType},
util, Connection, Event, RemoteInfo, Request,
};
Expand All @@ -40,7 +42,7 @@ pub fn pipewire_thread(
pw::context::Context,
Connection,
Rc<pw::registry::Registry>,
) = match (|| {
) = match (|| -> Result<_, connection::Error> {
let mainloop = if mainloop_properties.is_empty() {
pw::main_loop::MainLoop::new(None)?
} else {
Expand All @@ -65,21 +67,10 @@ pub fn pipewire_thread(
})() {
Ok(instance) => instance,
Err(e) => {
use crate::backend::connection::Error;
match e {
Error::PipeWire(e) => {
eprintln!("Error initializing PipeWire: {e}");
}
#[cfg(feature = "xdg_desktop_portals")]
Error::PortalUnavailable => {
eprintln!("Portal unavailable");
}
#[cfg(feature = "xdg_desktop_portals")]
Error::Ashpd(e) => {
eprintln!("Error accessing portal: {e}");
}
}
eprintln!("Failed to connect to remote: {e}");

sx.send(Event::Stop).ok();

return;
}
};
Expand Down Expand Up @@ -287,14 +278,7 @@ pub fn pipewire_thread(
Ok(bound_global) => {
binds.borrow_mut().insert(id, bound_global);
}
Err(e) => match e {
Error::Unimplemented => {
eprintln!("Unsupported object type {}", global.type_);
}
Error::PipeWire(e) => {
eprintln!("Error binding object {id}: {e}");
}
},
Err(e) => eprintln!("Error binding object {id}: {e}"),
}
}
})
Expand Down

0 comments on commit 29d5077

Please sign in to comment.