Skip to content

Commit 3707d9d

Browse files
committed
Remove unused test, use consts where appropriate and remove deadcode
1 parent f727041 commit 3707d9d

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/date.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ use std::fmt::{self, Display, Formatter};
33
use std::str::{from_utf8, FromStr};
44
use std::time::{Duration, SystemTime, UNIX_EPOCH};
55

6-
/// HTTP timestamp type.
7-
///
8-
/// Parse using `FromStr` impl.
6+
const IMF_FIXDATE_LENGTH: usize = 29;
7+
const RFC850_MAX_LENGTH: usize = 23;
8+
const ASCTIME_LENGTH: usize = 24;
9+
10+
const YEAR_9999_SECONDS: u64 = 253402300800;
11+
const SECONDS_IN_DAY: u64 = 86400;
12+
const SECONDS_IN_HOUR: u64 = 3600;
13+
914
/// Format using the `Display` trait.
1015
/// Convert timestamp into/from `SytemTime` to use.
1116
/// Supports comparsion and sorting.
12-
//#[derive(Copy, Clone, Debug, Eq, Ord)]
13-
#[derive(Copy, Clone, Debug)]
17+
#[derive(Copy, Clone, Debug, Eq, Ord)]
1418
pub struct HttpDate {
1519
/// 0...59
1620
second: u8,
@@ -62,7 +66,12 @@ impl HttpDate {
6266

6367
fn parse_imf_fixdate(s: &[u8]) -> Result<HttpDate, Exception> {
6468
// Example: `Sun, 06 Nov 1994 08:49:37 GMT`
65-
if s.len() != 29 || &s[25..] != b" GMT" || s[16] != b' ' || s[19] != b':' || s[22] != b':' {
69+
if s.len() != IMF_FIXDATE_LENGTH
70+
|| &s[25..] != b" GMT"
71+
|| s[16] != b' '
72+
|| s[19] != b':'
73+
|| s[22] != b':'
74+
{
6675
return Err("Date time not in imf fixdate format".into());
6776
}
6877
Ok(HttpDate {
@@ -101,7 +110,7 @@ fn parse_imf_fixdate(s: &[u8]) -> Result<HttpDate, Exception> {
101110

102111
fn parse_rfc850_date(s: &[u8]) -> Result<HttpDate, Exception> {
103112
// Example: `Sunday, 06-Nov-94 08:49:37 GMT`
104-
if s.len() < 23 {
113+
if s.len() < RFC850_MAX_LENGTH {
105114
return Err("Date time not in rfc850 format".into());
106115
}
107116

@@ -155,7 +164,8 @@ fn parse_rfc850_date(s: &[u8]) -> Result<HttpDate, Exception> {
155164

156165
fn parse_asctime(s: &[u8]) -> Result<HttpDate, Exception> {
157166
// Example: `Sun Nov 6 08:49:37 1994`
158-
if s.len() != 24 || s[10] != b' ' || s[13] != b':' || s[16] != b':' || s[19] != b' ' {
167+
if s.len() != ASCTIME_LENGTH || s[10] != b' ' || s[13] != b':' || s[16] != b':' || s[19] != b' '
168+
{
159169
return Err("Date time not in asctime format".into());
160170
}
161171
Ok(HttpDate {
@@ -202,7 +212,7 @@ impl From<SystemTime> for HttpDate {
202212
.expect("all times should be after the epoch");
203213
let secs_since_epoch = dur.as_secs();
204214

205-
if secs_since_epoch >= 253402300800 {
215+
if secs_since_epoch >= YEAR_9999_SECONDS {
206216
// year 9999
207217
panic!("date must be before year 9999");
208218
}
@@ -213,8 +223,8 @@ impl From<SystemTime> for HttpDate {
213223
const DAYS_PER_100Y: i64 = 365 * 100 + 24;
214224
const DAYS_PER_4Y: i64 = 365 * 4 + 1;
215225

216-
let days = (secs_since_epoch / 86400) as i64 - LEAPOCH;
217-
let secs_of_day = secs_since_epoch % 86400;
226+
let days = (secs_since_epoch / SECONDS_IN_DAY) as i64 - LEAPOCH;
227+
let secs_of_day = secs_since_epoch % SECONDS_IN_DAY;
218228

219229
let mut qc_cycles = days / DAYS_PER_400Y;
220230
let mut remdays = days % DAYS_PER_400Y;
@@ -268,8 +278,8 @@ impl From<SystemTime> for HttpDate {
268278

269279
HttpDate {
270280
second: (secs_of_day % 60) as u8,
271-
minute: ((secs_of_day % 3600) / 60) as u8,
272-
hour: (secs_of_day / 3600) as u8,
281+
minute: ((secs_of_day % SECONDS_IN_HOUR) / 60) as u8,
282+
hour: (secs_of_day / SECONDS_IN_HOUR) as u8,
273283
day: mday as u8,
274284
month: month as u8,
275285
year: year as u16,
@@ -306,8 +316,8 @@ impl From<HttpDate> for SystemTime {
306316
+ Duration::from_secs(
307317
http_date.second as u64
308318
+ http_date.minute as u64 * 60
309-
+ http_date.hour as u64 * 3600
310-
+ days * 86400,
319+
+ http_date.hour as u64 * SECONDS_IN_HOUR
320+
+ days * SECONDS_IN_DAY,
311321
)
312322
}
313323
}
@@ -403,10 +413,9 @@ fn is_leap_year(year: u16) -> bool {
403413

404414
#[cfg(test)]
405415
mod tests {
406-
use std::str;
407416
use std::time::{Duration, UNIX_EPOCH};
408417

409-
use super::{fmt_http_date, parse_http_date, HttpDate};
418+
use super::{fmt_http_date, parse_http_date, HttpDate, SECONDS_IN_DAY, SECONDS_IN_HOUR};
410419

411420
#[test]
412421
fn test_rfc_example() {
@@ -438,9 +447,9 @@ mod tests {
438447
fn test3() {
439448
let mut d = UNIX_EPOCH;
440449
assert_eq!(d, parse_http_date("Thu, 01 Jan 1970 00:00:00 GMT").unwrap());
441-
d += Duration::from_secs(3600);
450+
d += Duration::from_secs(SECONDS_IN_HOUR);
442451
assert_eq!(d, parse_http_date("Thu, 01 Jan 1970 01:00:00 GMT").unwrap());
443-
d += Duration::from_secs(86400);
452+
d += Duration::from_secs(SECONDS_IN_DAY);
444453
assert_eq!(d, parse_http_date("Fri, 02 Jan 1970 01:00:00 GMT").unwrap());
445454
d += Duration::from_secs(2592000);
446455
assert_eq!(d, parse_http_date("Sun, 01 Feb 1970 01:00:00 GMT").unwrap());
@@ -464,17 +473,6 @@ mod tests {
464473
assert_eq!(fmt_http_date(d), "Sun, 02 Oct 2016 14:44:11 GMT");
465474
}
466475

467-
#[allow(dead_code)]
468-
fn testcase(data: &[u8]) {
469-
if let Ok(s) = str::from_utf8(data) {
470-
println!("{:?}", s);
471-
if let Ok(d) = parse_http_date(s) {
472-
let o = fmt_http_date(d);
473-
assert!(!o.is_empty());
474-
}
475-
}
476-
}
477-
478476
#[test]
479477
fn size_of() {
480478
assert_eq!(::std::mem::size_of::<HttpDate>(), 8);

0 commit comments

Comments
 (0)