1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-06 23:49:21 +00:00
hls_m3u8/src/tags/media_segment/key.rs

158 lines
4 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
2019-09-17 12:45:10 +00:00
use std::ops::{Deref, DerefMut};
2019-09-06 10:55:00 +00:00
use std::str::FromStr;
2019-09-15 14:45:43 +00:00
use url::Url;
2019-09-17 12:45:10 +00:00
use crate::types::{DecryptionKey, EncryptionMethod};
use crate::utils::tag;
2019-09-13 14:06:52 +00:00
use crate::Error;
2019-09-06 10:55:00 +00:00
/// [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
2019-09-15 14:45:43 +00:00
/// # Note
/// In case of an empty key (`EncryptionMethod::None`), all attributes will be ignored.
2019-09-06 10:55:00 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-09-17 12:45:10 +00:00
pub struct ExtXKey(DecryptionKey);
2019-09-06 10:55:00 +00:00
impl ExtXKey {
pub(crate) const PREFIX: &'static str = "#EXT-X-KEY:";
/// Makes a new `ExtXKey` tag.
2019-09-15 14:45:43 +00:00
/// # Example
/// ```
/// use url::Url;
///
/// use hls_m3u8::tags::ExtXKey;
/// use hls_m3u8::types::EncryptionMethod;
///
/// let key = ExtXKey::new(
/// EncryptionMethod::Aes128,
/// "https://www.example.com".parse().unwrap()
/// );
///
/// assert_eq!(
/// key.to_string(),
/// "#EXT-X-KEY:METHOD=AES-128,URI=\"https://www.example.com/\""
/// );
/// ```
pub const fn new(method: EncryptionMethod, uri: Url) -> Self {
2019-09-17 12:45:10 +00:00
Self(DecryptionKey::new(method, uri))
2019-09-06 10:55:00 +00:00
}
/// Makes a new `ExtXKey` tag without a decryption key.
2019-09-15 14:45:43 +00:00
/// # Example
/// ```
/// use hls_m3u8::tags::ExtXKey;
///
/// let key = ExtXKey::empty();
///
/// assert_eq!(
/// key.to_string(),
/// "#EXT-X-KEY:METHOD=NONE"
/// );
/// ```
pub const fn empty() -> Self {
2019-09-17 12:45:10 +00:00
Self(DecryptionKey {
2019-09-15 14:45:43 +00:00
method: EncryptionMethod::None,
uri: None,
iv: None,
key_format: None,
key_format_versions: None,
2019-09-17 12:45:10 +00:00
})
2019-09-15 14:45:43 +00:00
}
2019-09-17 12:45:10 +00:00
/// Returns whether the [EncryptionMethod] is [None](EncryptionMethod::None).
2019-09-15 14:45:43 +00:00
/// # Example
/// ```
/// use hls_m3u8::tags::ExtXKey;
/// use hls_m3u8::types::EncryptionMethod;
///
/// let key = ExtXKey::empty();
///
/// assert_eq!(
/// key.method() == EncryptionMethod::None,
/// key.is_empty()
/// );
/// ```
pub fn is_empty(&self) -> bool {
2019-09-17 12:45:10 +00:00
self.0.method() == EncryptionMethod::None
2019-09-15 14:45:43 +00:00
}
2019-09-17 12:45:10 +00:00
}
2019-09-15 14:45:43 +00:00
2019-09-17 12:45:10 +00:00
impl FromStr for ExtXKey {
type Err = Error;
2019-09-06 10:55:00 +00:00
2019-09-17 12:45:10 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
let input = tag(input, Self::PREFIX)?;
Ok(Self(input.parse()?))
2019-09-06 10:55:00 +00:00
}
}
impl fmt::Display for ExtXKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-17 12:45:10 +00:00
write!(f, "{}{}", Self::PREFIX, self.0)
2019-09-06 10:55:00 +00:00
}
}
2019-09-17 12:45:10 +00:00
impl Deref for ExtXKey {
type Target = DecryptionKey;
2019-09-14 09:31:16 +00:00
2019-09-17 12:45:10 +00:00
fn deref(&self) -> &Self::Target {
&self.0
}
}
2019-09-15 14:45:43 +00:00
2019-09-17 12:45:10 +00:00
impl DerefMut for ExtXKey {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
2019-09-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
2019-09-15 14:45:43 +00:00
use crate::types::EncryptionMethod;
2019-09-06 10:55:00 +00:00
#[test]
2019-09-15 14:45:43 +00:00
fn test_display() {
assert_eq!(
ExtXKey::empty().to_string(),
"#EXT-X-KEY:METHOD=NONE".to_string()
);
2019-09-17 12:45:10 +00:00
let mut key = ExtXKey::empty();
// it is expected, that all attributes will be ignored in an empty key!
key.set_key_format("hi");
2019-09-15 14:45:43 +00:00
key.set_iv([
16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82,
]);
2019-09-17 12:45:10 +00:00
key.set_uri("https://www.example.com".parse().unwrap());
key.set_key_format_versions("1/2/3");
2019-09-15 14:45:43 +00:00
2019-09-17 12:45:10 +00:00
assert_eq!(key.to_string(), "#EXT-X-KEY:METHOD=NONE".to_string());
2019-09-15 14:45:43 +00:00
}
#[test]
fn test_parser() {
assert_eq!(
r#"#EXT-X-KEY:METHOD=AES-128,URI="https://priv.example.com/key.php?r=52""#
.parse::<ExtXKey>()
.unwrap(),
ExtXKey::new(
EncryptionMethod::Aes128,
"https://priv.example.com/key.php?r=52".parse().unwrap()
)
);
let mut key = ExtXKey::new(
EncryptionMethod::Aes128,
"https://www.example.com/hls-key/key.bin".parse().unwrap(),
);
key.set_iv([
16, 239, 143, 117, 140, 165, 85, 17, 85, 132, 187, 91, 60, 104, 127, 82,
]);
2019-09-06 10:55:00 +00:00
}
}