#[cfg(feature = "deflate")]
pub mod deflate;
use crate::base::{Data, Header};
use std::{borrow::Cow, fmt};
pub trait Extension: std::fmt::Debug {
fn is_enabled(&self) -> bool;
fn name(&self) -> &str;
fn params(&self) -> &[Param];
fn configure(&mut self, params: &[Param]) -> Result<(), crate::BoxError>;
fn encode(&mut self, h: &mut Header, d: &mut Option<Data>) -> Result<(), crate::BoxError>;
fn decode(&mut self, h: &mut Header, d: &mut Option<Data>) -> Result<(), crate::BoxError>;
fn reserved_bits(&self) -> (bool, bool, bool) {
(false, false, false)
}
}
impl<E: Extension + ?Sized> Extension for Box<E> {
fn is_enabled(&self) -> bool {
(**self).is_enabled()
}
fn name(&self) -> &str {
(**self).name()
}
fn params(&self) -> &[Param] {
(**self).params()
}
fn configure(&mut self, params: &[Param]) -> Result<(), crate::BoxError> {
(**self).configure(params)
}
fn encode(&mut self, h: &mut Header, d: &mut Option<Data>) -> Result<(), crate::BoxError> {
(**self).encode(h, d)
}
fn decode(&mut self, h: &mut Header, d: &mut Option<Data>) -> Result<(), crate::BoxError> {
(**self).decode(h, d)
}
fn reserved_bits(&self) -> (bool, bool, bool) {
(**self).reserved_bits()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Param<'a> {
name: Cow<'a, str>,
value: Option<Cow<'a, str>>
}
impl<'a> fmt::Display for Param<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(v) = &self.value {
write!(f, "{} = {}", self.name, v)
} else {
write!(f, "{}", self.name)
}
}
}
impl<'a> Param<'a> {
pub fn new(name: impl Into<Cow<'a, str>>) -> Self{
Param {
name: name.into(),
value: None
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn value(&self) -> Option<&str> {
self.value.as_ref().map(|v| v.as_ref())
}
pub fn set_value(&mut self, value: Option<impl Into<Cow<'a, str>>>) -> &mut Self {
self.value = value.map(Into::into);
self
}
pub fn acquire(self) -> Param<'static> {
Param {
name: Cow::Owned(self.name.into_owned()),
value: self.value.map(|v| Cow::Owned(v.into_owned()))
}
}
}