2019-09-06 11:20:40 +00:00
|
|
|
use std::fmt;
|
2019-09-13 14:06:52 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use crate::Error;
|
2019-09-06 11:20:40 +00:00
|
|
|
|
|
|
|
/// Encryption method.
|
|
|
|
///
|
|
|
|
/// See: [4.3.2.4. EXT-X-KEY]
|
|
|
|
///
|
|
|
|
/// [4.3.2.4. EXT-X-KEY]: https://tools.ietf.org/html/rfc8216#section-4.3.2.4
|
|
|
|
#[allow(missing_docs)]
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum EncryptionMethod {
|
|
|
|
Aes128,
|
|
|
|
SampleAes,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for EncryptionMethod {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-09-14 19:42:06 +00:00
|
|
|
match &self {
|
2019-09-06 11:20:40 +00:00
|
|
|
EncryptionMethod::Aes128 => "AES-128".fmt(f),
|
|
|
|
EncryptionMethod::SampleAes => "SAMPLE-AES".fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for EncryptionMethod {
|
|
|
|
type Err = Error;
|
2019-09-13 14:06:52 +00:00
|
|
|
|
|
|
|
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
|
|
|
match input {
|
2019-09-06 11:20:40 +00:00
|
|
|
"AES-128" => Ok(EncryptionMethod::Aes128),
|
|
|
|
"SAMPLE-AES" => Ok(EncryptionMethod::SampleAes),
|
2019-09-13 14:06:52 +00:00
|
|
|
_ => Err(Error::custom(format!(
|
2019-09-06 11:20:40 +00:00
|
|
|
"Unknown encryption method: {:?}",
|
2019-09-13 14:06:52 +00:00
|
|
|
input
|
|
|
|
))),
|
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 encryption_method = EncryptionMethod::Aes128;
|
|
|
|
assert_eq!(encryption_method.to_string(), "AES-128".to_string());
|
|
|
|
|
|
|
|
let encryption_method = EncryptionMethod::SampleAes;
|
|
|
|
assert_eq!(encryption_method.to_string(), "SAMPLE-AES".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse() {
|
|
|
|
let encryption_method = EncryptionMethod::Aes128;
|
|
|
|
assert_eq!(
|
|
|
|
encryption_method,
|
|
|
|
"AES-128".parse::<EncryptionMethod>().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let encryption_method = EncryptionMethod::SampleAes;
|
|
|
|
assert_eq!(
|
|
|
|
encryption_method,
|
|
|
|
"SAMPLE-AES".parse::<EncryptionMethod>().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|