Skip to content

Commit

Permalink
Add more conversion functions to StatusCode
Browse files Browse the repository at this point in the history
  • Loading branch information
martinthomson committed Jun 25, 2024
1 parent a8eae8d commit 8a3c1fc
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions bhttp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,28 @@ impl TryFrom<u64> for StatusCode {
type Error = Error;

fn try_from(value: u64) -> Result<Self, Self::Error> {
let v = u16::try_from(value).map_err(|_| Error::InvalidStatus)?;
if matches!(v, 100..=599) {
Ok(Self(v))
Self::try_from(u16::try_from(value).map_err(|_| Error::InvalidStatus)?)
}
}

impl TryFrom<u16> for StatusCode {
type Error = Error;

fn try_from(value: u16) -> Result<Self, Self::Error> {
if matches!(value, 100..=599) {
Ok(Self(value))
} else {
Err(Error::InvalidStatus)
}
}
}

impl From<StatusCode> for u16 {
fn from(value: StatusCode) -> Self {
value.code()
}
}

pub trait ReadSeek: io::BufRead + io::Seek {}
impl<T> ReadSeek for io::Cursor<T> where T: AsRef<[u8]> {}
impl<T> ReadSeek for io::BufReader<T> where T: io::Read + io::Seek {}
Expand Down

0 comments on commit 8a3c1fc

Please sign in to comment.