From 35db5fb07d5190d1371fddf7008a249b51283448 Mon Sep 17 00:00:00 2001 From: krolxon Date: Thu, 16 May 2024 18:08:41 +0530 Subject: [PATCH] syntactical suger --- src/app.rs | 2 +- src/connection.rs | 43 +++++++++++++++++-------------------------- src/event/handler.rs | 3 +-- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/app.rs b/src/app.rs index b4f964a..5d749b2 100755 --- a/src/app.rs +++ b/src/app.rs @@ -41,7 +41,7 @@ pub enum SelectedTab { impl App { pub fn builder(addrs: &str) -> AppResult { - let mut conn = Connection::new(addrs).unwrap(); + let mut conn = Connection::builder(addrs)?; let mut queue_list = ContentList::new(); let mut pl_list = ContentList::new(); diff --git a/src/connection.rs b/src/connection.rs index f98310d..7df0517 100755 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,12 +1,10 @@ +use crate::app::AppResult; use mpd::song::Song; use mpd::{Client, State}; use simple_dmenu::dmenu; use std::process::Command; use std::time::Duration; -pub type Result = core::result::Result; -pub type Error = Box; - #[derive(Debug)] /// struct storing the mpd Client related stuff pub struct Connection { @@ -24,7 +22,7 @@ pub struct Connection { impl Connection { /// Create a new connection - pub fn new(addrs: &str) -> Result { + pub fn builder(addrs: &str) -> AppResult { let mut conn = Client::connect(addrs).unwrap_or_else(|_| { eprintln!("Error connecting to mpd server, Make sure mpd is running"); std::process::exit(1); @@ -36,8 +34,7 @@ impl Connection { }; let songs_filenames: Vec = conn - .listall() - .unwrap() + .listall()? .into_iter() .map(|x| x.file) .collect(); @@ -68,7 +65,7 @@ impl Connection { } /// Dmenu prompt for selecting songs - pub fn play_dmenu(&mut self) -> Result<()> { + pub fn play_dmenu(&mut self) -> AppResult<()> { if is_installed("dmenu") { let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect(); let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30"); @@ -94,11 +91,11 @@ impl Connection { let stats = self.conn.stats().unwrap_or_default(); // Playback State - match status.state { - State::Stop => self.state = "Stopped".to_string(), - State::Play => self.state = "Playing".to_string(), - State::Pause => self.state = "Paused".to_string(), - } + self.state = match status.state { + State::Stop => "Stopped".to_string(), + State::Play => "Playing".to_string(), + State::Pause => "Paused".to_string(), + }; // Progress let (elapsed, total) = status.time.unwrap_or_default(); @@ -137,7 +134,7 @@ impl Connection { } /// push the given song to queue - pub fn push(&mut self, song: &Song) -> Result<()> { + pub fn push(&mut self, song: &Song) -> AppResult<()> { if self.conn.queue().unwrap().is_empty() { self.conn.push(song).unwrap(); self.conn.play().unwrap(); @@ -154,14 +151,14 @@ impl Connection { } /// Push all songs of a playlist into queue - pub fn load_playlist(&mut self, playlist: &str) -> Result<()> { + pub fn load_playlist(&mut self, playlist: &str) -> AppResult<()> { self.conn.load(playlist, ..)?; self.conn.play()?; Ok(()) } /// Add given song to playlist - pub fn add_to_playlist(&mut self, playlist: &str, song: &Song) -> Result<()> { + pub fn add_to_playlist(&mut self, playlist: &str, song: &Song) -> AppResult<()> { self.conn.pl_push(playlist, song)?; Ok(()) } @@ -185,7 +182,7 @@ impl Connection { } /// Gives title of current playing song - pub fn now_playing(&mut self) -> Result> { + pub fn now_playing(&mut self) -> AppResult> { if let Some(s) = &self.current_song.title { if let Some(a) = &self.current_song.artist { Ok(Some(format!("\"{}\" By {}", s, a))) @@ -210,20 +207,14 @@ impl Connection { /// Toggle Repeat mode pub fn toggle_repeat(&mut self) { - if self.conn.status().unwrap().repeat { - self.conn.repeat(false).unwrap(); - } else { - self.conn.repeat(true).unwrap(); - } + let mode = self.conn.status().unwrap().repeat; + self.conn.repeat(!mode).unwrap(); } /// Toggle random mode pub fn toggle_random(&mut self) { - if self.conn.status().unwrap().random { - self.conn.random(false).unwrap(); - } else { - self.conn.random(true).unwrap(); - } + let mode = self.conn.status().unwrap().random; + self.conn.random(!mode).unwrap(); } // Volume controls diff --git a/src/event/handler.rs b/src/event/handler.rs index 1d0ceb7..454b5c4 100755 --- a/src/event/handler.rs +++ b/src/event/handler.rs @@ -137,8 +137,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { app.conn.songs_filenames = app .conn .conn - .listall() - .unwrap() + .listall()? .into_iter() .map(|x| x.file) .collect();