This commit is contained in:
Oystein Kristoffer Tveit 2024-05-05 13:20:07 +02:00
parent eb81d7c463
commit c2bcdcf51a
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
5 changed files with 47 additions and 26 deletions

View File

@ -90,7 +90,7 @@ pub struct Playlist(pub Vec<PlaylistEntry>);
pub struct PlaylistEntry {
pub id: usize,
pub filename: String,
pub title: String,
pub title: Option<String>,
pub current: bool,
}

View File

@ -154,10 +154,10 @@ pub trait MpvExt {
async fn get_speed(&self) -> Result<f64, MpvError>;
/// Get the current position in the current video.
async fn get_time_pos(&self) -> Result<f64, MpvError>;
async fn get_time_pos(&self) -> Result<Option<f64>, MpvError>;
/// Get the amount of time remaining in the current video.
async fn get_time_remaining(&self) -> Result<f64, MpvError>;
async fn get_time_remaining(&self) -> Result<Option<f64>, MpvError>;
/// Get the total duration of the current video.
async fn get_duration(&self) -> Result<f64, MpvError>;
@ -415,7 +415,7 @@ impl MpvExt for Mpv {
}
}
async fn get_time_pos(&self) -> Result<f64, MpvError> {
async fn get_time_pos(&self) -> Result<Option<f64>, MpvError> {
let data = self.get_property("time-pos").await?;
match parse_property("time-pos", data)? {
Property::TimePos(value) => Ok(value),
@ -423,7 +423,7 @@ impl MpvExt for Mpv {
}
}
async fn get_time_remaining(&self) -> Result<f64, MpvError> {
async fn get_time_remaining(&self) -> Result<Option<f64>, MpvError> {
let data = self.get_property("time-remaining").await?;
match parse_property("time-remaining", data)? {
Property::TimeRemaining(value) => Ok(value),

View File

@ -169,14 +169,14 @@ fn json_map_to_playlist_entry(
None => return Err(MpvError::MissingMpvData),
};
let title = match map.get("title") {
Some(Value::String(s)) => s.to_string(),
Some(Value::String(s)) => Some(s.to_string()),
Some(data) => {
return Err(MpvError::ValueContainsUnexpectedType {
expected_type: "String".to_owned(),
received: data.clone(),
})
}
None => return Err(MpvError::MissingMpvData),
None => None,
};
let current = match map.get("current") {
Some(Value::Bool(b)) => *b,
@ -186,7 +186,7 @@ fn json_map_to_playlist_entry(
received: data.clone(),
})
}
None => return Err(MpvError::MissingMpvData),
None => false,
};
Ok(PlaylistEntry {
id: 0,
@ -324,6 +324,10 @@ mod test {
"filename": "file2",
"title": "title2",
"current": false
},
{
"filename": "file3",
"current": false
}
]);
@ -331,13 +335,19 @@ mod test {
PlaylistEntry {
id: 0,
filename: "file1".to_string(),
title: "title1".to_string(),
title: Some("title1".to_string()),
current: true,
},
PlaylistEntry {
id: 1,
filename: "file2".to_string(),
title: "title2".to_string(),
title: Some("title2".to_string()),
current: false,
},
PlaylistEntry {
id: 2,
filename: "file3".to_string(),
title: None,
current: false,
},
];

View File

@ -34,11 +34,12 @@ pub enum Property {
PlaylistPos(Option<usize>),
LoopFile(LoopProperty),
LoopPlaylist(LoopProperty),
TimePos(f64),
TimeRemaining(f64),
TimePos(Option<f64>),
TimeRemaining(Option<f64>),
Speed(f64),
Volume(f64),
Mute(bool),
EofReached(bool),
Unknown {
name: String,
data: Option<MpvDataType>,
@ -204,31 +205,28 @@ pub fn parse_property(name: &str, data: Option<MpvDataType>) -> Result<Property,
}
"time-pos" => {
let time_pos = match data {
Some(MpvDataType::Double(d)) => d,
Some(MpvDataType::Double(d)) => Some(d),
Some(data) => {
return Err(MpvError::DataContainsUnexpectedType {
expected_type: "f64".to_owned(),
received: data,
})
}
None => {
return Err(MpvError::MissingMpvData);
}
None => None,
};
Ok(Property::TimePos(time_pos))
}
"time-remaining" => {
let time_remaining = match data {
Some(MpvDataType::Double(d)) => d,
Some(MpvDataType::Double(d)) => Some(d),
Some(data) => {
return Err(MpvError::DataContainsUnexpectedType {
expected_type: "f64".to_owned(),
received: data,
})
}
None => {
return Err(MpvError::MissingMpvData);
}
None => None,
};
Ok(Property::TimeRemaining(time_remaining))
}
@ -277,6 +275,19 @@ pub fn parse_property(name: &str, data: Option<MpvDataType>) -> Result<Property,
};
Ok(Property::Mute(mute))
}
"eof-reached" => {
let eof_reached = match data {
Some(MpvDataType::Bool(b)) => b,
Some(data) => {
return Err(MpvError::DataContainsUnexpectedType {
expected_type: "bool".to_owned(),
received: data,
})
}
None => true,
};
Ok(Property::EofReached(eof_reached))
}
// TODO: add missing cases
_ => Ok(Property::Unknown {
name: name.to_owned(),
@ -299,14 +310,14 @@ fn mpv_data_to_playlist_entry(
None => return Err(MpvError::MissingMpvData),
};
let title = match map.get("title") {
Some(MpvDataType::String(s)) => s.to_string(),
Some(MpvDataType::String(s)) => Some(s.to_string()),
Some(data) => {
return Err(MpvError::DataContainsUnexpectedType {
expected_type: "String".to_owned(),
received: data.clone(),
})
}
None => return Err(MpvError::MissingMpvData),
None => None,
};
let current = match map.get("current") {
Some(MpvDataType::Bool(b)) => *b,
@ -316,7 +327,7 @@ fn mpv_data_to_playlist_entry(
received: data.clone(),
})
}
None => return Err(MpvError::MissingMpvData),
None => false
};
Ok(PlaylistEntry {
id: 0,

View File

@ -182,19 +182,19 @@ async fn test_get_playlist() -> Result<(), MpvError> {
PlaylistEntry {
id: 0,
filename: "file1".to_string(),
title: "title1".to_string(),
title: Some("title1".to_string()),
current: false,
},
PlaylistEntry {
id: 1,
filename: "file2".to_string(),
title: "title2".to_string(),
title: Some("title2".to_string()),
current: true,
},
PlaylistEntry {
id: 2,
filename: "file3".to_string(),
title: "title3".to_string(),
title: Some("title3".to_string()),
current: false,
},
]);