diff --git a/src/error.rs b/src/error.rs index 85fc169..01a0bfe 100644 --- a/src/error.rs +++ b/src/error.rs @@ -107,6 +107,13 @@ impl Error { }) } + pub(crate) fn missing_field(strct: D, field: T) -> Self { + Self::new(ErrorKind::Custom(format!( + "the field `{}` is missing for `{}`", + field, strct + ))) + } + pub(crate) fn unexpected_attribute(value: T) -> Self { Self::new(ErrorKind::UnexpectedAttribute { attribute: value.to_string(), diff --git a/src/traits.rs b/src/traits.rs index 4175588..dd459a3 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -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> { ::keys(self).first().copied() } /// Returns the number of keys. #[must_use] + #[inline] fn len(&self) -> usize { ::keys(self).len() } + /// Returns `true`, if the number of keys is zero. #[must_use] + #[inline] fn is_empty(&self) -> bool { ::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 RequiredVersion for Vec { } } +impl RequiredVersion for BTreeMap { + fn required_version(&self) -> ProtocolVersion { + self.values() + .map(RequiredVersion::required_version) + .max() + .unwrap_or_default() + } +} + impl RequiredVersion for Option { fn required_version(&self) -> ProtocolVersion { self.iter() @@ -105,6 +99,15 @@ impl RequiredVersion for Option { } } +impl RequiredVersion for HashMap { + fn required_version(&self) -> ProtocolVersion { + self.values() + .map(RequiredVersion::required_version) + .max() + .unwrap_or_default() + } +} + #[cfg(test)] mod tests { use super::*;