cargo fmt + clippy
This commit is contained in:
89
src/api.rs
89
src/api.rs
@@ -321,13 +321,14 @@ impl Mpv {
|
||||
}
|
||||
|
||||
pub async fn get_event_stream(&self) -> impl futures::Stream<Item = Result<Event, Error>> {
|
||||
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<Event, Error> {
|
||||
@@ -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),
|
||||
|
||||
@@ -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<Playlist, Error>;
|
||||
async fn get_metadata(&self) -> Result<HashMap<String, MpvDataType>, Error>;
|
||||
|
||||
}
|
||||
|
||||
impl MpvExt for Mpv {
|
||||
@@ -64,7 +72,7 @@ impl MpvExt for Mpv {
|
||||
async fn get_playlist(&self) -> Result<Playlist, Error> {
|
||||
self.get_property::<Vec<PlaylistEntry>>("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::<f64>("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(|_| ())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ impl MpvIpc {
|
||||
.as_ref()
|
||||
.map_err(|why| Error(ErrorCode::ConnectError(why.to_string())))
|
||||
.and_then(|event| {
|
||||
serde_json::from_str::<Value>(&event)
|
||||
serde_json::from_str::<Value>(event)
|
||||
.map_err(|why| Error(ErrorCode::JsonParseError(why.to_string())))
|
||||
});
|
||||
|
||||
@@ -192,5 +192,5 @@ fn parse_mpv_response_data(value: Value) -> Result<Option<Value>, 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())
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
mod api;
|
||||
mod api_extension;
|
||||
mod ipc;
|
||||
mod message_parser;
|
||||
mod api_extension;
|
||||
|
||||
pub use api::*;
|
||||
pub use api_extension::*;
|
||||
pub use api_extension::*;
|
||||
|
||||
@@ -24,7 +24,9 @@ impl TypeHandler for String {
|
||||
|
||||
impl TypeHandler for bool {
|
||||
fn get_value(value: Value) -> Result<bool, Error> {
|
||||
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<f64, Error> {
|
||||
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<usize, Error> {
|
||||
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<String, MpvDataType> {
|
||||
fn get_value(value: Value) -> Result<HashMap<String, MpvDataType>, 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<String, MpvDataType> {
|
||||
|
||||
impl TypeHandler for Vec<PlaylistEntry> {
|
||||
fn get_value(value: Value) -> Result<Vec<PlaylistEntry>, 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<String, Value>,
|
||||
) -> HashMap<String, MpvDataType> {
|
||||
let mut output_map: HashMap<String, MpvDataType> = 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<Value>) -> Vec<MpvDataType> {
|
||||
pub(crate) fn json_array_to_vec(array: &[Value]) -> Vec<MpvDataType> {
|
||||
let mut output: Vec<MpvDataType> = 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<Value>) -> Vec<MpvDataType> {
|
||||
output
|
||||
}
|
||||
|
||||
pub(crate) fn json_array_to_playlist(array: &Vec<Value>) -> Vec<PlaylistEntry> {
|
||||
pub(crate) fn json_array_to_playlist(array: &[Value]) -> Vec<PlaylistEntry> {
|
||||
let mut output: Vec<PlaylistEntry> = Vec::new();
|
||||
for (id, entry) in array.iter().enumerate() {
|
||||
let mut filename: String = String::new();
|
||||
|
||||
Reference in New Issue
Block a user