From 7c842c457b79113a9ea7feea07f6aeeee17fd072 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Thu, 14 Mar 2024 13:49:39 +0100 Subject: [PATCH] Clean up code extract functions rename exec functions to separate from Exec functionality --- src/controller.rs | 114 ++++++++++++++++++++++---------------------- src/mpris_client.rs | 16 +++---- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/src/controller.rs b/src/controller.rs index dbc750b..e2b9379 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -28,7 +28,7 @@ fn exec(in_channel: Receiver, config: Arc) { } } -fn exec_binary_functionality(player_ids: &Option>, state: u8, exec_closure: F) +fn execute_binary_functionality(player_ids: &Option>, state: u8, exec_closure: F) where F: Fn(Option<&str>) -> (), { @@ -50,62 +50,9 @@ fn eval(key: NanoKeys, state: u8, config: &Arc) { ); for action in actions { match action { - KeyMapVariant::Mpris { ids, action } => match action { - MprisAction::Play => { - exec_binary_functionality(ids, state, |player_id: Option<&str>| { - mpris_client::play(player_id) - }) - } - MprisAction::Pause => { - exec_binary_functionality(ids, state, |player_id: Option<&str>| { - mpris_client::pause(player_id) - }) - } - MprisAction::PlayPause => { - exec_binary_functionality(ids, state, |player_id: Option<&str>| { - mpris_client::play_pause(player_id) - }) - } - MprisAction::Stop => { - exec_binary_functionality(ids, state, |player_id: Option<&str>| { - mpris_client::stop(player_id) - }) - } - MprisAction::Next => { - exec_binary_functionality(ids, state, |player_id: Option<&str>| { - mpris_client::next(player_id) - }) - } - MprisAction::Previous => { - exec_binary_functionality(ids, state, |player_id: Option<&str>| { - mpris_client::previous(player_id) - }) - } - MprisAction::Volume => { - let volume = state as f64 / 100.0; - match ids { - Some(some_ids) => some_ids.iter().for_each(|id| { - mpris_client::set_volume(Option::Some(id), volume) - }), - None => mpris_client::set_volume(Option::None, volume), - } - } - }, - KeyMapVariant::PipeWire { ids } => {} - KeyMapVariant::Exec { command, args } => { - let output: Result = match args { - Some(some_args) => Command::new(command).args(some_args).output(), - None => Command::new(command).output(), - }; - match output { - Ok(out) => { - info!("{:?}", out); - } - Err(err) => { - error!("{:?}", err); - } - } - } + KeyMapVariant::Mpris { ids, action } => parse_mpris_action(state, ids, action), + KeyMapVariant::PulseAudio { ids } => {} + KeyMapVariant::Exec { command, args } => parse_exec_action(command, args), } } } @@ -115,6 +62,59 @@ fn eval(key: NanoKeys, state: u8, config: &Arc) { } } +fn parse_mpris_action(state: u8, ids: &Option>, action: &MprisAction) { + match action { + MprisAction::Play => execute_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::play(player_id) + }), + MprisAction::Pause => { + execute_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::pause(player_id) + }) + } + MprisAction::PlayPause => { + execute_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::play_pause(player_id) + }) + } + MprisAction::Stop => execute_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::stop(player_id) + }), + MprisAction::Next => execute_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::next(player_id) + }), + MprisAction::Previous => { + execute_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::previous(player_id) + }) + } + MprisAction::Volume => { + let volume = state as f64 / 100.0; + match ids { + Some(some_ids) => some_ids + .iter() + .for_each(|id| mpris_client::set_volume(Option::Some(id), volume)), + None => mpris_client::set_volume(Option::None, volume), + } + } + } +} + +fn parse_exec_action(command: &str, args: &Option>) { + let output: Result = match args { + Some(some_args) => Command::new(command).args(some_args).output(), + None => Command::new(command).output(), + }; + match output { + Ok(out) => { + info!("{:?}", out); + } + Err(err) => { + error!("{:?}", err); + } + } +} + fn is_logical_true(state: u8) -> bool { return state == 127; } diff --git a/src/mpris_client.rs b/src/mpris_client.rs index 08d090b..dcc79f1 100644 --- a/src/mpris_client.rs +++ b/src/mpris_client.rs @@ -48,7 +48,7 @@ where } } -fn exec_functionality(player_id: Option<&str>, exec_closure: F) +fn execute_functionality(player_id: Option<&str>, exec_closure: F) where F: Fn(&Player) -> (), { @@ -59,43 +59,43 @@ where } pub fn play(player_id: Option<&str>) -> () { - exec_functionality(player_id, |player: &Player| { + execute_functionality(player_id, |player: &Player| { let _ = player.play(); }); } pub fn pause(player_id: Option<&str>) -> () { - exec_functionality(player_id, |player: &Player| { + execute_functionality(player_id, |player: &Player| { let _ = player.pause(); }); } pub fn play_pause(player_id: Option<&str>) -> () { - exec_functionality(player_id, |player: &Player| { + execute_functionality(player_id, |player: &Player| { let _ = player.play_pause(); }); } pub fn stop(player_id: Option<&str>) -> () { - exec_functionality(player_id, |player: &Player| { + execute_functionality(player_id, |player: &Player| { let _ = player.stop(); }); } pub fn next(player_id: Option<&str>) -> () { - exec_functionality(player_id, |player: &Player| { + execute_functionality(player_id, |player: &Player| { let _ = player.next(); }); } pub fn previous(player_id: Option<&str>) -> () { - exec_functionality(player_id, |player: &Player| { + execute_functionality(player_id, |player: &Player| { let _ = player.previous(); }); } pub fn set_volume(player_id: Option<&str>, volume: f64) -> () { - exec_functionality(player_id, |player: &Player| { + execute_functionality(player_id, |player: &Player| { let _ = player.set_volume(volume); }); }