1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-17 16:02:59 +00:00
hls_m3u8/src/tags/master_playlist/session_data.rs

339 lines
9.9 KiB
Rust
Raw Normal View History

use std::borrow::Cow;
use std::convert::TryFrom;
2019-09-06 10:55:00 +00:00
use std::fmt;
2019-09-21 09:04:45 +00:00
use derive_builder::Builder;
2020-02-02 12:38:11 +00:00
use shorthand::ShortHand;
2019-09-10 09:05:20 +00:00
use crate::attribute::AttributePairs;
2019-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
2019-09-10 09:05:20 +00:00
use crate::utils::{quote, tag, unquote};
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-10 09:05:20 +00:00
/// The data of [`ExtXSessionData`].
2020-04-09 08:50:41 +00:00
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SessionData<'a> {
2020-04-09 08:50:41 +00:00
/// Contains the data identified by the [`ExtXSessionData::data_id`].
///
/// If a [`language`] is specified, this variant should contain a
/// human-readable string written in the specified language.
2019-10-05 14:08:03 +00:00
///
/// [`data_id`]: ExtXSessionData::data_id
/// [`language`]: ExtXSessionData::language
Value(Cow<'a, str>),
/// An [`URI`], which points to a [`json`] file.
2019-09-21 09:04:45 +00:00
///
2019-10-03 14:23:27 +00:00
/// [`json`]: https://tools.ietf.org/html/rfc8259
/// [`URI`]: https://tools.ietf.org/html/rfc3986
Uri(Cow<'a, str>),
}
impl<'a> SessionData<'a> {
/// Makes the struct independent of its lifetime, by taking ownership of all
/// internal [`Cow`]s.
///
/// # Note
///
/// This is a relatively expensive operation.
#[must_use]
pub fn into_owned(self) -> SessionData<'static> {
match self {
Self::Value(v) => SessionData::Value(Cow::Owned(v.into_owned())),
Self::Uri(v) => SessionData::Uri(Cow::Owned(v.into_owned())),
}
}
2019-09-15 14:45:43 +00:00
}
/// Allows arbitrary session data to be carried in a [`MasterPlaylist`].
2019-09-21 09:04:45 +00:00
///
2020-02-14 12:05:18 +00:00
/// [`MasterPlaylist`]: crate::MasterPlaylist
2020-02-02 12:38:11 +00:00
#[derive(ShortHand, Builder, Hash, Eq, Ord, Debug, PartialEq, Clone, PartialOrd)]
2019-09-21 09:04:45 +00:00
#[builder(setter(into))]
2020-02-02 12:38:11 +00:00
#[shorthand(enable(must_use, into))]
pub struct ExtXSessionData<'a> {
/// This should conform to a [reverse DNS] naming convention, such as
/// `com.example.movie.title`.
///
2019-09-21 09:04:45 +00:00
/// # Note
2020-02-02 12:38:11 +00:00
///
/// There is no central registration authority, so a value
/// should be choosen, that is unlikely to collide with others.
///
2019-09-21 09:04:45 +00:00
/// This field is required.
2019-10-05 14:08:03 +00:00
///
2020-02-02 12:38:11 +00:00
/// [reverse DNS]: https://en.wikipedia.org/wiki/Reverse_domain_name_notation
data_id: Cow<'a, str>,
/// The [`SessionData`] associated with the
/// [`data_id`](ExtXSessionData::data_id).
///
2019-09-21 09:04:45 +00:00
/// # Note
2019-10-05 14:08:03 +00:00
///
2020-02-02 12:38:11 +00:00
/// This field is required.
2020-04-09 08:50:41 +00:00
#[shorthand(enable(skip))]
pub data: SessionData<'a>,
2020-02-14 12:05:18 +00:00
/// The `language` attribute identifies the language of the [`SessionData`].
///
/// # Note
///
/// This field is optional and the provided value should conform to
/// [RFC5646].
///
/// [RFC5646]: https://tools.ietf.org/html/rfc5646
#[builder(setter(strip_option), default)]
language: Option<Cow<'a, str>>,
2019-09-06 10:55:00 +00:00
}
impl<'a> ExtXSessionData<'a> {
2019-09-06 10:55:00 +00:00
pub(crate) const PREFIX: &'static str = "#EXT-X-SESSION-DATA:";
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXSessionData`] tag.
2019-09-21 09:04:45 +00:00
///
/// # Example
2020-02-10 12:21:48 +00:00
///
2019-09-21 09:04:45 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXSessionData;
/// use hls_m3u8::tags::SessionData;
2019-09-21 09:04:45 +00:00
///
2020-04-09 08:50:41 +00:00
/// let session_data = ExtXSessionData::new(
2019-09-21 09:04:45 +00:00
/// "com.example.movie.title",
2020-04-09 08:50:41 +00:00
/// SessionData::Uri("https://www.example.com/".into()),
2019-09-21 09:04:45 +00:00
/// );
/// ```
2020-02-24 15:30:43 +00:00
#[must_use]
pub fn new<T: Into<Cow<'a, str>>>(data_id: T, data: SessionData<'a>) -> Self {
2019-09-22 18:33:40 +00:00
Self {
2020-02-10 12:21:48 +00:00
data_id: data_id.into(),
2019-09-06 10:55:00 +00:00
data,
language: None,
}
}
/// Returns a builder for [`ExtXSessionData`].
2019-09-21 09:04:45 +00:00
///
/// # Example
2020-02-10 12:21:48 +00:00
///
2019-09-21 09:04:45 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXSessionData;
/// use hls_m3u8::tags::SessionData;
2019-09-21 09:04:45 +00:00
///
/// let session_data = ExtXSessionData::builder()
/// .data_id("com.example.movie.title")
2020-04-09 08:50:41 +00:00
/// .data(SessionData::Value("some data".into()))
/// .language("en")
/// .build()?;
2020-04-09 08:50:41 +00:00
/// # Ok::<(), String>(())
2019-09-21 09:04:45 +00:00
/// ```
2020-02-24 15:30:43 +00:00
#[must_use]
pub fn builder() -> ExtXSessionDataBuilder<'a> { ExtXSessionDataBuilder::default() }
2019-09-21 09:04:45 +00:00
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXSessionData`] tag, with the given language.
2019-09-21 09:04:45 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-09-21 09:04:45 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXSessionData;
/// use hls_m3u8::tags::SessionData;
2019-09-21 09:04:45 +00:00
///
/// let session_data = ExtXSessionData::with_language(
/// "com.example.movie.title",
2020-04-09 08:50:41 +00:00
/// SessionData::Value("some data".into()),
/// "en",
2019-09-21 09:04:45 +00:00
/// );
/// ```
2020-02-24 15:30:43 +00:00
#[must_use]
pub fn with_language<T, K>(data_id: T, data: SessionData<'a>, language: K) -> Self
2020-02-10 12:21:48 +00:00
where
T: Into<Cow<'a, str>>,
K: Into<Cow<'a, str>>,
2020-02-10 12:21:48 +00:00
{
2019-09-22 18:33:40 +00:00
Self {
2020-02-10 12:21:48 +00:00
data_id: data_id.into(),
2019-09-06 10:55:00 +00:00
data,
2020-02-10 12:21:48 +00:00
language: Some(language.into()),
2019-09-06 10:55:00 +00:00
}
}
/// Makes the struct independent of its lifetime, by taking ownership of all
/// internal [`Cow`]s.
///
/// # Note
///
/// This is a relatively expensive operation.
#[must_use]
pub fn into_owned(self) -> ExtXSessionData<'static> {
ExtXSessionData {
data_id: Cow::Owned(self.data_id.into_owned()),
data: self.data.into_owned(),
language: self.language.map(|v| Cow::Owned(v.into_owned())),
}
}
2019-09-22 08:57:28 +00:00
}
2019-09-21 09:04:45 +00:00
2020-02-02 12:38:11 +00:00
/// This tag requires [`ProtocolVersion::V1`].
impl<'a> RequiredVersion for ExtXSessionData<'a> {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
2019-09-06 10:55:00 +00:00
}
impl<'a> fmt::Display for ExtXSessionData<'a> {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-09-06 10:55:00 +00:00
write!(f, "{}", Self::PREFIX)?;
2019-09-08 09:30:52 +00:00
write!(f, "DATA-ID={}", quote(&self.data_id))?;
2019-09-22 18:33:40 +00:00
2019-09-14 19:42:06 +00:00
match &self.data {
SessionData::Value(value) => write!(f, ",VALUE={}", quote(value))?,
SessionData::Uri(value) => write!(f, ",URI={}", quote(value))?,
2019-09-06 10:55:00 +00:00
}
2019-09-22 18:33:40 +00:00
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.language {
write!(f, ",LANGUAGE={}", quote(value))?;
2019-09-06 10:55:00 +00:00
}
2019-09-22 18:33:40 +00:00
2019-09-06 10:55:00 +00:00
Ok(())
}
}
impl<'a> TryFrom<&'a str> for ExtXSessionData<'a> {
type Error = Error;
2019-09-08 10:23:33 +00:00
fn try_from(input: &'a str) -> Result<Self, Self::Error> {
2019-09-10 09:05:20 +00:00
let input = tag(input, Self::PREFIX)?;
2019-09-06 10:55:00 +00:00
let mut data_id = None;
let mut session_value = None;
let mut uri = None;
let mut language = None;
2019-09-10 09:05:20 +00:00
for (key, value) in AttributePairs::new(input) {
match key {
2019-09-08 09:30:52 +00:00
"DATA-ID" => data_id = Some(unquote(value)),
"VALUE" => session_value = Some(unquote(value)),
"URI" => uri = Some(unquote(value)),
"LANGUAGE" => language = Some(unquote(value)),
2019-09-06 10:55:00 +00:00
_ => {
// [6.3.1. General Client Responsibilities]
2019-10-03 15:01:15 +00:00
// > ignore any attribute/value pair with an unrecognized
// AttributeName.
2019-09-06 10:55:00 +00:00
}
}
}
2019-09-22 18:33:40 +00:00
let data_id = data_id.ok_or_else(|| Error::missing_value("EXT-X-DATA-ID"))?;
2020-04-09 08:50:41 +00:00
2019-09-13 14:06:52 +00:00
let data = {
if let Some(value) = session_value {
if uri.is_some() {
2020-04-09 08:50:41 +00:00
return Err(Error::custom("unexpected URI"));
2019-09-13 14:06:52 +00:00
} else {
SessionData::Value(value)
}
} else if let Some(uri) = uri {
SessionData::Uri(uri)
} else {
2020-04-09 08:50:41 +00:00
return Err(Error::custom(
"expected either `SessionData::Uri` or `SessionData::Value`",
));
2019-09-13 14:06:52 +00:00
}
2019-09-06 10:55:00 +00:00
};
2019-09-13 14:06:52 +00:00
2019-09-22 18:33:40 +00:00
Ok(Self {
2019-09-06 10:55:00 +00:00
data_id,
data,
language,
})
}
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
2019-09-06 10:55:00 +00:00
macro_rules! generate_tests {
( $( { $struct:expr, $str:expr } ),+ $(,)* ) => {
#[test]
fn test_display() {
$(
assert_eq!($struct.to_string(), $str.to_string());
)+
}
2019-09-06 10:55:00 +00:00
#[test]
fn test_parser() {
$(
assert_eq!($struct, TryFrom::try_from($str).unwrap());
)+
2020-04-09 08:50:41 +00:00
assert!(
ExtXSessionData::try_from(concat!(
2020-04-09 08:50:41 +00:00
"#EXT-X-SESSION-DATA:",
"DATA-ID=\"foo\",",
"LANGUAGE=\"baz\""
))
2020-04-09 08:50:41 +00:00
.is_err()
);
2020-04-09 08:50:41 +00:00
assert!(
ExtXSessionData::try_from(concat!(
2020-04-09 08:50:41 +00:00
"#EXT-X-SESSION-DATA:",
"DATA-ID=\"foo\",",
"LANGUAGE=\"baz\",",
"VALUE=\"VALUE\",",
"URI=\"https://www.example.com/\""
))
2020-04-09 08:50:41 +00:00
.is_err()
);
}
2019-09-22 16:00:38 +00:00
}
2019-09-10 09:05:20 +00:00
}
generate_tests! {
{
2019-09-21 09:04:45 +00:00
ExtXSessionData::new(
"com.example.lyrics",
SessionData::Uri("lyrics.json".into())
),
concat!(
"#EXT-X-SESSION-DATA:",
"DATA-ID=\"com.example.lyrics\",",
"URI=\"lyrics.json\""
2019-09-21 09:04:45 +00:00
)
},
{
2019-09-21 09:04:45 +00:00
ExtXSessionData::with_language(
"com.example.title",
SessionData::Value("This is an example".into()),
2019-09-21 09:04:45 +00:00
"en"
),
concat!(
"#EXT-X-SESSION-DATA:",
"DATA-ID=\"com.example.title\",",
"VALUE=\"This is an example\",",
"LANGUAGE=\"en\""
2019-09-21 09:04:45 +00:00
)
},
{
2019-09-21 09:04:45 +00:00
ExtXSessionData::with_language(
"com.example.title",
SessionData::Value("Este es un ejemplo".into()),
2019-09-21 09:04:45 +00:00
"es"
),
concat!(
"#EXT-X-SESSION-DATA:",
"DATA-ID=\"com.example.title\",",
"VALUE=\"Este es un ejemplo\",",
"LANGUAGE=\"es\""
2019-09-21 09:04:45 +00:00
)
}
2019-09-22 16:00:38 +00:00
}
#[test]
fn test_required_version() {
assert_eq!(
ExtXSessionData::new("com.example.lyrics", SessionData::Uri("lyrics.json".into()))
.required_version(),
2019-09-22 16:00:38 +00:00
ProtocolVersion::V1
);
2019-09-10 09:05:20 +00:00
}
2019-09-06 10:55:00 +00:00
}