From 2911b9bb49b2372095f2ae2e1def479a8fb1e2cf Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 19 Jun 2019 00:30:16 +0200 Subject: [PATCH] Simplify user code by parsing properties ourselves --- examples/media_player.rs | 74 ++++++++++++++-------------------------- src/ipc.rs | 38 +++++++++++++++++++-- src/lib.rs | 16 +++++++-- 3 files changed, 74 insertions(+), 54 deletions(-) diff --git a/examples/media_player.rs b/examples/media_player.rs index dde77bb..f54d6c9 100644 --- a/examples/media_player.rs +++ b/examples/media_player.rs @@ -4,6 +4,7 @@ use mpvipc::{ Event, Mpv, MpvDataType, + Property, }; use std::io::{self, Write}; @@ -31,57 +32,32 @@ fn main() -> Result<(), Error> { loop { let event = mpv.event_listen()?; match event { - Event::PropertyChange { name, id: _, data } => { - match name.as_ref() { - "path" => { - match data { - MpvDataType::String(value) => println!("\nPlaying: {}", value), - MpvDataType::Null => (), - _ => panic!("Wrong data type for 'path' value: {:?}", 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); } }, - "pause" => { - match data { - MpvDataType::Bool(value) => pause = value, - _ => panic!("Wrong data type for 'pause' value: {:?}", data), - } - }, - "playback-time" => { - match data { - MpvDataType::Double(value) => playback_time = value, - MpvDataType::Null => (), - _ => panic!("Wrong data type for 'playback-time' value: {:?}", data), - } - }, - "duration" => { - match data { - MpvDataType::Double(value) => duration = value, - MpvDataType::Null => (), - _ => panic!("Wrong data type for 'duration' value: {:?}", data), - } - }, - "metadata" => { - match data { - MpvDataType::HashMap(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); - } - }, - MpvDataType::Null => (), - _ => panic!("Wrong data type for 'metadata' value: {:?}", data), - } - }, - _ => panic!("Wrong property changed: {}", name), + Property::Metadata(None) => (), + Property::Unknown { name: _, id: _, data: _ } => (), } }, Event::Shutdown => return Ok(()), diff --git a/src/ipc.rs b/src/ipc.rs index 7b7d93f..fa16307 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -1,4 +1,4 @@ -use log::debug; +use log::{debug, warn}; use serde_json::{self, Value}; use std::collections::HashMap; use std::io::BufReader; @@ -279,6 +279,40 @@ pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Resul } } +fn try_convert_property(name: &str, id: isize, data: MpvDataType) -> Event { + let property = match name { + "path" => match data { + MpvDataType::String(value) => Property::Path(Some(value)), + MpvDataType::Null => Property::Path(None), + _ => unimplemented!(), + }, + "pause" => match data { + MpvDataType::Bool(value) => Property::Pause(value), + _ => unimplemented!(), + }, + "playback-time" => match data { + MpvDataType::Double(value) => Property::PlaybackTime(Some(value)), + MpvDataType::Null => Property::PlaybackTime(None), + _ => unimplemented!(), + }, + "duration" => match data { + MpvDataType::Double(value) => Property::Duration(Some(value)), + MpvDataType::Null => Property::Duration(None), + _ => unimplemented!(), + }, + "metadata" => match data { + MpvDataType::HashMap(value) => Property::Metadata(Some(value)), + MpvDataType::Null => Property::Metadata(None), + _ => unimplemented!(), + }, + _ => { + warn!("Property {} not implemented", name); + Property::Unknown { name: name.to_string(), id, data } + } + }; + Event::PropertyChange(property) +} + pub fn listen(instance: &mut Mpv) -> Result { let mut response = String::new(); instance.reader.read_line(&mut response).unwrap(); @@ -391,7 +425,7 @@ pub fn listen(instance: &mut Mpv) -> Result { } } - event = Event::PropertyChange { name, id, data } + event = try_convert_property(name.as_ref(), id, data); } _ => { event = Event::Unimplemented; diff --git a/src/lib.rs b/src/lib.rs index 5775ccb..1edb582 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,13 +36,23 @@ pub enum Event { MetadataUpdate, Seek, PlaybackRestart, - PropertyChange { + PropertyChange(Property), + ChapterChange, + Unimplemented, +} + +#[derive(Debug)] +pub enum Property { + Path(Option), + Pause(bool), + PlaybackTime(Option), + Duration(Option), + Metadata(Option>), + Unknown { name: String, id: isize, data: MpvDataType, }, - ChapterChange, - Unimplemented, } #[derive(Debug)]