1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-03 05:59:22 +00:00
hls_m3u8/src/types/closed_captions.rs

63 lines
1.6 KiB
Rust
Raw Normal View History

2019-09-08 09:30:52 +00:00
use crate::utils::{quote, unquote};
2019-09-06 11:20:40 +00:00
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 {
2019-09-08 09:30:52 +00:00
GroupId(String),
2019-09-06 11:20:40 +00:00
None,
}
impl fmt::Display for ClosedCaptions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-14 19:42:06 +00:00
match &self {
ClosedCaptions::GroupId(value) => write!(f, "{}", quote(value)),
2019-09-06 11:20:40 +00:00
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 {
2019-09-08 09:30:52 +00:00
Ok(ClosedCaptions::GroupId(unquote(s)))
2019-09-06 11:20:40 +00:00
}
}
}
2019-09-06 11:46:21 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() {
let closed_captions = ClosedCaptions::None;
assert_eq!(closed_captions.to_string(), "NONE".to_string());
2019-09-08 09:30:52 +00:00
let closed_captions = ClosedCaptions::GroupId("value".into());
2019-09-06 11:46:21 +00:00
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());
2019-09-08 09:30:52 +00:00
let closed_captions = ClosedCaptions::GroupId("value".into());
2019-09-06 11:46:21 +00:00
assert_eq!(
closed_captions,
"\"value\"".parse::<ClosedCaptions>().unwrap()
);
}
}