1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-10 01:09:27 +00:00
hls_m3u8/src/types/key_format.rs

73 lines
1.9 KiB
Rust
Raw Normal View History

2019-09-22 16:00:38 +00:00
use std::fmt;
use std::str::FromStr;
2019-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
2019-09-22 16:00:38 +00:00
use crate::utils::{quote, tag, unquote};
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-22 16:00:38 +00:00
2020-03-25 10:49:16 +00:00
/// Specifies how the key is represented in the resource identified by the
/// `URI`.
2020-02-10 12:21:48 +00:00
#[non_exhaustive]
2020-02-02 12:38:11 +00:00
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
2019-09-22 16:00:38 +00:00
pub enum KeyFormat {
2020-03-25 10:49:16 +00:00
/// An [`EncryptionMethod::Aes128`] uses 16-octet (16 byte/128 bit) keys. If
/// the format is [`KeyFormat::Identity`], the key file is a single packed
/// array of 16 octets (16 byte/128 bit) in binary format.
///
/// [`EncryptionMethod::Aes128`]: crate::types::EncryptionMethod::Aes128
2019-09-22 16:00:38 +00:00
Identity,
}
impl Default for KeyFormat {
2019-10-03 15:01:15 +00:00
fn default() -> Self { Self::Identity }
2019-09-22 16:00:38 +00:00
}
impl FromStr for KeyFormat {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
tag(&unquote(input), "identity")?; // currently only KeyFormat::Identity exists!
Ok(Self::Identity)
}
}
impl fmt::Display for KeyFormat {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", quote(&"identity")) }
2019-09-22 16:00:38 +00:00
}
/// This tag requires [`ProtocolVersion::V5`].
2019-09-22 16:00:38 +00:00
impl RequiredVersion for KeyFormat {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V5 }
2019-09-22 16:00:38 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
2019-09-22 16:00:38 +00:00
#[test]
fn test_display() {
assert_eq!(KeyFormat::Identity.to_string(), quote("identity"));
}
#[test]
fn test_parser() {
assert_eq!(KeyFormat::Identity, quote("identity").parse().unwrap());
assert_eq!(KeyFormat::Identity, "identity".parse().unwrap());
2019-09-22 18:33:40 +00:00
assert!("garbage".parse::<KeyFormat>().is_err());
2019-09-22 16:00:38 +00:00
}
#[test]
fn test_required_version() {
assert_eq!(KeyFormat::Identity.required_version(), ProtocolVersion::V5)
}
#[test]
fn test_default() {
assert_eq!(KeyFormat::Identity, KeyFormat::default());
}
}