From 3be7b2bda68d1677eb41301cb429e1947f010f49 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 30 Apr 2024 02:13:57 +0200 Subject: [PATCH] cargo fmt + clippy --- examples/fetch_state.rs | 1 - examples/media_player.rs | 12 +++--- src/api.rs | 89 ++++++++++++++++++++-------------------- src/api_extension.rs | 26 +++++++----- src/ipc.rs | 4 +- src/lib.rs | 4 +- src/message_parser.rs | 35 +++++++++------- tests/events.rs | 2 +- tests/get_property.rs | 45 +++++++++----------- tests/set_property.rs | 54 +++++++++++------------- 10 files changed, 134 insertions(+), 138 deletions(-) diff --git a/examples/fetch_state.rs b/examples/fetch_state.rs index ba4063d..8c699fd 100644 --- a/examples/fetch_state.rs +++ b/examples/fetch_state.rs @@ -1,4 +1,3 @@ -use env_logger; use mpvipc::{Error as MpvError, Mpv, MpvExt}; #[tokio::main] diff --git a/examples/media_player.rs b/examples/media_player.rs index cfa0ee8..0bd7b3b 100644 --- a/examples/media_player.rs +++ b/examples/media_player.rs @@ -1,6 +1,4 @@ -use env_logger; -use mpvipc::{Error, Event, Mpv, MpvExt, MpvDataType, Property}; -use std::io::{self, Write}; +use mpvipc::{Error, Mpv, MpvExt}; fn seconds_to_hms(total: f64) -> String { let total = total as u64; @@ -15,10 +13,10 @@ fn seconds_to_hms(total: f64) -> String { async fn main() -> Result<(), Error> { env_logger::init(); - let mut mpv = Mpv::connect("/tmp/mpv.sock").await?; - let mut pause = false; - let mut playback_time = std::f64::NAN; - let mut duration = std::f64::NAN; + let mpv = Mpv::connect("/tmp/mpv.sock").await?; + let pause = false; + let playback_time = std::f64::NAN; + let duration = std::f64::NAN; mpv.observe_property(1, "path").await?; mpv.observe_property(2, "pause").await?; mpv.observe_property(3, "playback-time").await?; diff --git a/src/api.rs b/src/api.rs index 5a9892f..1542ffa 100644 --- a/src/api.rs +++ b/src/api.rs @@ -321,13 +321,14 @@ impl Mpv { } pub async fn get_event_stream(&self) -> impl futures::Stream> { - tokio_stream::wrappers::BroadcastStream::new(self.broadcast_channel.subscribe()) - .map(|event| { - match event { - Ok(event) => Mpv::map_event(event), - Err(_) => Err(Error(ErrorCode::ConnectError("Failed to receive event".to_string()))), - } - }) + tokio_stream::wrappers::BroadcastStream::new(self.broadcast_channel.subscribe()).map( + |event| match event { + Ok(event) => Mpv::map_event(event), + Err(_) => Err(Error(ErrorCode::ConnectError( + "Failed to receive event".to_string(), + ))), + }, + ) } fn map_event(raw_event: MpvIpcEvent) -> Result { @@ -373,43 +374,43 @@ impl Mpv { .ok_or(Error(ErrorCode::ValueDoesNotContainString))?; match property_name { - "path" => { - let path = event - .get("data") - .ok_or(Error(ErrorCode::MissingValue))? - .as_str() - .map(|s| s.to_string()); - Ok(Event::PropertyChange { - id, - property: Property::Path(path), - }) - } - "pause" => { - let pause = event - .get("data") - .ok_or(Error(ErrorCode::MissingValue))? - .as_bool() - .ok_or(Error(ErrorCode::ValueDoesNotContainBool))?; - Ok(Event::PropertyChange { - id, - property: Property::Pause(pause), - }) - } - // TODO: missing cases - _ => { - let data = event - .get("data") - .ok_or(Error(ErrorCode::MissingValue))? - .clone(); - Ok(Event::PropertyChange { - id, - property: Property::Unknown { - name: property_name.to_string(), - // TODO: fix - data: MpvDataType::Double(data.as_f64().unwrap_or(0.0)), - }, - }) - } + "path" => { + let path = event + .get("data") + .ok_or(Error(ErrorCode::MissingValue))? + .as_str() + .map(|s| s.to_string()); + Ok(Event::PropertyChange { + id, + property: Property::Path(path), + }) + } + "pause" => { + let pause = event + .get("data") + .ok_or(Error(ErrorCode::MissingValue))? + .as_bool() + .ok_or(Error(ErrorCode::ValueDoesNotContainBool))?; + Ok(Event::PropertyChange { + id, + property: Property::Pause(pause), + }) + } + // TODO: missing cases + _ => { + let data = event + .get("data") + .ok_or(Error(ErrorCode::MissingValue))? + .clone(); + Ok(Event::PropertyChange { + id, + property: Property::Unknown { + name: property_name.to_string(), + // TODO: fix + data: MpvDataType::Double(data.as_f64().unwrap_or(0.0)), + }, + }) + } } } "chapter-change" => Ok(Event::ChapterChange), diff --git a/src/api_extension.rs b/src/api_extension.rs index 608b1b0..f95d653 100644 --- a/src/api_extension.rs +++ b/src/api_extension.rs @@ -1,4 +1,7 @@ -use crate::{Error, IntoRawCommandPart, Mpv, MpvCommand, MpvDataType, Playlist, PlaylistAddOptions, PlaylistAddTypeOptions, PlaylistEntry, SeekOptions}; +use crate::{ + Error, IntoRawCommandPart, Mpv, MpvCommand, MpvDataType, Playlist, PlaylistAddOptions, + PlaylistAddTypeOptions, PlaylistEntry, SeekOptions, +}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -31,7 +34,8 @@ pub enum Switch { pub trait MpvExt { async fn toggle(&self) -> Result<(), Error>; async fn stop(&self) -> Result<(), Error>; - async fn set_volume(&self, input_volume: f64, option: NumberChangeOptions) -> Result<(), Error>; + async fn set_volume(&self, input_volume: f64, option: NumberChangeOptions) + -> Result<(), Error>; async fn set_speed(&self, input_speed: f64, option: NumberChangeOptions) -> Result<(), Error>; async fn set_mute(&self, option: Switch) -> Result<(), Error>; async fn set_loop_playlist(&self, option: Switch) -> Result<(), Error>; @@ -43,7 +47,12 @@ pub trait MpvExt { async fn playlist_play_id(&self, id: usize) -> Result<(), Error>; async fn playlist_move_id(&self, from: usize, to: usize) -> Result<(), Error>; async fn playlist_clear(&self) -> Result<(), Error>; - async fn playlist_add(&self, file: &str, file_type: PlaylistAddTypeOptions, option: PlaylistAddOptions) -> Result<(), Error>; + async fn playlist_add( + &self, + file: &str, + file_type: PlaylistAddTypeOptions, + option: PlaylistAddOptions, + ) -> Result<(), Error>; async fn restart(&self) -> Result<(), Error>; async fn prev(&self) -> Result<(), Error>; async fn pause(&self) -> Result<(), Error>; @@ -53,7 +62,6 @@ pub trait MpvExt { async fn kill(&self) -> Result<(), Error>; async fn get_playlist(&self) -> Result; async fn get_metadata(&self) -> Result, Error>; - } impl MpvExt for Mpv { @@ -64,7 +72,7 @@ impl MpvExt for Mpv { async fn get_playlist(&self) -> Result { self.get_property::>("playlist") .await - .map(|entries| Playlist(entries)) + .map(Playlist) } async fn kill(&self) -> Result<(), Error> { @@ -217,11 +225,7 @@ impl MpvExt for Mpv { self.set_property("mute", enabled).await } - async fn set_speed( - &self, - input_speed: f64, - option: NumberChangeOptions, - ) -> Result<(), Error> { + async fn set_speed(&self, input_speed: f64, option: NumberChangeOptions) -> Result<(), Error> { match self.get_property::("speed").await { Ok(speed) => match option { NumberChangeOptions::Increase => { @@ -266,4 +270,4 @@ impl MpvExt for Mpv { async fn toggle(&self) -> Result<(), Error> { self.run_command_raw("cycle", &["pause"]).await.map(|_| ()) } -} \ No newline at end of file +} diff --git a/src/ipc.rs b/src/ipc.rs index d91a7b4..03e635e 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -115,7 +115,7 @@ impl MpvIpc { .as_ref() .map_err(|why| Error(ErrorCode::ConnectError(why.to_string()))) .and_then(|event| { - serde_json::from_str::(&event) + serde_json::from_str::(event) .map_err(|why| Error(ErrorCode::JsonParseError(why.to_string()))) }); @@ -192,5 +192,5 @@ fn parse_mpv_response_data(value: Value) -> Result, Error> { Ok(v) => log::trace!("Successfully parsed mpv response data: {:?}", v), Err(e) => log::trace!("Error parsing mpv response data: {:?}", e), } - result.map(|opt| opt.map(|val| val.clone())) + result.map(|opt| opt.cloned()) } diff --git a/src/lib.rs b/src/lib.rs index 7ff6196..aca7f54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ mod api; +mod api_extension; mod ipc; mod message_parser; -mod api_extension; pub use api::*; -pub use api_extension::*; \ No newline at end of file +pub use api_extension::*; diff --git a/src/message_parser.rs b/src/message_parser.rs index 1f6fb83..8555400 100644 --- a/src/message_parser.rs +++ b/src/message_parser.rs @@ -24,7 +24,9 @@ impl TypeHandler for String { impl TypeHandler for bool { fn get_value(value: Value) -> Result { - value.as_bool().ok_or(Error(ErrorCode::ValueDoesNotContainBool)) + value + .as_bool() + .ok_or(Error(ErrorCode::ValueDoesNotContainBool)) } fn as_string(&self) -> String { @@ -38,7 +40,9 @@ impl TypeHandler for bool { impl TypeHandler for f64 { fn get_value(value: Value) -> Result { - value.as_f64().ok_or(Error(ErrorCode::ValueDoesNotContainF64)) + value + .as_f64() + .ok_or(Error(ErrorCode::ValueDoesNotContainF64)) } fn as_string(&self) -> String { @@ -48,9 +52,10 @@ impl TypeHandler for f64 { impl TypeHandler for usize { fn get_value(value: Value) -> Result { - value.as_u64() + value + .as_u64() .map(|u| u as usize) -.ok_or(Error(ErrorCode::ValueDoesNotContainUsize)) + .ok_or(Error(ErrorCode::ValueDoesNotContainUsize)) } fn as_string(&self) -> String { @@ -60,8 +65,9 @@ impl TypeHandler for usize { impl TypeHandler for HashMap { fn get_value(value: Value) -> Result, Error> { - value.as_object() - .ok_or(Error(ErrorCode::ValueDoesNotContainHashMap)) + value + .as_object() + .ok_or(Error(ErrorCode::ValueDoesNotContainHashMap)) .map(json_map_to_hashmap) } @@ -72,9 +78,10 @@ impl TypeHandler for HashMap { impl TypeHandler for Vec { fn get_value(value: Value) -> Result, Error> { - value.as_array() - .ok_or(Error(ErrorCode::ValueDoesNotContainPlaylist)) - .map(json_array_to_playlist) + value + .as_array() + .ok_or(Error(ErrorCode::ValueDoesNotContainPlaylist)) + .map(|array| json_array_to_playlist(array)) } fn as_string(&self) -> String { @@ -86,8 +93,8 @@ pub(crate) fn json_map_to_hashmap( map: &serde_json::map::Map, ) -> HashMap { let mut output_map: HashMap = HashMap::new(); - for (ref key, ref value) in map.iter() { - match **value { + for (ref key, value) in map.iter() { + match *value { Value::Array(ref array) => { output_map.insert( key.to_string(), @@ -126,9 +133,9 @@ pub(crate) fn json_map_to_hashmap( output_map } -pub(crate) fn json_array_to_vec(array: &Vec) -> Vec { +pub(crate) fn json_array_to_vec(array: &[Value]) -> Vec { let mut output: Vec = Vec::new(); - if array.len() > 0 { + if !array.is_empty() { match array[0] { Value::Array(_) => { for entry in array { @@ -184,7 +191,7 @@ pub(crate) fn json_array_to_vec(array: &Vec) -> Vec { output } -pub(crate) fn json_array_to_playlist(array: &Vec) -> Vec { +pub(crate) fn json_array_to_playlist(array: &[Value]) -> Vec { let mut output: Vec = Vec::new(); for (id, entry) in array.iter().enumerate() { let mut filename: String = String::new(); diff --git a/tests/events.rs b/tests/events.rs index a5584ef..acf30c1 100644 --- a/tests/events.rs +++ b/tests/events.rs @@ -1,7 +1,7 @@ use std::panic; use futures::{stream::StreamExt, SinkExt}; -use mpvipc::{Mpv, MpvExt, MpvDataType, Property}; +use mpvipc::{Mpv, MpvDataType, MpvExt, Property}; use serde_json::json; use test_log::test; use tokio::{net::UnixStream, task::JoinHandle}; diff --git a/tests/get_property.rs b/tests/get_property.rs index cb7367e..9baac20 100644 --- a/tests/get_property.rs +++ b/tests/get_property.rs @@ -89,32 +89,27 @@ async fn test_get_property_simultaneous_requests() { let mut framed = Framed::new(socket, LinesCodec::new()); while let Some(request) = framed.next().await { - match serde_json::from_str::(&request.unwrap()) { - Ok(json) => { - let property = json["command"][1].as_str().unwrap(); - log::info!("Received request for property: {:?}", property); - match property { - "volume" => { - let response = - json!({ "data": 100.0, "request_id": 0, "error": "success" }) - .to_string(); - framed.send(response).await.unwrap(); - } - "pause" => { - let response = - json!({ "data": true, "request_id": 0, "error": "success" }) - .to_string(); - framed.send(response).await.unwrap(); - } - _ => { - let response = - json!({ "error": "property unavailable", "request_id": 0 }) - .to_string(); - framed.send(response).await.unwrap(); - } + if let Ok(json) = serde_json::from_str::(&request.unwrap()) { + let property = json["command"][1].as_str().unwrap(); + log::info!("Received request for property: {:?}", property); + match property { + "volume" => { + let response = + json!({ "data": 100.0, "request_id": 0, "error": "success" }) + .to_string(); + framed.send(response).await.unwrap(); + } + "pause" => { + let response = json!({ "data": true, "request_id": 0, "error": "success" }) + .to_string(); + framed.send(response).await.unwrap(); + } + _ => { + let response = + json!({ "error": "property unavailable", "request_id": 0 }).to_string(); + framed.send(response).await.unwrap(); } } - Err(_) => {} } } @@ -136,7 +131,7 @@ async fn test_get_property_simultaneous_requests() { loop { tokio::time::sleep(Duration::from_millis(1)).await; let paused: bool = mpv_clone_2.get_property("pause").await.unwrap(); - assert_eq!(paused, true); + assert!(paused); } }); diff --git a/tests/set_property.rs b/tests/set_property.rs index a91a231..edafaea 100644 --- a/tests/set_property.rs +++ b/tests/set_property.rs @@ -79,9 +79,7 @@ async fn test_get_property_error() { assert_eq!( maybe_volume, - Err(Error(ErrorCode::MpvError( - "property not found".to_owned() - ))) + Err(Error(ErrorCode::MpvError("property not found".to_owned()))) ); join_handle.await.unwrap().unwrap(); @@ -94,33 +92,29 @@ async fn test_set_property_simultaneous_requests() { let mut framed = Framed::new(socket, LinesCodec::new()); while let Some(request) = framed.next().await { - match serde_json::from_str::(&request.unwrap()) { - Ok(json) => { - let property = json["command"][1].as_str().unwrap(); - let value = &json["command"][2]; - log::info!("Received set property command: {:?} => {:?}", property, value); - match property { - "volume" => { - let response = - json!({ "request_id": 0, "error": "success" }) - .to_string(); - framed.send(response).await.unwrap(); - } - "pause" => { - let response = - json!({ "request_id": 0, "error": "success" }) - .to_string(); - framed.send(response).await.unwrap(); - } - _ => { - let response = - json!({ "error":"property not found", "request_id": 0 }) - .to_string(); - framed.send(response).await.unwrap(); - } + if let Ok(json) = serde_json::from_str::(&request.unwrap()) { + let property = json["command"][1].as_str().unwrap(); + let value = &json["command"][2]; + log::info!( + "Received set property command: {:?} => {:?}", + property, + value + ); + match property { + "volume" => { + let response = json!({ "request_id": 0, "error": "success" }).to_string(); + framed.send(response).await.unwrap(); + } + "pause" => { + let response = json!({ "request_id": 0, "error": "success" }).to_string(); + framed.send(response).await.unwrap(); + } + _ => { + let response = + json!({ "error":"property not found", "request_id": 0 }).to_string(); + framed.send(response).await.unwrap(); } } - Err(_) => {} } } @@ -153,9 +147,7 @@ async fn test_set_property_simultaneous_requests() { let maybe_volume = mpv_clone_3.set_property("nonexistent", "a").await; assert_eq!( maybe_volume, - Err(Error(ErrorCode::MpvError( - "property not found".to_owned() - ))) + Err(Error(ErrorCode::MpvError("property not found".to_owned()))) ); } });