#[cfg(feature = "deflate")]
pub mod deflate;
use crate::{base::Header, BoxedError, Storage};
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<(), BoxedError>;
fn encode(&mut self, header: &mut Header, data: &mut Storage) -> Result<(), BoxedError>;
fn decode(&mut self, header: &mut Header, data: &mut Vec<u8>) -> Result<(), BoxedError>;
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<(), BoxedError> {
(**self).configure(params)
}
fn encode(&mut self, header: &mut Header, data: &mut Storage) -> Result<(), BoxedError> {
(**self).encode(header, data)
}
fn decode(&mut self, header: &mut Header, data: &mut Vec<u8>) -> Result<(), BoxedError> {
(**self).decode(header, data)
}
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())) }
}
}