Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export from_str_header and csv_heade macros #22

Merged
merged 10 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions crates/sip-types/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use headers::OneOrMore;
use name::Name;

mod error;
pub(crate) mod headers;
pub mod headers;
pub mod multiple;
pub(crate) mod name;

Expand Down Expand Up @@ -88,18 +88,19 @@ impl<H: HeaderParse> DecodeValues for H {
}
}

#[macro_export]
macro_rules! csv_header {
($(#[$meta:meta])* $struct_name:ident, $wrapping:ty, $header_name:expr) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $struct_name(pub $wrapping);

impl ConstNamed for $struct_name {
impl $crate::header::ConstNamed for $struct_name {
const NAME: Name = $header_name;
}

impl HeaderParse for $struct_name {
fn parse<'i>(ctx: ParseCtx, i: &'i str) -> Result<(&'i str, Self)> {
impl $crate::header::HeaderParse for $struct_name {
fn parse<'i>(ctx: $crate::parse::ParseCtx, i: &'i str) -> Result<(&'i str, Self), Error> {
if let Some(comma_idx) = i.find(',') {
Ok((
&i[comma_idx..],
Expand All @@ -111,48 +112,49 @@ macro_rules! csv_header {
}
}

impl ExtendValues for $struct_name {
fn extend_values(&self, _: PrintCtx<'_>, values: &mut OneOrMore) {
impl $crate::header::ExtendValues for $struct_name {
fn extend_values(&self, _: $crate::print::PrintCtx<'_>, values: &mut $crate::header::headers::OneOrMore) {
let value = match values {
OneOrMore::One(value) => value,
OneOrMore::More(values) => {
$crate::header::headers::OneOrMore::One(value) => value,
$crate::header::headers::OneOrMore::More(values) => {
values.last_mut().expect("empty OneOrMore::More variant")
}
};

*value = format!("{}, {}", value, self.0).into();
}

fn create_values(&self, _: PrintCtx<'_>) -> OneOrMore {
OneOrMore::One(self.0.to_string().into())
fn create_values(&self, _: $crate::print::PrintCtx<'_>) -> $crate::header::headers::OneOrMore {
$crate::header::headers::OneOrMore::One(self.0.to_string().into())
}
}
};
}

#[macro_export]
macro_rules! from_str_header {
($(#[$meta:meta])* $struct_name:ident, $header_name:expr, $from_str_ty:ty) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $struct_name(pub $from_str_ty);

impl ConstNamed for $struct_name {
impl $crate::header::ConstNamed for $struct_name {
const NAME: Name = $header_name;
}

impl HeaderParse for $struct_name {
fn parse<'i>(_: ParseCtx, i: &'i str) -> Result<(&'i str, Self)> {
impl $crate::header::HeaderParse for $struct_name {
fn parse<'i>(_: $crate::parse::ParseCtx, i: &'i str) -> Result<(&'i str, Self), Error> {
Ok(("", Self(i.trim().parse()?)))
}
}

impl ExtendValues for $struct_name {
fn extend_values(&self, ctx: PrintCtx<'_>, values: &mut OneOrMore) {
impl $crate::header::ExtendValues for $struct_name {
fn extend_values(&self, ctx: $crate::print::PrintCtx<'_>, values: &mut $crate::header::headers::OneOrMore) {
*values = self.create_values(ctx)
}

fn create_values(&self, _: PrintCtx<'_>) -> OneOrMore {
OneOrMore::One(self.0.to_string().into())
fn create_values(&self, _: $crate::print::PrintCtx<'_>) -> $crate::header::headers::OneOrMore {
$crate::header::headers::OneOrMore::One(self.0.to_string().into())
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/sip-types/src/header/typed/accept.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::header::headers::OneOrMore;
use crate::header::name::Name;
use crate::header::{ConstNamed, ExtendValues, HeaderParse};
use crate::parse::ParseCtx;
use crate::print::PrintCtx;
use anyhow::Result;
use anyhow::{Error, Result};
use bytesstr::BytesStr;

csv_header! {
Expand Down
6 changes: 1 addition & 5 deletions crates/sip-types/src/header/typed/allow.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::header::headers::OneOrMore;
use crate::header::name::Name;
use crate::header::{ConstNamed, ExtendValues, HeaderParse};
use crate::method::Method;
use crate::parse::ParseCtx;
use crate::print::PrintCtx;
use anyhow::Result;
use anyhow::{Error, Result};

csv_header! {
/// `Allow` header, contains only one method.
Expand Down
2 changes: 1 addition & 1 deletion crates/sip-types/src/header/typed/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::header::name::Name;
use crate::header::{ConstNamed, ExtendValues, HeaderParse};
use crate::parse::ParseCtx;
use crate::print::PrintCtx;
use anyhow::Result;
use anyhow::{Error, Result};
use bytesstr::BytesStr;

from_str_header! {
Expand Down
6 changes: 1 addition & 5 deletions crates/sip-types/src/header/typed/expires.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::header::headers::OneOrMore;
use crate::header::{ConstNamed, ExtendValues, HeaderParse};
use crate::parse::ParseCtx;
use crate::print::PrintCtx;
use crate::Name;
use anyhow::Result;
use anyhow::{Error, Result};

from_str_header! {
/// `Expires` header
Expand Down
6 changes: 1 addition & 5 deletions crates/sip-types/src/header/typed/extensions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::header::headers::OneOrMore;
use crate::header::name::Name;
use crate::header::{ConstNamed, ExtendValues, HeaderParse};
use crate::parse::ParseCtx;
use crate::print::PrintCtx;
use anyhow::Result;
use anyhow::{Error, Result};
use bytesstr::BytesStr;

csv_header! {
Expand Down
7 changes: 2 additions & 5 deletions crates/sip-types/src/header/typed/max_fwd.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::header::headers::OneOrMore;
use crate::header::{ConstNamed, ExtendValues, HeaderParse};
use crate::parse::ParseCtx;
use crate::print::PrintCtx;
use crate::Name;
use anyhow::Result;
use anyhow::{Error, Result};

from_str_header! {
/// `Max-Forwards` header
Expand All @@ -16,6 +12,7 @@ from_str_header! {
mod test {
use super::*;
use crate::parse::ParseCtx;
use crate::header::HeaderParse;
use bytesstr::BytesStr;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/sip-types/src/header/typed/prack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::method::Method;
use crate::parse::ParseCtx;
use crate::print::PrintCtx;
use crate::Name;
use anyhow::Result;
use anyhow::{Error, Result};
use internal::ws;
use nom::character::complete::digit1;
use nom::combinator::{map, map_res};
Expand Down
2 changes: 1 addition & 1 deletion crates/sip-types/src/header/typed/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::parse::ParseCtx;
use crate::print::{AppendCtx, Print, PrintCtx};
use crate::uri::params::{Params, CPS};
use crate::Name;
use anyhow::Result;
use anyhow::{Error, Result};
use internal::ws;
use nom::character::complete::alphanumeric1;
use nom::combinator::{map, map_res};
Expand Down
Loading