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