fix potential bug

could potentially return MpvError("success")
pull/1/head
jole 2022-07-19 20:59:33 +02:00
parent fde2bce07d
commit cc9f2cae53
2 changed files with 13 additions and 12 deletions

View File

@ -204,20 +204,19 @@ pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result<String,
Err(Error(ErrorCode::UnexpectedValue))
}?;
if error == "success" && map.contains_key("data") {
match map["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)),
_ => 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)),
}
}

View File

@ -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\'")