2020-03-23 12:34:26 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2019-10-04 09:02:21 +00:00
|
|
|
|
2020-04-18 10:50:45 +00:00
|
|
|
use stable_vec::StableVec;
|
|
|
|
|
2020-03-23 12:34:26 +00:00
|
|
|
use crate::types::{DecryptionKey, ProtocolVersion};
|
2019-10-04 09:02:21 +00:00
|
|
|
|
2020-03-23 12:34:26 +00:00
|
|
|
mod private {
|
|
|
|
pub trait Sealed {}
|
2020-04-22 07:54:48 +00:00
|
|
|
impl<'a> Sealed for crate::MediaSegment<'a> {}
|
|
|
|
impl<'a> Sealed for crate::tags::ExtXMap<'a> {}
|
2020-03-23 12:34:26 +00:00
|
|
|
}
|
2019-10-04 09:02:21 +00:00
|
|
|
|
2020-03-23 12:34:26 +00:00
|
|
|
/// Signals that a type or some of the asssociated data might need to be
|
|
|
|
/// decrypted.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// You are not supposed to implement this trait, therefore it is "sealed".
|
2020-04-22 07:54:48 +00:00
|
|
|
pub trait Decryptable<'a>: private::Sealed {
|
2020-03-23 12:34:26 +00:00
|
|
|
/// Returns all keys, associated with the type.
|
2019-10-04 09:02:21 +00:00
|
|
|
///
|
2020-03-23 12:34:26 +00:00
|
|
|
/// # Example
|
2020-02-02 12:38:11 +00:00
|
|
|
///
|
2020-03-23 12:34:26 +00:00
|
|
|
/// ```
|
|
|
|
/// use hls_m3u8::tags::ExtXMap;
|
|
|
|
/// use hls_m3u8::types::{ByteRange, EncryptionMethod};
|
|
|
|
/// use hls_m3u8::Decryptable;
|
|
|
|
///
|
|
|
|
/// let map = ExtXMap::with_range("https://www.example.url/", ByteRange::from(2..11));
|
2020-02-02 12:38:11 +00:00
|
|
|
///
|
2020-03-23 12:34:26 +00:00
|
|
|
/// for key in map.keys() {
|
|
|
|
/// if key.method == EncryptionMethod::Aes128 {
|
|
|
|
/// // fetch content with the uri and decrypt the result
|
|
|
|
/// break;
|
|
|
|
/// }
|
|
|
|
/// }
|
2019-10-04 09:02:21 +00:00
|
|
|
/// ```
|
2020-03-23 12:34:26 +00:00
|
|
|
#[must_use]
|
2020-04-22 07:54:48 +00:00
|
|
|
fn keys(&self) -> Vec<&DecryptionKey<'a>>;
|
2020-02-14 12:05:18 +00:00
|
|
|
|
2020-03-23 12:34:26 +00:00
|
|
|
/// Most of the time only a single key is provided, so instead of iterating
|
|
|
|
/// through all keys, one might as well just get the first key.
|
|
|
|
#[must_use]
|
2020-03-25 13:11:11 +00:00
|
|
|
#[inline]
|
2020-04-22 07:54:48 +00:00
|
|
|
fn first_key(&self) -> Option<&DecryptionKey<'a>> {
|
2020-03-23 12:34:26 +00:00
|
|
|
<Self as Decryptable>::keys(self).first().copied()
|
2019-10-04 09:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 12:34:26 +00:00
|
|
|
/// Returns the number of keys.
|
|
|
|
#[must_use]
|
2020-03-25 13:11:11 +00:00
|
|
|
#[inline]
|
2020-03-23 12:34:26 +00:00
|
|
|
fn len(&self) -> usize { <Self as Decryptable>::keys(self).len() }
|
|
|
|
|
2020-03-25 13:11:11 +00:00
|
|
|
/// Returns `true`, if the number of keys is zero.
|
2020-03-23 12:34:26 +00:00
|
|
|
#[must_use]
|
2020-03-25 13:11:11 +00:00
|
|
|
#[inline]
|
2020-03-23 12:34:26 +00:00
|
|
|
fn is_empty(&self) -> bool { <Self as Decryptable>::len(self) == 0 }
|
2019-10-04 09:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 13:11:11 +00:00
|
|
|
#[doc(hidden)]
|
2019-10-04 09:02:21 +00:00
|
|
|
pub trait RequiredVersion {
|
|
|
|
/// Returns the protocol compatibility version that this tag requires.
|
2019-10-06 15:30:24 +00:00
|
|
|
///
|
|
|
|
/// # Note
|
2020-02-02 12:38:11 +00:00
|
|
|
///
|
2019-10-06 15:30:24 +00:00
|
|
|
/// This is for the latest working [`ProtocolVersion`] and a client, that
|
|
|
|
/// only supports an older version would break.
|
2020-03-25 13:11:11 +00:00
|
|
|
#[must_use]
|
2019-10-04 09:02:21 +00:00
|
|
|
fn required_version(&self) -> ProtocolVersion;
|
2019-10-06 15:30:24 +00:00
|
|
|
|
|
|
|
/// The protocol version, in which the tag has been introduced.
|
2020-03-25 13:11:11 +00:00
|
|
|
#[must_use]
|
2019-10-06 15:30:24 +00:00
|
|
|
fn introduced_version(&self) -> ProtocolVersion { self.required_version() }
|
2019-10-04 09:02:21 +00:00
|
|
|
}
|
2019-10-05 10:49:08 +00:00
|
|
|
|
|
|
|
impl<T: RequiredVersion> RequiredVersion for Vec<T> {
|
|
|
|
fn required_version(&self) -> ProtocolVersion {
|
|
|
|
self.iter()
|
2020-02-14 12:05:18 +00:00
|
|
|
.map(RequiredVersion::required_version)
|
2019-10-05 10:49:08 +00:00
|
|
|
.max()
|
|
|
|
// return ProtocolVersion::V1, if the iterator is empty:
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 13:11:11 +00:00
|
|
|
impl<K, V: RequiredVersion> RequiredVersion for BTreeMap<K, V> {
|
|
|
|
fn required_version(&self) -> ProtocolVersion {
|
|
|
|
self.values()
|
|
|
|
.map(RequiredVersion::required_version)
|
|
|
|
.max()
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-05 10:49:08 +00:00
|
|
|
impl<T: RequiredVersion> RequiredVersion for Option<T> {
|
|
|
|
fn required_version(&self) -> ProtocolVersion {
|
|
|
|
self.iter()
|
2020-02-14 12:05:18 +00:00
|
|
|
.map(RequiredVersion::required_version)
|
2019-10-05 10:49:08 +00:00
|
|
|
.max()
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 12:05:18 +00:00
|
|
|
|
2020-03-25 13:11:11 +00:00
|
|
|
impl<K, V: RequiredVersion, S> RequiredVersion for HashMap<K, V, S> {
|
|
|
|
fn required_version(&self) -> ProtocolVersion {
|
|
|
|
self.values()
|
|
|
|
.map(RequiredVersion::required_version)
|
|
|
|
.max()
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 10:50:45 +00:00
|
|
|
impl<T: RequiredVersion> RequiredVersion for StableVec<T> {
|
|
|
|
fn required_version(&self) -> ProtocolVersion {
|
|
|
|
self.values()
|
|
|
|
.map(RequiredVersion::required_version)
|
|
|
|
.max()
|
|
|
|
// return ProtocolVersion::V1, if the iterator is empty:
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:05:18 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_required_version_trait() {
|
|
|
|
struct Example;
|
|
|
|
|
|
|
|
impl RequiredVersion for Example {
|
|
|
|
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V3 }
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(Example.required_version(), ProtocolVersion::V3);
|
|
|
|
assert_eq!(Example.introduced_version(), ProtocolVersion::V3);
|
|
|
|
}
|
|
|
|
}
|