mod decoder;
mod encoder;
mod huffman;
mod static_table;
#[cfg(test)]
mod tests;
mod varint;
const INDEXED_FIELD_LINE: u8 = 0x80;
const INDEXED_STATIC_FLAG: u8 = 0x40;
const LITERAL_WITH_NAME_REF: u8 = 0x40;
const NAME_REF_STATIC_FLAG: u8 = 0x10;
const LITERAL_WITH_LITERAL_NAME: u8 = 0x20;
use crate::{Headers, Method, Status};
use fieldwork::Fieldwork;
use std::{
borrow::Cow,
fmt::{self, Display, Formatter},
};
#[derive(Debug, Default, Clone, PartialEq, Eq, Fieldwork)]
#[fieldwork(get, take, with, without, set, into)]
pub struct PseudoHeaders<'a> {
#[field(copy)]
method: Option<Method>,
#[field(copy)]
status: Option<Status>,
path: Option<Cow<'a, str>>,
scheme: Option<Cow<'a, str>>,
authority: Option<Cow<'a, str>>,
protocol: Option<Cow<'a, str>>,
}
impl Display for PseudoHeaders<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(method) = &self.method {
writeln!(f, ":method: {method}")?;
}
if let Some(status) = &self.status {
writeln!(f, ":status: {status}")?;
}
if let Some(path) = &self.path {
writeln!(f, ":path: {path}")?;
}
if let Some(scheme) = &self.scheme {
writeln!(f, ":scheme: {scheme}")?;
}
if let Some(authority) = &self.authority {
writeln!(f, ":authority: {authority}")?;
}
if let Some(protocol) = &self.protocol {
writeln!(f, ":protocol: {protocol}")?;
}
Ok(())
}
}
#[cfg(feature = "unstable")]
pub use huffman::HuffmanError;
#[derive(Debug, Clone, Fieldwork)]
#[fieldwork(get, get_mut, into_field)]
pub struct FieldSection<'a> {
pseudo_headers: PseudoHeaders<'a>,
headers: Cow<'a, Headers>,
}
impl<'a> FieldSection<'a> {
pub fn new(pseudo_headers: PseudoHeaders<'a>, headers: &'a Headers) -> Self {
Self {
pseudo_headers,
headers: Cow::Borrowed(headers),
}
}
#[cfg(any(feature = "unstable", test))]
pub fn into_parts(self) -> (PseudoHeaders<'a>, Headers) {
(self.pseudo_headers, self.headers.into_owned())
}
}
impl Display for FieldSection<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.pseudo_headers)?;
for (n, v) in &*self.headers {
for v in v {
writeln!(f, "{n}: {v}")?;
}
}
Ok(())
}
}