Skip to content

Commit

Permalink
use stable versions and features to be more crates.io-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
l1na-forever committed Jul 19, 2022
1 parent d5e0b82 commit 2945592
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ album-art = ["ureq", "image"]
# Macro to generate error types from enums
thiserror = "1"

# Pure Rust library for accessing D-Bus. A specific revision ahead of latest
# release (0.17) is used to support Marshal/Unmarshal on enums, as well as get
# back better typed errors. Move to 0.18 when 0.18 is cut.
rustbus = { git = "https://github.com/KillingSpark/rustbus", rev = "1dfd01d0ef6be915a7a50d501da849703b636f68" }
rustbus_derive = { git = "https://github.com/KillingSpark/rustbus", rev = "1dfd01d0ef6be915a7a50d501da849703b636f68" }
# Pure Rust library for accessing D-Bus.
rustbus = "0.17"

# Logging facade and simple output logger
log = "0.4"
Expand Down
29 changes: 26 additions & 3 deletions src/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use thiserror::Error;
#[derive(Debug, Error)]
pub enum DBusError {
#[error("error connecting to D-Bus")]
Connection(#[from] rustbus::connection::Error),
Connection(rustbus::connection::Error),

#[error("Unexpected D-Bus message format")]
Invalid(String),
Expand All @@ -14,16 +14,39 @@ pub enum DBusError {
Generic(String),

#[error("error unmarshalling D-Bus message")]
Unmarshal(#[from] rustbus::wire::errors::UnmarshalError),
Unmarshal(rustbus::wire::unmarshal::Error),

#[error("error marshalling D-Bus message")]
Marshal(#[from] rustbus::wire::errors::MarshalError),
Marshal(rustbus::wire::marshal::Error),

#[error("rustbus message error")]
Message(rustbus::Error)
}

pub struct DBusConnection {
connection: DuplexConn,
}

impl From<rustbus::connection::Error> for DBusError {
fn from(err: rustbus::connection::Error) -> Self {
DBusError::Connection(err)
}
}

impl From<rustbus::wire::unmarshal::Error> for DBusError {
fn from(err: rustbus::wire::unmarshal::Error) -> Self {
DBusError::Unmarshal(err)
}
}

impl From<rustbus::Error> for DBusError {
fn from(err: rustbus::Error) -> Self {
DBusError::Message(err)
}
}



impl DBusConnection {
pub fn new() -> Result<Self, DBusError> {
let (connection, _) = Self::connect()?;
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(is_some_with)]

#[cfg(feature = "album-art")]
mod art;

Expand Down
19 changes: 7 additions & 12 deletions src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::formatter::FormattedNotification;
use crate::mpris::PlayerMetadata;
use crate::Configuration;
use rustbus::MessageBuilder;
use rustbus::{Marshal, Signature, Unmarshal};
use rustbus::{dbus_variant_sig, Marshal, Unmarshal, Signature};
use std::collections::HashMap;

const NOTIFICATION_NAMESPACE: &str = "org.freedesktop.Notifications";
Expand All @@ -17,16 +17,11 @@ pub struct Notifier {
configuration: Configuration,
}

// HACK - This is used to get Rustbus to marshal a nested Dict has <String,
// Variant> (rather than as <String, String>).
#[derive(Marshal, Unmarshal, Signature, Debug)]
enum HintVariant {
String(String),
ImageData(NotificationImage),
}
type NotificationHintMap = HashMap<String, NotificationHintVariant>;
dbus_variant_sig!(NotificationHintVariant, CaseString => String; CaseNotificationImage => NotificationImage);

// See: https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html#icons-and-images
#[derive(Marshal, Unmarshal, Signature, Debug)]
#[derive(Marshal, Unmarshal, Signature, Debug, Eq, PartialEq)]
pub struct NotificationImage {
width: i32,
height: i32,
Expand Down Expand Up @@ -82,13 +77,13 @@ impl Notifier {
message.body.push_param(subject)?; // summary
message.body.push_param(body)?; // body
message.body.push_param(Vec::<String>::new())?; // actions (array of strings)
let mut hints: HashMap<String, HintVariant> = HashMap::new();
let mut hints: NotificationHintMap = HashMap::new();
hints.insert(
"x-canonical-private-synchronous".to_string(),
HintVariant::String(NOTIFICATION_SOURCE.to_string()),
NotificationHintVariant::CaseString(NOTIFICATION_SOURCE.to_string()),
);
if let Some(album_art) = album_art {
hints.insert("image-data".to_string(), HintVariant::ImageData(album_art));
hints.insert("image-data".to_string(), NotificationHintVariant::CaseNotificationImage(album_art));
}
message.body.push_param(&hints)?; // hints (dict of a{sv})
message.body.push_param(-1_i32)?; // timeout
Expand Down
5 changes: 3 additions & 2 deletions src/signal_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ impl SignalHandler {
}

// Don't notify for the same track twice, unless we're resuming play
if previous_track_id.is_some_and(|id| *id == metadata.track_id)
|| previous_status.is_some_and(|status| *status != PlayerStatus::Playing)
if previous_track_id.is_some()
&& (previous_track_id.unwrap() == metadata.track_id
|| previous_status.unwrap() != PlayerStatus::Playing)
{
return Ok(());
}
Expand Down

0 comments on commit 2945592

Please sign in to comment.