From c2bcdcf51abcbefa3d475def1f8e3aa18c80e2fc Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 5 May 2024 13:20:07 +0200 Subject: [PATCH] WIP --- src/core_api.rs | 2 +- src/highlevel_api_extension.rs | 8 +++--- src/message_parser.rs | 20 +++++++++---- src/property_parser.rs | 37 ++++++++++++++++--------- tests/mock_socket_tests/get_property.rs | 6 ++-- 5 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/core_api.rs b/src/core_api.rs index 49636a2..d5e1517 100644 --- a/src/core_api.rs +++ b/src/core_api.rs @@ -90,7 +90,7 @@ pub struct Playlist(pub Vec); pub struct PlaylistEntry { pub id: usize, pub filename: String, - pub title: String, + pub title: Option, pub current: bool, } diff --git a/src/highlevel_api_extension.rs b/src/highlevel_api_extension.rs index 9f50897..de760ab 100644 --- a/src/highlevel_api_extension.rs +++ b/src/highlevel_api_extension.rs @@ -154,10 +154,10 @@ pub trait MpvExt { async fn get_speed(&self) -> Result; /// Get the current position in the current video. - async fn get_time_pos(&self) -> Result; + async fn get_time_pos(&self) -> Result, MpvError>; /// Get the amount of time remaining in the current video. - async fn get_time_remaining(&self) -> Result; + async fn get_time_remaining(&self) -> Result, MpvError>; /// Get the total duration of the current video. async fn get_duration(&self) -> Result; @@ -415,7 +415,7 @@ impl MpvExt for Mpv { } } - async fn get_time_pos(&self) -> Result { + async fn get_time_pos(&self) -> Result, MpvError> { let data = self.get_property("time-pos").await?; match parse_property("time-pos", data)? { Property::TimePos(value) => Ok(value), @@ -423,7 +423,7 @@ impl MpvExt for Mpv { } } - async fn get_time_remaining(&self) -> Result { + async fn get_time_remaining(&self) -> Result, MpvError> { let data = self.get_property("time-remaining").await?; match parse_property("time-remaining", data)? { Property::TimeRemaining(value) => Ok(value), diff --git a/src/message_parser.rs b/src/message_parser.rs index 3955082..25febe5 100644 --- a/src/message_parser.rs +++ b/src/message_parser.rs @@ -169,14 +169,14 @@ fn json_map_to_playlist_entry( None => return Err(MpvError::MissingMpvData), }; let title = match map.get("title") { - Some(Value::String(s)) => s.to_string(), + Some(Value::String(s)) => Some(s.to_string()), Some(data) => { return Err(MpvError::ValueContainsUnexpectedType { expected_type: "String".to_owned(), received: data.clone(), }) } - None => return Err(MpvError::MissingMpvData), + None => None, }; let current = match map.get("current") { Some(Value::Bool(b)) => *b, @@ -186,7 +186,7 @@ fn json_map_to_playlist_entry( received: data.clone(), }) } - None => return Err(MpvError::MissingMpvData), + None => false, }; Ok(PlaylistEntry { id: 0, @@ -324,6 +324,10 @@ mod test { "filename": "file2", "title": "title2", "current": false + }, + { + "filename": "file3", + "current": false } ]); @@ -331,13 +335,19 @@ mod test { PlaylistEntry { id: 0, filename: "file1".to_string(), - title: "title1".to_string(), + title: Some("title1".to_string()), current: true, }, PlaylistEntry { id: 1, filename: "file2".to_string(), - title: "title2".to_string(), + title: Some("title2".to_string()), + current: false, + }, + PlaylistEntry { + id: 2, + filename: "file3".to_string(), + title: None, current: false, }, ]; diff --git a/src/property_parser.rs b/src/property_parser.rs index 6995b61..5b1586f 100644 --- a/src/property_parser.rs +++ b/src/property_parser.rs @@ -34,11 +34,12 @@ pub enum Property { PlaylistPos(Option), LoopFile(LoopProperty), LoopPlaylist(LoopProperty), - TimePos(f64), - TimeRemaining(f64), + TimePos(Option), + TimeRemaining(Option), Speed(f64), Volume(f64), Mute(bool), + EofReached(bool), Unknown { name: String, data: Option, @@ -204,31 +205,28 @@ pub fn parse_property(name: &str, data: Option) -> Result { let time_pos = match data { - Some(MpvDataType::Double(d)) => d, + Some(MpvDataType::Double(d)) => Some(d), Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "f64".to_owned(), received: data, }) } - None => { - return Err(MpvError::MissingMpvData); - } + None => None, }; + Ok(Property::TimePos(time_pos)) } "time-remaining" => { let time_remaining = match data { - Some(MpvDataType::Double(d)) => d, + Some(MpvDataType::Double(d)) => Some(d), Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "f64".to_owned(), received: data, }) } - None => { - return Err(MpvError::MissingMpvData); - } + None => None, }; Ok(Property::TimeRemaining(time_remaining)) } @@ -277,6 +275,19 @@ pub fn parse_property(name: &str, data: Option) -> Result { + let eof_reached = match data { + Some(MpvDataType::Bool(b)) => b, + Some(data) => { + return Err(MpvError::DataContainsUnexpectedType { + expected_type: "bool".to_owned(), + received: data, + }) + } + None => true, + }; + Ok(Property::EofReached(eof_reached)) + } // TODO: add missing cases _ => Ok(Property::Unknown { name: name.to_owned(), @@ -299,14 +310,14 @@ fn mpv_data_to_playlist_entry( None => return Err(MpvError::MissingMpvData), }; let title = match map.get("title") { - Some(MpvDataType::String(s)) => s.to_string(), + Some(MpvDataType::String(s)) => Some(s.to_string()), Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "String".to_owned(), received: data.clone(), }) } - None => return Err(MpvError::MissingMpvData), + None => None, }; let current = match map.get("current") { Some(MpvDataType::Bool(b)) => *b, @@ -316,7 +327,7 @@ fn mpv_data_to_playlist_entry( received: data.clone(), }) } - None => return Err(MpvError::MissingMpvData), + None => false }; Ok(PlaylistEntry { id: 0, diff --git a/tests/mock_socket_tests/get_property.rs b/tests/mock_socket_tests/get_property.rs index 1ce44e4..905d00a 100644 --- a/tests/mock_socket_tests/get_property.rs +++ b/tests/mock_socket_tests/get_property.rs @@ -182,19 +182,19 @@ async fn test_get_playlist() -> Result<(), MpvError> { PlaylistEntry { id: 0, filename: "file1".to_string(), - title: "title1".to_string(), + title: Some("title1".to_string()), current: false, }, PlaylistEntry { id: 1, filename: "file2".to_string(), - title: "title2".to_string(), + title: Some("title2".to_string()), current: true, }, PlaylistEntry { id: 2, filename: "file3".to_string(), - title: "title3".to_string(), + title: Some("title3".to_string()), current: false, }, ]);