diff --git a/examples/fetch_state.rs b/examples/fetch_state.rs index e2a01f7..6387399 100644 --- a/examples/fetch_state.rs +++ b/examples/fetch_state.rs @@ -1,5 +1,5 @@ use env_logger; -use mpvipc::{Mpv, Error as MpvError}; +use mpvipc::{Error as MpvError, Mpv}; fn main() -> Result<(), MpvError> { env_logger::init(); diff --git a/examples/media_player.rs b/examples/media_player.rs index f54d6c9..c7d0cf0 100644 --- a/examples/media_player.rs +++ b/examples/media_player.rs @@ -1,11 +1,5 @@ use env_logger; -use mpvipc::{ - Error, - Event, - Mpv, - MpvDataType, - Property, -}; +use mpvipc::{Error, Event, Mpv, MpvDataType, Property}; use std::io::{self, Write}; fn seconds_to_hms(total: f64) -> String { @@ -32,39 +26,47 @@ fn main() -> Result<(), Error> { loop { let event = mpv.event_listen()?; match event { - Event::PropertyChange(property) => { - match property { - Property::Path(Some(value)) => println!("\nPlaying: {}", value), - Property::Path(None) => (), - Property::Pause(value) => pause = value, - Property::PlaybackTime(Some(value)) => playback_time = value, - Property::PlaybackTime(None) => playback_time = std::f64::NAN, - Property::Duration(Some(value)) => duration = value, - Property::Duration(None) => duration = std::f64::NAN, - Property::Metadata(Some(value)) => { - println!("File tags:"); - if let Some(MpvDataType::String(value)) = value.get("ARTIST") { - println!(" Artist: {}", value); - } - if let Some(MpvDataType::String(value)) = value.get("ALBUM") { - println!(" Album: {}", value); - } - if let Some(MpvDataType::String(value)) = value.get("TITLE") { - println!(" Title: {}", value); - } - if let Some(MpvDataType::String(value)) = value.get("TRACK") { - println!(" Track: {}", value); - } - }, - Property::Metadata(None) => (), - Property::Unknown { name: _, id: _, data: _ } => (), + Event::PropertyChange(property) => match property { + Property::Path(Some(value)) => println!("\nPlaying: {}", value), + Property::Path(None) => (), + Property::Pause(value) => pause = value, + Property::PlaybackTime(Some(value)) => playback_time = value, + Property::PlaybackTime(None) => playback_time = std::f64::NAN, + Property::Duration(Some(value)) => duration = value, + Property::Duration(None) => duration = std::f64::NAN, + Property::Metadata(Some(value)) => { + println!("File tags:"); + if let Some(MpvDataType::String(value)) = value.get("ARTIST") { + println!(" Artist: {}", value); + } + if let Some(MpvDataType::String(value)) = value.get("ALBUM") { + println!(" Album: {}", value); + } + if let Some(MpvDataType::String(value)) = value.get("TITLE") { + println!(" Title: {}", value); + } + if let Some(MpvDataType::String(value)) = value.get("TRACK") { + println!(" Track: {}", value); + } } + Property::Metadata(None) => (), + Property::Unknown { + name: _, + id: _, + data: _, + } => (), }, Event::Shutdown => return Ok(()), Event::Unimplemented => panic!("Unimplemented event"), _ => (), } - print!("{}{} / {} ({:.0}%)\r", if pause { "(Paused) " } else { "" }, seconds_to_hms(playback_time), seconds_to_hms(duration), 100. * playback_time / duration); + print!( + "{}{} / {} ({:.0}%)\r", + if pause { "(Paused) " } else { "" }, + seconds_to_hms(playback_time), + seconds_to_hms(duration), + 100. * playback_time / duration + ); io::stdout().flush().unwrap(); } } diff --git a/src/ipc.rs b/src/ipc.rs index fa16307..72fb641 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -1,10 +1,10 @@ +use super::*; use log::{debug, warn}; use serde_json::{self, Value}; use std::collections::HashMap; -use std::io::BufReader; use std::io::prelude::*; +use std::io::BufReader; use std::iter::Iterator; -use super::*; #[derive(Debug)] pub struct PlaylistEntry { @@ -260,8 +260,7 @@ pub fn run_mpv_command(instance: &Mpv, command: &str, args: &[&str]) -> Result<( pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Result<(), Error> { let ipc_string = format!( "{{ \"command\": [\"observe_property\", {}, \"{}\"] }}\n", - id, - property + id, property ); match serde_json::from_str::(&send_command_sync(instance, &ipc_string)) { Ok(feedback) => { @@ -307,7 +306,11 @@ fn try_convert_property(name: &str, id: isize, data: MpvDataType) -> Event { }, _ => { warn!("Property {} not implemented", name); - Property::Unknown { name: name.to_string(), id, data } + Property::Unknown { + name: name.to_string(), + id, + data, + } } }; Event::PropertyChange(property) diff --git a/src/lib.rs b/src/lib.rs index 1edb582..8e3ded1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,8 @@ pub mod ipc; use ipc::*; use std::collections::HashMap; use std::fmt::{self, Display}; +use std::io::{BufReader, Read}; use std::os::unix::net::UnixStream; -use std::io::{Read, BufReader}; #[derive(Debug)] pub enum Event { @@ -132,9 +132,7 @@ impl Drop for Mpv { impl fmt::Debug for Mpv { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.debug_tuple("Mpv") - .field(&self.name) - .finish() + fmt.debug_tuple("Mpv").field(&self.name).finish() } } @@ -185,9 +183,7 @@ impl Display for ErrorCode { f.write_str("The received value is not of type \'std::f64\'") } ErrorCode::ValueDoesNotContainHashMap => { - f.write_str( - "The received value is not of type \'std::collections::HashMap\'", - ) + f.write_str("The received value is not of type \'std::collections::HashMap\'") } ErrorCode::ValueDoesNotContainPlaylist => { f.write_str("The received value is not of type \'mpvipc::Playlist\'") @@ -290,9 +286,9 @@ impl Mpv { pub fn disconnect(&self) { let mut stream = &self.stream; - stream.shutdown(std::net::Shutdown::Both).expect( - "socket disconnect", - ); + stream + .shutdown(std::net::Shutdown::Both) + .expect("socket disconnect"); let mut buffer = [0; 32]; for _ in 0..stream.bytes().count() { stream.read(&mut buffer[..]).unwrap(); @@ -449,33 +445,25 @@ impl Mpv { option: PlaylistAddOptions, ) -> Result<(), Error> { match file_type { - PlaylistAddTypeOptions::File => { - match option { - PlaylistAddOptions::Replace => { - run_mpv_command(self, "loadfile", &[file, "replace"]) - } - PlaylistAddOptions::Append => { - run_mpv_command(self, "loadfile", &[file, "append"]) - } - PlaylistAddOptions::AppendPlay => { - run_mpv_command(self, "loadfile", &[file, "append-play"]) - } + PlaylistAddTypeOptions::File => match option { + PlaylistAddOptions::Replace => { + run_mpv_command(self, "loadfile", &[file, "replace"]) } - } + PlaylistAddOptions::Append => run_mpv_command(self, "loadfile", &[file, "append"]), + PlaylistAddOptions::AppendPlay => { + run_mpv_command(self, "loadfile", &[file, "append-play"]) + } + }, - PlaylistAddTypeOptions::Playlist => { - match option { - PlaylistAddOptions::Replace => { - run_mpv_command(self, "loadlist", &[file, "replace"]) - } - PlaylistAddOptions::Append | - PlaylistAddOptions::AppendPlay => { - run_mpv_command(self, "loadlist", &[file, "append"]) - } + PlaylistAddTypeOptions::Playlist => match option { + PlaylistAddOptions::Replace => { + run_mpv_command(self, "loadlist", &[file, "replace"]) } - } + PlaylistAddOptions::Append | PlaylistAddOptions::AppendPlay => { + run_mpv_command(self, "loadlist", &[file, "append"]) + } + }, } - } pub fn playlist_clear(&self) -> Result<(), Error> { @@ -492,13 +480,11 @@ impl Mpv { pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> { match get_mpv_property::(self, "playlist-pos") { - Ok(current_id) => { - run_mpv_command( - self, - "playlist-move", - &[&id.to_string(), &(current_id + 1).to_string()], - ) - } + Ok(current_id) => run_mpv_command( + self, + "playlist-move", + &[&id.to_string(), &(current_id + 1).to_string()], + ), Err(msg) => Err(msg), } } @@ -533,21 +519,17 @@ impl Mpv { match option { Switch::On => enabled = true, Switch::Off => {} - Switch::Toggle => { - match get_mpv_property_string(self, "loop-file") { - Ok(value) => { - match value.as_ref() { - "false" => { - enabled = true; - } - _ => { - enabled = false; - } - } + Switch::Toggle => match get_mpv_property_string(self, "loop-file") { + Ok(value) => match value.as_ref() { + "false" => { + enabled = true; } - Err(msg) => return Err(msg), - } - } + _ => { + enabled = false; + } + }, + Err(msg) => return Err(msg), + }, } set_mpv_property(self, "loop-file", enabled) } @@ -557,21 +539,17 @@ impl Mpv { match option { Switch::On => enabled = true, Switch::Off => {} - Switch::Toggle => { - match get_mpv_property_string(self, "loop-playlist") { - Ok(value) => { - match value.as_ref() { - "false" => { - enabled = true; - } - _ => { - enabled = false; - } - } + Switch::Toggle => match get_mpv_property_string(self, "loop-playlist") { + Ok(value) => match value.as_ref() { + "false" => { + enabled = true; } - Err(msg) => return Err(msg), - } - } + _ => { + enabled = false; + } + }, + Err(msg) => return Err(msg), + }, } set_mpv_property(self, "loop-playlist", enabled) } @@ -581,14 +559,12 @@ impl Mpv { match option { Switch::On => enabled = true, Switch::Off => {} - Switch::Toggle => { - match get_mpv_property::(self, "mute") { - Ok(value) => { - enabled = !value; - } - Err(msg) => return Err(msg), + Switch::Toggle => match get_mpv_property::(self, "mute") { + Ok(value) => { + enabled = !value; } - } + Err(msg) => return Err(msg), + }, } set_mpv_property(self, "mute", enabled) } @@ -627,38 +603,34 @@ impl Mpv { pub fn set_speed(&self, input_speed: f64, option: NumberChangeOptions) -> Result<(), Error> { match get_mpv_property::(self, "speed") { - Ok(speed) => { - match option { - NumberChangeOptions::Increase => { - set_mpv_property(self, "speed", speed + input_speed) - } - - NumberChangeOptions::Decrease => { - set_mpv_property(self, "speed", speed - input_speed) - } - - NumberChangeOptions::Absolute => set_mpv_property(self, "speed", input_speed), + Ok(speed) => match option { + NumberChangeOptions::Increase => { + set_mpv_property(self, "speed", speed + input_speed) } - } + + NumberChangeOptions::Decrease => { + set_mpv_property(self, "speed", speed - input_speed) + } + + NumberChangeOptions::Absolute => set_mpv_property(self, "speed", input_speed), + }, Err(msg) => Err(msg), } } pub fn set_volume(&self, input_volume: f64, option: NumberChangeOptions) -> Result<(), Error> { match get_mpv_property::(self, "volume") { - Ok(volume) => { - match option { - NumberChangeOptions::Increase => { - set_mpv_property(self, "volume", volume + input_volume) - } - - NumberChangeOptions::Decrease => { - set_mpv_property(self, "volume", volume - input_volume) - } - - NumberChangeOptions::Absolute => set_mpv_property(self, "volume", input_volume), + Ok(volume) => match option { + NumberChangeOptions::Increase => { + set_mpv_property(self, "volume", volume + input_volume) } - } + + NumberChangeOptions::Decrease => { + set_mpv_property(self, "volume", volume - input_volume) + } + + NumberChangeOptions::Absolute => set_mpv_property(self, "volume", input_volume), + }, Err(msg) => Err(msg), } }