From 233be204792f9ab1f6016006628da237191c2570 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Thu, 18 Feb 2021 15:52:53 +0100 Subject: [PATCH] Refactor exec_swayr_cmd args into separat struct --- src/cmds.rs | 36 ++++++++++++++++++++---------------- src/demon.rs | 5 ++++- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/cmds.rs b/src/cmds.rs index 5e42188..e39061d 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -13,42 +13,43 @@ use std::sync::RwLock; use swayipc as s; use swayipc::reply as r; +pub struct ExecSwayrCmdArgs<'a> { + pub cmd: &'a SwayrCommand, + pub extra_props: Arc>>, +} + impl fmt::Display for SwayrCommand { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!(f, "{:?}", self) } } -pub fn exec_swayr_cmd( - cmd: &SwayrCommand, - extra_props: Arc>>, -) { - match cmd { +pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { + let props = args.extra_props; + match args.cmd { SwayrCommand::SwitchToUrgentOrLRUWindow => { - switch_to_urgent_or_lru_window(Some(&*extra_props.read().unwrap())) + switch_to_urgent_or_lru_window(Some(&*props.read().unwrap())) } SwayrCommand::SwitchWindow => { - switch_window(Some(&*extra_props.read().unwrap())) + switch_window(Some(&*props.read().unwrap())) } SwayrCommand::NextWindow => focus_next_window_in_direction( Direction::Forward, - Some(&*extra_props.read().unwrap()), + Some(&*props.read().unwrap()), ), SwayrCommand::PrevWindow => focus_next_window_in_direction( Direction::Backward, - Some(&*extra_props.read().unwrap()), + Some(&*props.read().unwrap()), ), - SwayrCommand::QuitWindow => { - quit_window(Some(&*extra_props.read().unwrap())) - } + SwayrCommand::QuitWindow => quit_window(Some(&*props.read().unwrap())), SwayrCommand::SwitchWorkspace => { - switch_workspace(Some(&*extra_props.read().unwrap())) + switch_workspace(Some(&*props.read().unwrap())) } SwayrCommand::SwitchWorkspaceOrWindow => { - switch_workspace_or_window(Some(&*extra_props.read().unwrap())) + switch_workspace_or_window(Some(&*props.read().unwrap())) } SwayrCommand::QuitWorkspaceOrWindow => { - quit_workspace_or_window(Some(&*extra_props.read().unwrap())) + quit_workspace_or_window(Some(&*props.read().unwrap())) } SwayrCommand::ExecuteSwaymsgCommand => exec_swaymsg_command(), SwayrCommand::ExecuteSwayrCommand => { @@ -66,7 +67,10 @@ pub fn exec_swayr_cmd( SwayrCommand::PrevWindow, ], ) { - exec_swayr_cmd(c, extra_props); + exec_swayr_cmd(ExecSwayrCmdArgs { + cmd: c, + extra_props: props, + }); } } } diff --git a/src/demon.rs b/src/demon.rs index 5417a8e..b61d9d2 100644 --- a/src/demon.rs +++ b/src/demon.rs @@ -203,7 +203,10 @@ fn handle_client_request( let mut cmd_str = String::new(); if stream.read_to_string(&mut cmd_str).is_ok() { if let Ok(cmd) = serde_json::from_str::(&cmd_str) { - cmds::exec_swayr_cmd(&cmd, extra_props); + cmds::exec_swayr_cmd(cmds::ExecSwayrCmdArgs { + cmd: &cmd, + extra_props, + }); } else { eprintln!( "Could not serialize following string to SwayrCommand.\n{}",