/
akmitrich
/
udith
Обзор
Документация
Войти
/
akmitrich
/
udith
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/message/generic.rs
95 строк
2 KB
akmitrich
refactor: Clean up 1.90 warnings
25 окт 2025, 17:45
25 окт 2025, 17:45
38d6cef
Код
Авторство
О чём код?
use nom::{IResult, Parser}; use crate::parse_utils::{ParseResult, equal, token}; use super::{header, start_line::StartLine}; #[derive(Debug)] pub struct Message { pub start_line: StartLine, pub headers: header::Map, pub body: Box<[u8]>, } impl Message { pub fn parse(src: &[u8]) -> IResult<&[u8], Self> { let (rest, (start_line, headers)) = (StartLine::parse, header::Map::parse).parse(src)?; let content_length = headers.content_length().unwrap_or(0); let body = rest[..content_length].to_vec().into_boxed_slice(); Ok(( &rest[content_length..], Self { start_line, headers, body, }, )) } } #[derive(Debug)] pub struct GenericParam { name: String, value: Option<GenValue>, } impl GenericParam { pub fn parse(src: &'_ [u8]) -> ParseResult<'_, Self> { let (remainder, name) = nom::combinator::map(token, |name| String::from_utf8(name.to_vec()).unwrap()) .parse(src)?; let (rest, maybe_value) = nom::multi::many_m_n(0, 1, (equal, GenValue::parse)).parse(remainder)?; Ok(( rest, Self { name, value: maybe_value.into_iter().next().map(|(_, value)| value), }, )) } } impl std::fmt::Display for GenericParam { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}{}", self.name, self.value .as_ref() .map(|v| format!("={}", v)) .unwrap_or_default() ) } } #[derive(Debug)] pub enum GenValue { Token(String), Host(String), Quoted(String), } impl GenValue { pub fn parse(src: &'_ [u8]) -> ParseResult<'_, Self> { nom::combinator::map(token, |x| { Self::Token(String::from_utf8(x.to_vec()).unwrap()) }) .parse(src) } } impl std::fmt::Display for GenValue { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}", match self { GenValue::Token(token) => token.to_string(), GenValue::Host(host) => host.to_string(), GenValue::Quoted(quoted) => format!("\"{}\"", quoted), } ) } }