From 50f19b1f8e705b45811a5482ad8916e95ff70ee7 Mon Sep 17 00:00:00 2001 From: Oystein Tveit Date: Fri, 3 May 2024 14:02:45 +0200 Subject: [PATCH] wrap event data in `Option` --- src/event_parser.rs | 12 ++--- src/event_property_parser.rs | 96 ++++++++++++++++++++++-------------- tests/events.rs | 5 +- 3 files changed, 65 insertions(+), 48 deletions(-) diff --git a/src/event_parser.rs b/src/event_parser.rs index 5df061f..8518e67 100644 --- a/src/event_parser.rs +++ b/src/event_parser.rs @@ -111,7 +111,7 @@ pub enum Event { PropertyChange { id: usize, name: String, - data: MpvDataType, + data: Option, }, EventQueueOverflow, None, @@ -292,17 +292,11 @@ fn parse_client_message(event: &Map) -> Result { fn parse_property_change(event: &Map) -> Result { let id = get_key_as!(as_u64, "id", event) as usize; let property_name = get_key_as!(as_str, "name", event); - let data = event - .get("data") - .ok_or(MpvError::MissingKeyInObject { - key: "data".to_owned(), - map: event.clone(), - })? - .clone(); + let data = event.get("data").map(|d| json_to_value(d)).transpose()?; Ok(Event::PropertyChange { id, name: property_name.to_string(), - data: json_to_value(&data)?, + data: data, }) } diff --git a/src/event_property_parser.rs b/src/event_property_parser.rs index 3da8361..c3b7d2d 100644 --- a/src/event_property_parser.rs +++ b/src/event_property_parser.rs @@ -34,7 +34,7 @@ pub enum Property { Speed(f64), Volume(f64), Mute(bool), - Unknown { name: String, data: MpvDataType }, + Unknown { name: String, data: Option }, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -58,34 +58,40 @@ pub fn parse_event_property(event: Event) -> Result<(usize, Property), MpvError> match name.as_str() { "path" => { let path = match data { - MpvDataType::String(s) => Some(s), - MpvDataType::Null => None, - _ => { + Some(MpvDataType::String(s)) => Some(s), + Some(MpvDataType::Null) => None, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "String".to_owned(), received: data, }) } + None => { + return Err(MpvError::MissingMpvData); + } }; Ok((id, Property::Path(path))) } "pause" => { let pause = match data { - MpvDataType::Bool(b) => b, - _ => { + Some(MpvDataType::Bool(b)) => b, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "bool".to_owned(), received: data, }) } + None => { + return Err(MpvError::MissingMpvData); + } }; Ok((id, Property::Pause(pause))) } "playback-time" => { let playback_time = match data { - MpvDataType::Double(d) => Some(d), - MpvDataType::Null => None, - _ => { + Some(MpvDataType::Double(d)) => Some(d), + None | Some(MpvDataType::Null) => None, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "f64".to_owned(), received: data, @@ -96,9 +102,9 @@ pub fn parse_event_property(event: Event) -> Result<(usize, Property), MpvError> } "duration" => { let duration = match data { - MpvDataType::Double(d) => Some(d), - MpvDataType::Null => None, - _ => { + Some(MpvDataType::Double(d)) => Some(d), + None | Some(MpvDataType::Null) => None, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "f64".to_owned(), received: data, @@ -109,9 +115,9 @@ pub fn parse_event_property(event: Event) -> Result<(usize, Property), MpvError> } "metadata" => { let metadata = match data { - MpvDataType::HashMap(m) => Some(m), - MpvDataType::Null => None, - _ => { + Some(MpvDataType::HashMap(m)) => Some(m), + None | Some(MpvDataType::Null) => None, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "HashMap".to_owned(), received: data, @@ -130,10 +136,11 @@ pub fn parse_event_property(event: Event) -> Result<(usize, Property), MpvError> // } "playlist-pos" => { let playlist_pos = match data { - MpvDataType::Usize(u) => Some(u), - MpvDataType::MinusOne => None, - MpvDataType::Null => None, - _ => { + Some(MpvDataType::Usize(u)) => Some(u), + Some(MpvDataType::MinusOne) => None, + Some(MpvDataType::Null) => None, + None => None, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "usize or -1".to_owned(), received: data, @@ -144,76 +151,91 @@ pub fn parse_event_property(event: Event) -> Result<(usize, Property), MpvError> } "loop-file" => { let loop_file = match data.to_owned() { - MpvDataType::Usize(n) => Some(LoopProperty::N(n)), - MpvDataType::Bool(b) => match b { + Some(MpvDataType::Usize(n)) => Some(LoopProperty::N(n)), + Some(MpvDataType::Bool(b)) => match b { true => Some(LoopProperty::Inf), false => Some(LoopProperty::No), }, - MpvDataType::String(s) => match s.as_str() { + Some(MpvDataType::String(s)) => match s.as_str() { "inf" => Some(LoopProperty::Inf), _ => None, }, _ => None, } - .ok_or(MpvError::DataContainsUnexpectedType { - expected_type: "'inf', bool, or usize".to_owned(), - received: data, + .ok_or(match data { + Some(data) => MpvError::DataContainsUnexpectedType { + expected_type: "'inf', bool, or usize".to_owned(), + received: data, + }, + None => MpvError::MissingMpvData, })?; Ok((id, Property::LoopFile(loop_file))) } "loop-playlist" => { let loop_playlist = match data.to_owned() { - MpvDataType::Usize(n) => Some(LoopProperty::N(n)), - MpvDataType::Bool(b) => match b { + Some(MpvDataType::Usize(n)) => Some(LoopProperty::N(n)), + Some(MpvDataType::Bool(b)) => match b { true => Some(LoopProperty::Inf), false => Some(LoopProperty::No), }, - MpvDataType::String(s) => match s.as_str() { + Some(MpvDataType::String(s)) => match s.as_str() { "inf" => Some(LoopProperty::Inf), _ => None, }, _ => None, } - .ok_or(MpvError::DataContainsUnexpectedType { - expected_type: "'inf', bool, or usize".to_owned(), - received: data, + .ok_or(match data { + Some(data) => MpvError::DataContainsUnexpectedType { + expected_type: "'inf', bool, or usize".to_owned(), + received: data, + }, + None => MpvError::MissingMpvData, })?; Ok((id, Property::LoopPlaylist(loop_playlist))) } "speed" => { let speed = match data { - MpvDataType::Double(d) => d, - _ => { + Some(MpvDataType::Double(d)) => d, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "f64".to_owned(), received: data, }) } + None => { + return Err(MpvError::MissingMpvData); + } }; Ok((id, Property::Speed(speed))) } "volume" => { let volume = match data { - MpvDataType::Double(d) => d, - _ => { + Some(MpvDataType::Double(d)) => d, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "f64".to_owned(), received: data, }) } + None => { + return Err(MpvError::MissingMpvData); + } }; Ok((id, Property::Volume(volume))) } "mute" => { let mute = match data { - MpvDataType::Bool(b) => b, - _ => { + Some(MpvDataType::Bool(b)) => b, + Some(data) => { return Err(MpvError::DataContainsUnexpectedType { expected_type: "bool".to_owned(), received: data, }) } + None => { + return Err(MpvError::MissingMpvData); + } }; Ok((id, Property::Mute(mute))) } diff --git a/tests/events.rs b/tests/events.rs index 0d59bef..1e482af 100644 --- a/tests/events.rs +++ b/tests/events.rs @@ -60,8 +60,9 @@ async fn test_observe_event_successful() { Err(err) => panic!("{:?}", err), }; match data { - MpvDataType::Double(data) => assert_eq!(data, 64.0), - err => panic!("{:?}", err), + Some(MpvDataType::Double(data)) => assert_eq!(data, 64.0), + Some(data) => panic!("Unexpected value: {:?}", data), + None => panic!("No data"), } });