From cc9f2cae53f22ab99bba9fca5420fad2a729f854 Mon Sep 17 00:00:00 2001 From: jole Date: Tue, 19 Jul 2022 20:59:33 +0200 Subject: [PATCH] fix potential bug could potentially return MpvError("success") --- src/ipc.rs | 23 +++++++++++------------ src/lib.rs | 2 ++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/ipc.rs b/src/ipc.rs index 50bfb5f..055fa47 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -204,20 +204,19 @@ pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result Ok(b.to_string()), - Value::Number(ref n) => Ok(n.to_string()), - Value::String(ref s) => Ok(s.to_string()), - Value::Array(ref array) => Ok(format!("{:?}", array)), - Value::Object(ref map) => Ok(format!("{:?}", map)), - _ => Err(Error(ErrorCode::UnsupportedType)), - } + let data = if error == "success" { + Ok(&map["data"]) } else { - // TODO: there is a bug here - // this could return MpvError("success") if error == "success" but the map doesn't contain - // data Err(Error(ErrorCode::MpvError(error.to_string()))) + }?; + + match data { + Value::Bool(b) => Ok(b.to_string()), + Value::Number(ref n) => Ok(n.to_string()), + Value::String(ref s) => Ok(s.to_string()), + Value::Array(ref array) => Ok(format!("{:?}", array)), + Value::Object(ref map) => Ok(format!("{:?}", map)), + Value::Null => Err(Error(ErrorCode::MissingValue)), } } diff --git a/src/lib.rs b/src/lib.rs index 5bb122e..d38ef29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,6 +118,7 @@ pub enum ErrorCode { JsonContainsUnexptectedType, UnexpectedResult, UnexpectedValue, + MissingValue, UnsupportedType, ValueDoesNotContainBool, ValueDoesNotContainF64, @@ -190,6 +191,7 @@ impl Display for ErrorCode { } ErrorCode::UnexpectedResult => f.write_str("Unexpected result received"), ErrorCode::UnexpectedValue => f.write_str("Unexpected value received"), + ErrorCode::MissingValue => f.write_str("Missing value"), ErrorCode::UnsupportedType => f.write_str("Unsupported type received"), ErrorCode::ValueDoesNotContainBool => { f.write_str("The received value is not of type \'std::bool\'")