1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-01 07:08:07 +00:00
hls_m3u8/src/tags/master_playlist/session_data.rs

378 lines
11 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
use std::str::FromStr;
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
2019-10-05 14:08:03 +00:00
/// The data of an [`ExtXSessionData`] tag.
2019-09-21 09:04:45 +00:00
#[derive(Hash, Eq, Ord, Debug, PartialEq, Clone, PartialOrd)]
2019-09-15 14:45:43 +00:00
pub enum SessionData {
2019-10-03 15:01:15 +00:00
/// A String, that contains the data identified by
2019-10-05 14:08:03 +00:00
/// [`data_id`].
/// If a [`language`] is specified, the value
2019-10-03 15:01:15 +00:00
/// 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
2019-09-15 14:45:43 +00:00
Value(String),
2019-10-03 14:23:27 +00:00
/// An [`uri`], which points to a [`json`].
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
2019-09-15 14:45:43 +00:00
Uri(String),
}
2019-09-22 16:00:38 +00:00
/// # [4.3.4.4. EXT-X-SESSION-DATA]
2019-09-06 10:55:00 +00:00
///
2019-10-03 14:23:27 +00:00
/// The [`ExtXSessionData`] tag allows arbitrary session data to be
/// carried in a [`Master Playlist`].
2019-09-21 09:04:45 +00:00
///
2019-10-03 14:23:27 +00:00
/// [`Master Playlist`]: crate::MasterPlaylist
2019-09-06 10:55:00 +00:00
/// [4.3.4.4. EXT-X-SESSION-DATA]: https://tools.ietf.org/html/rfc8216#section-4.3.4.4
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))]
2019-09-06 10:55:00 +00:00
pub struct ExtXSessionData {
2020-02-02 12:38:11 +00:00
/// Sets the `data_id` attribute, that should conform to a [reverse DNS]
/// naming convention, such as `com.example.movie.title`.
2019-10-03 14:23:27 +00:00
///
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
2019-09-08 09:30:52 +00:00
data_id: String,
2019-10-05 14:08:03 +00:00
/// The data associated with the [`data_id`].
2019-10-03 14:23:27 +00:00
/// For more information look [`here`](SessionData).
///
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.
2019-09-06 10:55:00 +00:00
data: SessionData,
2020-02-02 12:38:11 +00:00
/// The `language` attribute identifies the language of [`SessionData`].
/// See [rfc5646](https://tools.ietf.org/html/rfc5646).
2019-09-21 09:04:45 +00:00
#[builder(setter(into, strip_option), default)]
2019-09-08 09:30:52 +00:00
language: Option<String>,
2019-09-06 10:55:00 +00:00
}
impl ExtXSessionData {
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
/// ```
/// use hls_m3u8::tags::{ExtXSessionData, SessionData};
///
/// ExtXSessionData::new(
/// "com.example.movie.title",
2019-10-03 15:01:15 +00:00
/// SessionData::Uri("https://www.example.com/".to_string()),
2019-09-21 09:04:45 +00:00
/// );
/// ```
2019-09-08 09:30:52 +00:00
pub fn new<T: ToString>(data_id: T, data: SessionData) -> Self {
2019-09-22 18:33:40 +00:00
Self {
2019-09-08 09:30:52 +00:00
data_id: data_id.to_string(),
2019-09-06 10:55:00 +00:00
data,
language: None,
}
}
2019-10-03 14:23:27 +00:00
/// Returns a new Builder for [`ExtXSessionData`].
2019-09-21 09:04:45 +00:00
///
/// # Example
/// ```
/// use hls_m3u8::tags::{ExtXSessionData, SessionData};
///
/// let session_data = ExtXSessionData::builder()
/// .data_id("com.example.movie.title")
/// .data(SessionData::Value("some data".to_string()))
/// .language("english")
/// .build()
/// .expect("Failed to build an ExtXSessionData tag.");
///
/// assert_eq!(
/// session_data,
/// ExtXSessionData::with_language(
/// "com.example.movie.title",
/// SessionData::Value("some data".to_string()),
/// "english"
/// )
/// );
/// ```
2019-10-03 15:01:15 +00:00
pub fn builder() -> ExtXSessionDataBuilder { 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, SessionData};
///
/// let session_data = ExtXSessionData::with_language(
/// "com.example.movie.title",
/// SessionData::Value("some data".to_string()),
2019-10-03 15:01:15 +00:00
/// "english",
2019-09-21 09:04:45 +00:00
/// );
/// ```
2019-09-08 09:30:52 +00:00
pub fn with_language<T: ToString>(data_id: T, data: SessionData, language: T) -> Self {
2019-09-22 18:33:40 +00:00
Self {
2019-09-08 09:30:52 +00:00
data_id: data_id.to_string(),
2019-09-06 10:55:00 +00:00
data,
2019-09-08 09:30:52 +00:00
language: Some(language.to_string()),
2019-09-06 10:55:00 +00:00
}
}
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`].
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXSessionData {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
2019-09-06 10:55:00 +00:00
}
impl fmt::Display for ExtXSessionData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
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 FromStr for ExtXSessionData {
type Err = Error;
2019-09-08 10:23:33 +00:00
2019-09-10 09:05:20 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
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"))?;
2019-09-13 14:06:52 +00:00
let data = {
if let Some(value) = session_value {
if uri.is_some() {
2019-09-21 09:04:45 +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 {
return Err(Error::invalid_input());
}
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
#[test]
2019-09-10 09:05:20 +00:00
fn test_display() {
2019-09-22 16:00:38 +00:00
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"com.example.lyrics\",\
URI=\"lyrics.json\""
.to_string(),
2019-09-22 16:00:38 +00:00
ExtXSessionData::new(
"com.example.lyrics",
SessionData::Uri("lyrics.json".to_string())
)
.to_string()
);
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"com.example.title\",\
VALUE=\"This is an example\",\
LANGUAGE=\"en\""
2019-09-22 16:00:38 +00:00
.to_string(),
ExtXSessionData::with_language(
"com.example.title",
SessionData::Value("This is an example".to_string()),
"en"
)
.to_string()
);
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"com.example.title\",\
VALUE=\"Este es un ejemplo\",\
LANGUAGE=\"es\""
2019-09-22 16:00:38 +00:00
.to_string(),
ExtXSessionData::with_language(
"com.example.title",
SessionData::Value("Este es un ejemplo".to_string()),
"es"
)
.to_string()
);
2019-09-06 10:55:00 +00:00
2019-09-22 16:00:38 +00:00
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"foo\",\
VALUE=\"bar\""
.to_string(),
2019-09-22 16:00:38 +00:00
ExtXSessionData::new("foo", SessionData::Value("bar".into())).to_string()
);
2019-09-06 10:55:00 +00:00
2019-09-22 16:00:38 +00:00
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"foo\",\
URI=\"bar\""
.to_string(),
2019-09-22 16:00:38 +00:00
ExtXSessionData::new("foo", SessionData::Uri("bar".into())).to_string()
);
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"foo\",\
VALUE=\"bar\",\
LANGUAGE=\"baz\""
.to_string(),
2019-09-22 16:00:38 +00:00
ExtXSessionData::with_language("foo", SessionData::Value("bar".into()), "baz")
.to_string()
);
2019-09-10 09:05:20 +00:00
}
#[test]
fn test_parser() {
2019-09-21 09:04:45 +00:00
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"com.example.lyrics\",\
URI=\"lyrics.json\""
2019-09-22 16:00:38 +00:00
.parse::<ExtXSessionData>()
.unwrap(),
2019-09-21 09:04:45 +00:00
ExtXSessionData::new(
"com.example.lyrics",
SessionData::Uri("lyrics.json".to_string())
)
);
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"com.example.title\",\
LANGUAGE=\"en\",\
VALUE=\"This is an example\""
2019-09-22 16:00:38 +00:00
.parse::<ExtXSessionData>()
.unwrap(),
2019-09-21 09:04:45 +00:00
ExtXSessionData::with_language(
"com.example.title",
SessionData::Value("This is an example".to_string()),
"en"
)
);
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"com.example.title\",\
LANGUAGE=\"es\",\
VALUE=\"Este es un ejemplo\""
2019-09-22 16:00:38 +00:00
.parse::<ExtXSessionData>()
.unwrap(),
2019-09-21 09:04:45 +00:00
ExtXSessionData::with_language(
"com.example.title",
SessionData::Value("Este es un ejemplo".to_string()),
"es"
)
);
2019-09-22 16:00:38 +00:00
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"foo\",\
VALUE=\"bar\""
2019-09-22 16:00:38 +00:00
.parse::<ExtXSessionData>()
.unwrap(),
ExtXSessionData::new("foo", SessionData::Value("bar".into()))
);
2019-09-10 09:05:20 +00:00
2019-09-22 16:00:38 +00:00
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"foo\",\
URI=\"bar\""
2019-09-22 16:00:38 +00:00
.parse::<ExtXSessionData>()
.unwrap(),
ExtXSessionData::new("foo", SessionData::Uri("bar".into()))
);
2019-09-10 09:05:20 +00:00
2019-09-22 16:00:38 +00:00
assert_eq!(
2019-09-22 18:33:40 +00:00
"#EXT-X-SESSION-DATA:\
DATA-ID=\"foo\",\
VALUE=\"bar\",\
LANGUAGE=\"baz\",\
UNKNOWN=TAG"
2019-09-22 16:00:38 +00:00
.parse::<ExtXSessionData>()
.unwrap(),
ExtXSessionData::with_language("foo", SessionData::Value("bar".into()), "baz")
);
2019-09-22 18:33:40 +00:00
assert!("#EXT-X-SESSION-DATA:\
DATA-ID=\"foo\",\
LANGUAGE=\"baz\""
.parse::<ExtXSessionData>()
.is_err());
assert!("#EXT-X-SESSION-DATA:\
DATA-ID=\"foo\",\
LANGUAGE=\"baz\",\
VALUE=\"VALUE\",\
URI=\"https://www.example.com/\""
.parse::<ExtXSessionData>()
.is_err());
2019-09-22 16:00:38 +00:00
}
#[test]
fn test_required_version() {
assert_eq!(
ExtXSessionData::new(
"com.example.lyrics",
SessionData::Uri("lyrics.json".to_string())
)
.required_version(),
ProtocolVersion::V1
);
2019-09-10 09:05:20 +00:00
}
2019-09-06 10:55:00 +00:00
}