clean up get_mpv_property_string()

This commit is contained in:
jole 2022-07-19 20:54:44 +02:00
parent 8b5d3bc0fd
commit fde2bce07d
1 changed files with 28 additions and 23 deletions

View File

@ -189,10 +189,21 @@ pub fn get_mpv_property<T: TypeHandler>(instance: &Mpv, property: &str) -> Resul
pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result<String, Error> {
let ipc_string = format!("{{ \"command\": [\"get_property\",\"{}\"] }}\n", property);
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
Ok(val) => {
if let Value::Object(map) = val {
if let Value::String(ref error) = map["error"] {
let val = serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string))
.map_err(|why| Error(ErrorCode::JsonParseError(why.to_string())))?;
let map = if let Value::Object(map) = val {
Ok(map)
} else {
Err(Error(ErrorCode::UnexpectedValue))
}?;
let error = if let Value::String(ref error) = map["error"] {
Ok(error)
} else {
Err(Error(ErrorCode::UnexpectedValue))
}?;
if error == "success" && map.contains_key("data") {
match map["data"] {
Value::Bool(b) => Ok(b.to_string()),
@ -203,17 +214,11 @@ pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result<String,
_ => Err(Error(ErrorCode::UnsupportedType)),
}
} 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())))
}
} else {
Err(Error(ErrorCode::UnexpectedValue))
}
} else {
Err(Error(ErrorCode::UnexpectedValue))
}
}
Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))),
}
}
pub fn set_mpv_property<T: TypeHandler>(