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,30 +189,35 @@ 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 {
if error == "success" && map.contains_key("data") { Ok(map)
match map["data"] { } else {
Value::Bool(b) => Ok(b.to_string()), Err(Error(ErrorCode::UnexpectedValue))
Value::Number(ref n) => Ok(n.to_string()), }?;
Value::String(ref s) => Ok(s.to_string()),
Value::Array(ref array) => Ok(format!("{:?}", array)), let error = if let Value::String(ref error) = map["error"] {
Value::Object(ref map) => Ok(format!("{:?}", map)), Ok(error)
_ => Err(Error(ErrorCode::UnsupportedType)), } else {
} Err(Error(ErrorCode::UnexpectedValue))
} else { }?;
Err(Error(ErrorCode::MpvError(error.to_string())))
} if error == "success" && map.contains_key("data") {
} else { match map["data"] {
Err(Error(ErrorCode::UnexpectedValue)) Value::Bool(b) => Ok(b.to_string()),
} Value::Number(ref n) => Ok(n.to_string()),
} else { Value::String(ref s) => Ok(s.to_string()),
Err(Error(ErrorCode::UnexpectedValue)) Value::Array(ref array) => Ok(format!("{:?}", array)),
} Value::Object(ref map) => Ok(format!("{:?}", map)),
_ => Err(Error(ErrorCode::UnsupportedType)),
} }
Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))), } 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())))
} }
} }