From 7989c5f6eb28b6363e69a388dc365c2b23bb026e Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Wed, 31 Mar 2021 12:29:24 +0200 Subject: [PATCH] Release v1.0.0 --- Cargo.toml | 4 ++-- src/date.rs | 36 +++++++++++++++++++++--------------- src/lib.rs | 7 ------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cc9fad4..1c9b767 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "httpdate" -version = "1.0.0-alpha" -authors = ["Pyfisch "] +version = "1.0.0" +authors = ["Pyfisch "] license = "MIT/Apache-2.0" description = "HTTP date parsing and formatting" keywords = ["http", "date", "time", "simple", "timestamp"] diff --git a/src/date.rs b/src/date.rs index 8501afa..2ab3598 100644 --- a/src/date.rs +++ b/src/date.rs @@ -1,5 +1,6 @@ use std::cmp; use std::fmt::{self, Display, Formatter}; +use std::num::ParseIntError; use std::str::{from_utf8_unchecked, FromStr}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; @@ -250,16 +251,19 @@ fn conv(s: &[u8]) -> &str { unsafe { from_utf8_unchecked(s) } } +fn cvt_err(_: ParseIntError) -> Error { + Error(()) +} fn parse_imf_fixdate(s: &[u8]) -> Result { // Example: `Sun, 06 Nov 1994 08:49:37 GMT` if s.len() != 29 || &s[25..] != b" GMT" || s[16] != b' ' || s[19] != b':' || s[22] != b':' { return Err(Error(())); } Ok(HttpDate { - sec: conv(&s[23..25]).parse()?, - min: conv(&s[20..22]).parse()?, - hour: conv(&s[17..19]).parse()?, - day: conv(&s[5..7]).parse()?, + sec: conv(&s[23..25]).parse().map_err(cvt_err)?, + min: conv(&s[20..22]).parse().map_err(cvt_err)?, + hour: conv(&s[17..19]).parse().map_err(cvt_err)?, + day: conv(&s[5..7]).parse().map_err(cvt_err)?, mon: match &s[7..12] { b" Jan " => 1, b" Feb " => 2, @@ -275,7 +279,7 @@ fn parse_imf_fixdate(s: &[u8]) -> Result { b" Dec " => 12, _ => return Err(Error(())), }, - year: conv(&s[12..16]).parse()?, + year: conv(&s[12..16]).parse().map_err(cvt_err)?, wday: match &s[..5] { b"Mon, " => 1, b"Tue, " => 2, @@ -312,17 +316,17 @@ fn parse_rfc850_date(s: &[u8]) -> Result { if s.len() != 22 || s[12] != b':' || s[15] != b':' || &s[18..22] != b" GMT" { return Err(Error(())); } - let mut year = conv(&s[7..9]).parse::()?; + let mut year = conv(&s[7..9]).parse::().map_err(cvt_err)?; if year < 70 { year += 2000; } else { year += 1900; } Ok(HttpDate { - sec: conv(&s[16..18]).parse()?, - min: conv(&s[13..15]).parse()?, - hour: conv(&s[10..12]).parse()?, - day: conv(&s[0..2]).parse()?, + sec: conv(&s[16..18]).parse().map_err(cvt_err)?, + min: conv(&s[13..15]).parse().map_err(cvt_err)?, + hour: conv(&s[10..12]).parse().map_err(cvt_err)?, + day: conv(&s[0..2]).parse().map_err(cvt_err)?, mon: match &s[2..7] { b"-Jan-" => 1, b"-Feb-" => 2, @@ -349,12 +353,14 @@ fn parse_asctime(s: &[u8]) -> Result { return Err(Error(())); } Ok(HttpDate { - sec: conv(&s[17..19]).parse()?, - min: conv(&s[14..16]).parse()?, - hour: conv(&s[11..13]).parse()?, + sec: conv(&s[17..19]).parse().map_err(cvt_err)?, + min: conv(&s[14..16]).parse().map_err(cvt_err)?, + hour: conv(&s[11..13]).parse().map_err(cvt_err)?, day: { let x = &s[8..10]; - conv(if x[0] == b' ' { &x[1..2] } else { x }).parse()? + conv(if x[0] == b' ' { &x[1..2] } else { x }) + .parse() + .map_err(cvt_err)? }, mon: match &s[4..8] { b"Jan " => 1, @@ -371,7 +377,7 @@ fn parse_asctime(s: &[u8]) -> Result { b"Dec " => 12, _ => return Err(Error(())), }, - year: conv(&s[20..24]).parse()?, + year: conv(&s[20..24]).parse().map_err(cvt_err)?, wday: match &s[0..4] { b"Mon " => 1, b"Tue " => 2, diff --git a/src/lib.rs b/src/lib.rs index 820e0ac..ea67ca4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,6 @@ use std::error; use std::fmt::{self, Display, Formatter}; use std::io; -use std::num::ParseIntError; use std::time::SystemTime; pub use date::HttpDate; @@ -39,12 +38,6 @@ impl Display for Error { } } -impl From for Error { - fn from(_: ParseIntError) -> Error { - Error(()) - } -} - impl From for io::Error { fn from(e: Error) -> io::Error { io::Error::new(io::ErrorKind::Other, e)