1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 13:39:27 +00:00
hls_m3u8/src/types/closed_captions.rs

130 lines
3.7 KiB
Rust
Raw Permalink Normal View History

use core::convert::{Infallible, TryFrom};
use std::borrow::Cow;
2019-09-15 17:09:48 +00:00
use std::fmt;
2019-09-08 09:30:52 +00:00
use crate::utils::{quote, unquote};
2019-09-06 11:20:40 +00:00
/// The identifier of a closed captions group or its absence.
2020-02-10 12:20:39 +00:00
#[non_exhaustive]
2019-09-22 18:33:40 +00:00
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum ClosedCaptions<'a> {
2020-02-10 12:20:39 +00:00
/// It indicates the set of closed-caption renditions that can be used when
/// playing the presentation.
///
/// The [`String`] must match [`ExtXMedia::group_id`] elsewhere in the
/// Playlist and it's [`ExtXMedia::media_type`] must be
/// [`MediaType::ClosedCaptions`].
///
/// [`ExtXMedia::group_id`]: crate::tags::ExtXMedia::group_id
/// [`ExtXMedia::media_type`]: crate::tags::ExtXMedia::media_type
/// [`MediaType::ClosedCaptions`]: crate::types::MediaType::ClosedCaptions
GroupId(Cow<'a, str>),
2020-02-21 19:42:44 +00:00
/// This variant indicates that there are no closed captions in
2020-02-10 12:20:39 +00:00
/// any [`VariantStream`] in the [`MasterPlaylist`], therefore all
/// [`VariantStream::ExtXStreamInf`] tags must have this attribute with a
/// value of [`ClosedCaptions::None`].
///
/// Having [`ClosedCaptions`] in one [`VariantStream`] but not in another
/// can trigger playback inconsistencies.
///
/// [`MasterPlaylist`]: crate::MasterPlaylist
/// [`VariantStream`]: crate::tags::VariantStream
/// [`VariantStream::ExtXStreamInf`]:
/// crate::tags::VariantStream::ExtXStreamInf
2019-09-06 11:20:40 +00:00
None,
}
impl<'a> ClosedCaptions<'a> {
2020-02-21 19:42:44 +00:00
/// Creates a [`ClosedCaptions::GroupId`] with the provided [`String`].
///
/// # Example
///
/// ```
/// use hls_m3u8::types::ClosedCaptions;
///
/// assert_eq!(
/// ClosedCaptions::group_id("vg1"),
/// ClosedCaptions::GroupId("vg1".into())
/// );
/// ```
#[inline]
#[must_use]
pub fn group_id<I: Into<Cow<'a, str>>>(value: I) -> Self {
2020-02-21 19:42:44 +00:00
//
Self::GroupId(value.into())
}
/// 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) -> ClosedCaptions<'static> {
match self {
Self::GroupId(id) => ClosedCaptions::GroupId(Cow::Owned(id.into_owned())),
Self::None => ClosedCaptions::None,
}
}
2020-02-21 19:42:44 +00:00
}
impl<'a, T: PartialEq<str>> PartialEq<T> for ClosedCaptions<'a> {
2020-02-21 19:42:44 +00:00
fn eq(&self, other: &T) -> bool {
match &self {
Self::GroupId(value) => other.eq(value),
Self::None => other.eq("NONE"),
}
}
}
impl<'a> fmt::Display for ClosedCaptions<'a> {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-09-14 19:42:06 +00:00
match &self {
2019-09-22 18:33:40 +00:00
Self::GroupId(value) => write!(f, "{}", quote(value)),
Self::None => write!(f, "NONE"),
2019-09-06 11:20:40 +00:00
}
}
}
impl<'a> TryFrom<&'a str> for ClosedCaptions<'a> {
type Error = Infallible;
2019-09-22 18:33:40 +00:00
fn try_from(input: &'a str) -> Result<Self, Self::Error> {
2019-09-22 18:33:40 +00:00
if input.trim() == "NONE" {
Ok(Self::None)
2019-09-06 11:20:40 +00:00
} else {
2019-09-22 18:33:40 +00:00
Ok(Self::GroupId(unquote(input)))
2019-09-06 11:20:40 +00:00
}
}
}
2019-09-06 11:46:21 +00:00
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
2019-09-06 11:46:21 +00:00
#[test]
fn test_display() {
2019-09-22 18:33:40 +00:00
assert_eq!(ClosedCaptions::None.to_string(), "NONE".to_string());
2019-09-06 11:46:21 +00:00
2019-09-22 18:33:40 +00:00
assert_eq!(
ClosedCaptions::GroupId("value".into()).to_string(),
"\"value\"".to_string()
);
2019-09-06 11:46:21 +00:00
}
#[test]
2019-09-21 11:24:05 +00:00
fn test_parser() {
2019-09-22 18:33:40 +00:00
assert_eq!(
ClosedCaptions::None,
ClosedCaptions::try_from("NONE").unwrap()
2019-09-22 18:33:40 +00:00
);
2019-09-06 11:46:21 +00:00
assert_eq!(
2019-09-22 18:33:40 +00:00
ClosedCaptions::GroupId("value".into()),
ClosedCaptions::try_from("\"value\"").unwrap()
2019-09-06 11:46:21 +00:00
);
}
}