clean up listen()

This commit is contained in:
jole 2022-07-19 20:34:11 +02:00
parent 442f121b2e
commit 8b5d3bc0fd
1 changed files with 61 additions and 111 deletions

View File

@ -338,125 +338,75 @@ pub fn listen(instance: &mut Mpv) -> Result<Event, Error> {
instance.reader.read_line(&mut response).unwrap(); instance.reader.read_line(&mut response).unwrap();
response = response.trim_end().to_string(); response = response.trim_end().to_string();
debug!("Event: {}", response); debug!("Event: {}", response);
match serde_json::from_str::<Value>(&response) {
Ok(e) => { let e = serde_json::from_str::<Value>(&response)
.map_err(|why| Error(ErrorCode::JsonParseError(why.to_string())))?;
if let Value::String(ref name) = e["event"] { if let Value::String(ref name) = e["event"] {
let event: Event; let event = match name.as_str() {
match name.as_str() { "shutdown" => Event::Shutdown,
"shutdown" => { "start-file" => Event::StartFile,
event = Event::Shutdown; "file-loaded" => Event::FileLoaded,
} "seek" => Event::Seek,
"start-file" => { "playback-restart" => Event::PlaybackRestart,
event = Event::StartFile; "idle" => Event::Idle,
} "tick" => Event::Tick,
"file-loaded" => { "video-reconfig" => Event::VideoReconfig,
event = Event::FileLoaded; "audio-reconfig" => Event::AudioReconfig,
} "tracks-changed" => Event::TracksChanged,
"seek" => { "track-switched" => Event::TrackSwitched,
event = Event::Seek; "pause" => Event::Pause,
} "unpause" => Event::Unpause,
"playback-restart" => { "metadata-update" => Event::MetadataUpdate,
event = Event::PlaybackRestart; "chapter-change" => Event::ChapterChange,
} "end-file" => Event::EndFile,
"idle" => {
event = Event::Idle;
}
"tick" => {
event = Event::Tick;
}
"video-reconfig" => {
event = Event::VideoReconfig;
}
"audio-reconfig" => {
event = Event::AudioReconfig;
}
"tracks-changed" => {
event = Event::TracksChanged;
}
"track-switched" => {
event = Event::TrackSwitched;
}
"pause" => {
event = Event::Pause;
}
"unpause" => {
event = Event::Unpause;
}
"metadata-update" => {
event = Event::MetadataUpdate;
}
"chapter-change" => {
event = Event::ChapterChange;
}
"end-file" => {
event = Event::EndFile;
}
"property-change" => { "property-change" => {
let name: String; let name = match e["name"] {
let id: usize; Value::String(ref n) => Ok(n.to_string()),
let data: MpvDataType; _ => Err(Error(ErrorCode::JsonContainsUnexptectedType)),
}?;
if let Value::String(ref n) = e["name"] { let id: usize = match e["id"] {
name = n.to_string(); Value::Number(ref n) => n.as_u64().unwrap() as usize,
} else { _ => 0,
return Err(Error(ErrorCode::JsonContainsUnexptectedType)); };
}
if let Value::Number(ref n) = e["id"] { let data: MpvDataType = match e["data"] {
id = n.as_i64().unwrap() as usize; Value::String(ref n) => MpvDataType::String(n.to_string()),
} else {
id = 0;
}
match e["data"] {
Value::String(ref n) => {
data = MpvDataType::String(n.to_string());
}
Value::Array(ref a) => { Value::Array(ref a) => {
if name == "playlist".to_string() { if name == "playlist".to_string() {
data = MpvDataType::Playlist(Playlist(json_array_to_playlist(a)))
MpvDataType::Playlist(Playlist(json_array_to_playlist(a)));
} else { } else {
data = MpvDataType::Array(json_array_to_vec(a)); MpvDataType::Array(json_array_to_vec(a))
} }
} }
Value::Bool(ref b) => { Value::Bool(b) => MpvDataType::Bool(b),
data = MpvDataType::Bool(*b);
}
Value::Number(ref n) => { Value::Number(ref n) => {
if n.is_u64() { if n.is_u64() {
data = MpvDataType::Usize(n.as_u64().unwrap() as usize); MpvDataType::Usize(n.as_u64().unwrap() as usize)
} else if n.is_f64() { } else if n.is_f64() {
data = MpvDataType::Double(n.as_f64().unwrap()); MpvDataType::Double(n.as_f64().unwrap())
} else { } else {
return Err(Error(ErrorCode::JsonContainsUnexptectedType)); return Err(Error(ErrorCode::JsonContainsUnexptectedType));
} }
} }
Value::Object(ref m) => { Value::Object(ref m) => MpvDataType::HashMap(json_map_to_hashmap(m)),
data = MpvDataType::HashMap(json_map_to_hashmap(m));
}
Value::Null => { Value::Null => MpvDataType::Null,
data = MpvDataType::Null;
}
}
event = try_convert_property(name.as_ref(), id, data);
}
_ => {
event = Event::Unimplemented;
}
}; };
return Ok(event);
} try_convert_property(name.as_ref(), id, data)
}
Err(why) => return Err(Error(ErrorCode::JsonParseError(why.to_string()))),
} }
_ => Event::Unimplemented,
};
Ok(event)
} else {
unreachable!(); unreachable!();
}
} }
pub fn listen_raw(instance: &mut Mpv) -> String { pub fn listen_raw(instance: &mut Mpv) -> String {