begin header holder implementation with lazy parser by getters, add request::Mode, add common header_bytes helper

This commit is contained in:
yggverse 2025-03-24 06:50:08 +02:00
parent a12a73d311
commit 7c518cecf6
13 changed files with 267 additions and 151 deletions

View file

@ -0,0 +1,43 @@
pub mod error;
pub use error::Error;
pub struct Header(Vec<u8>);
impl Header {
// Constructors
pub fn parse(buffer: &[u8]) -> Result<Self, Error> {
if !buffer.starts_with(super::CODE) {
return Err(Error::Code);
}
Ok(Self(
crate::client::connection::response::header_bytes(buffer)
.map_err(|e| Error::Header(e))?
.to_vec(),
))
}
// Getters
/// Parse content type for `Self`
pub fn mime(&self) -> Result<String, Error> {
glib::Regex::split_simple(
r"^\d{2}\s([^\/]+\/[^\s;]+)",
std::str::from_utf8(&self.0).map_err(|e| Error::Utf8Error(e))?,
glib::RegexCompileFlags::DEFAULT,
glib::RegexMatchFlags::DEFAULT,
)
.get(1)
.map(|s| s.trim())
.filter(|s| !s.is_empty())
.map_or(Err(Error::Mime), |s| Ok(s.to_lowercase()))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}