1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-18 08:22:59 +00:00
hls_m3u8/src/types/closed_captions.rs
2019-09-06 13:46:21 +02:00

63 lines
1.7 KiB
Rust

use crate::types::QuotedString;
use crate::{Error, Result};
use std::fmt;
use std::str::{self, FromStr};
/// The identifier of a closed captions group or its absence.
///
/// See: [4.3.4.2. EXT-X-STREAM-INF]
///
/// [4.3.4.2. EXT-X-STREAM-INF]: https://tools.ietf.org/html/rfc8216#section-4.3.4.2
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ClosedCaptions {
GroupId(QuotedString),
None,
}
impl fmt::Display for ClosedCaptions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ClosedCaptions::GroupId(ref x) => x.fmt(f),
ClosedCaptions::None => "NONE".fmt(f),
}
}
}
impl FromStr for ClosedCaptions {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
if s == "NONE" {
Ok(ClosedCaptions::None)
} else {
Ok(ClosedCaptions::GroupId(track!(s.parse())?))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() {
let closed_captions = ClosedCaptions::None;
assert_eq!(closed_captions.to_string(), "NONE".to_string());
let closed_captions = ClosedCaptions::GroupId(QuotedString::new("value").unwrap());
assert_eq!(closed_captions.to_string(), "\"value\"".to_string());
}
#[test]
fn test_parse() {
let closed_captions = ClosedCaptions::None;
assert_eq!(closed_captions, "NONE".parse::<ClosedCaptions>().unwrap());
let closed_captions = ClosedCaptions::GroupId(QuotedString::new("value").unwrap());
assert_eq!(
closed_captions,
"\"value\"".parse::<ClosedCaptions>().unwrap()
);
}
}