Skip to content

Commit

Permalink
Implement From for HeaderValue on integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb authored and Jarema committed Aug 14, 2023
1 parent bfb8ea8 commit 69ee1bf
Showing 1 changed file with 64 additions and 8 deletions.
72 changes: 64 additions & 8 deletions async-nats/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,51 @@ impl AsRef<str> for HeaderValue {
}
}

impl FromStr for HeaderValue {
type Err = ParseHeaderValueError;
impl From<i16> for HeaderValue {
fn from(v: i16) -> Self {
Self {
inner: v.to_string(),
}
}
}

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.contains(['\r', '\n']) {
return Err(ParseHeaderValueError);
impl From<i32> for HeaderValue {
fn from(v: i32) -> Self {
Self {
inner: v.to_string(),
}
}
}

Ok(HeaderValue {
inner: s.to_string(),
})
impl From<i64> for HeaderValue {
fn from(v: i64) -> Self {
Self {
inner: v.to_string(),
}
}
}

impl From<isize> for HeaderValue {
fn from(v: isize) -> Self {
Self {
inner: v.to_string(),
}
}
}

impl From<u16> for HeaderValue {
fn from(v: u16) -> Self {
Self {
inner: v.to_string(),
}
}
}

impl From<u32> for HeaderValue {
fn from(v: u32) -> Self {
Self {
inner: v.to_string(),
}
}
}

Expand All @@ -265,6 +299,28 @@ impl From<u64> for HeaderValue {
}
}

impl From<usize> for HeaderValue {
fn from(v: usize) -> Self {
Self {
inner: v.to_string(),
}
}
}

impl FromStr for HeaderValue {
type Err = ParseHeaderValueError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.contains(['\r', '\n']) {
return Err(ParseHeaderValueError);
}

Ok(HeaderValue {
inner: s.to_string(),
})
}
}

impl From<&str> for HeaderValue {
fn from(v: &str) -> Self {
Self {
Expand Down

0 comments on commit 69ee1bf

Please sign in to comment.