1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-12-23 04:26:27 +00:00

some minor fixes

This commit is contained in:
Luro02 2020-03-25 14:11:11 +01:00
parent 7a63c2dcf2
commit cc48478b05
No known key found for this signature in database
GPG key ID: B66FD4F74501A9CF
2 changed files with 32 additions and 22 deletions

View file

@ -107,6 +107,13 @@ impl Error {
})
}
pub(crate) fn missing_field<T: fmt::Display, D: fmt::Display>(strct: D, field: T) -> Self {
Self::new(ErrorKind::Custom(format!(
"the field `{}` is missing for `{}`",
field, strct
)))
}
pub(crate) fn unexpected_attribute<T: ToString>(value: T) -> Self {
Self::new(ErrorKind::UnexpectedAttribute {
attribute: value.to_string(),

View file

@ -39,40 +39,23 @@ pub trait Decryptable: private::Sealed {
/// 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]
#[inline]
fn first_key(&self) -> Option<&DecryptionKey> {
<Self as Decryptable>::keys(self).first().copied()
}
/// Returns the number of keys.
#[must_use]
#[inline]
fn len(&self) -> usize { <Self as Decryptable>::keys(self).len() }
/// Returns `true`, if the number of keys is zero.
#[must_use]
#[inline]
fn is_empty(&self) -> bool { <Self as Decryptable>::len(self) == 0 }
}
/// # Example
///
/// Implementing it:
///
/// ```
/// # use hls_m3u8::RequiredVersion;
/// use hls_m3u8::types::ProtocolVersion;
///
/// struct ExampleTag(u64);
///
/// impl RequiredVersion for ExampleTag {
/// fn required_version(&self) -> ProtocolVersion {
/// if self.0 == 5 {
/// ProtocolVersion::V4
/// } else {
/// ProtocolVersion::V1
/// }
/// }
/// }
/// assert_eq!(ExampleTag(5).required_version(), ProtocolVersion::V4);
/// assert_eq!(ExampleTag(2).required_version(), ProtocolVersion::V1);
/// ```
#[doc(hidden)]
pub trait RequiredVersion {
/// Returns the protocol compatibility version that this tag requires.
///
@ -80,9 +63,11 @@ pub trait RequiredVersion {
///
/// This is for the latest working [`ProtocolVersion`] and a client, that
/// only supports an older version would break.
#[must_use]
fn required_version(&self) -> ProtocolVersion;
/// The protocol version, in which the tag has been introduced.
#[must_use]
fn introduced_version(&self) -> ProtocolVersion { self.required_version() }
}
@ -96,6 +81,15 @@ impl<T: RequiredVersion> RequiredVersion for Vec<T> {
}
}
impl<K, V: RequiredVersion> RequiredVersion for BTreeMap<K, V> {
fn required_version(&self) -> ProtocolVersion {
self.values()
.map(RequiredVersion::required_version)
.max()
.unwrap_or_default()
}
}
impl<T: RequiredVersion> RequiredVersion for Option<T> {
fn required_version(&self) -> ProtocolVersion {
self.iter()
@ -105,6 +99,15 @@ impl<T: RequiredVersion> RequiredVersion for Option<T> {
}
}
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()
}
}
#[cfg(test)]
mod tests {
use super::*;