You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
738 lines
47 KiB
738 lines
47 KiB
7 years ago
|
From b7fb44f8d6c7ea9693df758d038328dce5dd019a Mon Sep 17 00:00:00 2001
|
||
|
From: Geoffroy Couprie <geo.couprie@gmail.com>
|
||
|
Date: Sun, 18 Feb 2018 12:39:18 +0100
|
||
|
Subject: [PATCH 2/2] use the CompleteByteSlice input type to fix Incomplete
|
||
|
usage
|
||
|
|
||
|
---
|
||
|
src/lib.rs | 6 +-
|
||
|
src/parsers.rs | 195 +++++++++++++++++++++++-----------------------
|
||
|
tests/lib.rs | 206 +++++++++++++++++++++++++------------------------
|
||
|
3 files changed, 209 insertions(+), 198 deletions(-)
|
||
|
|
||
|
diff --git a/src/lib.rs b/src/lib.rs
|
||
|
index 4d8c965..59be807 100644
|
||
|
--- a/src/lib.rs
|
||
|
+++ b/src/lib.rs
|
||
|
@@ -100,7 +100,7 @@ impl Default for Date {
|
||
|
/// let date = iso8601::date("2015-11-02").unwrap();
|
||
|
/// ```
|
||
|
pub fn date(string: &str) -> Result<Date, String> {
|
||
|
- if let Ok((_, parsed)) = parsers::parse_date(string.as_bytes()) {
|
||
|
+ if let Ok((_, parsed)) = parsers::parse_date(nom::types::CompleteByteSlice(string.as_bytes())) {
|
||
|
Ok(parsed)
|
||
|
} else {
|
||
|
Err(format!("Parser Error: {}", string))
|
||
|
@@ -123,7 +123,7 @@ pub fn date(string: &str) -> Result<Date, String> {
|
||
|
/// let time = iso8601::time("21:56:42").unwrap();
|
||
|
/// ```
|
||
|
pub fn time(string: &str) -> Result<Time, String> {
|
||
|
- if let Ok((_, parsed)) = parsers::parse_time(string.as_bytes()) {
|
||
|
+ if let Ok((_, parsed)) = parsers::parse_time(nom::types::CompleteByteSlice(string.as_bytes())) {
|
||
|
Ok(parsed)
|
||
|
} else {
|
||
|
Err(format!("Parser Error: {}", string))
|
||
|
@@ -143,7 +143,7 @@ pub fn time(string: &str) -> Result<Time, String> {
|
||
|
/// let dt = iso8601::datetime("2015-11-03T21:56").unwrap();
|
||
|
/// ```
|
||
|
pub fn datetime(string: &str) -> Result<DateTime, String> {
|
||
|
- if let Ok((_left_overs, parsed)) = parsers::parse_datetime(string.as_bytes()) {
|
||
|
+ if let Ok((_left_overs, parsed)) = parsers::parse_datetime(nom::types::CompleteByteSlice(string.as_bytes())) {
|
||
|
Ok(parsed)
|
||
|
} else {
|
||
|
Err(format!("Parser Error: {}", string))
|
||
|
diff --git a/src/parsers.rs b/src/parsers.rs
|
||
|
index ed87388..e9679d5 100644
|
||
|
--- a/src/parsers.rs
|
||
|
+++ b/src/parsers.rs
|
||
|
@@ -10,12 +10,14 @@
|
||
|
|
||
|
use helper::*;
|
||
|
use nom::{self, is_digit, digit};
|
||
|
+use nom::types::CompleteByteSlice;
|
||
|
use super::{Time, DateTime, Date};
|
||
|
use std::str::{self, FromStr};
|
||
|
|
||
|
macro_rules! empty_or(
|
||
|
- ($i:expr, $submac:ident!( $($args:tt)* )) => (
|
||
|
- if $i.len() == 0 {
|
||
|
+ ($i:expr, $submac:ident!( $($args:tt)* )) => ({
|
||
|
+ use nom::InputLength;
|
||
|
+ if $i.input_len() == 0 {
|
||
|
Ok(($i, None))
|
||
|
} else {
|
||
|
match $submac!($i, $($args)*) {
|
||
|
@@ -26,7 +28,7 @@ macro_rules! empty_or(
|
||
|
|
||
|
}
|
||
|
}
|
||
|
- );
|
||
|
+ });
|
||
|
);
|
||
|
|
||
|
macro_rules! check(
|
||
|
@@ -34,7 +36,7 @@ macro_rules! check(
|
||
|
|
||
|
{
|
||
|
let mut failed = false;
|
||
|
- for &idx in $input {
|
||
|
+ for &idx in $input.0 {
|
||
|
if !$submac!(idx, $($args)*) {
|
||
|
failed = true;
|
||
|
break;
|
||
|
@@ -43,7 +45,7 @@ macro_rules! check(
|
||
|
if failed {
|
||
|
Err(nom::Err::Error(error_position!($input, nom::ErrorKind::Custom(20u32))))
|
||
|
} else {
|
||
|
- Ok((&b""[..], $input))
|
||
|
+ Ok((CompleteByteSlice(&b""[..]), $input))
|
||
|
}
|
||
|
}
|
||
|
);
|
||
|
@@ -61,52 +63,52 @@ macro_rules! char_between(
|
||
|
);
|
||
|
);
|
||
|
|
||
|
-named!(take_4_digits, flat_map!(take!(4), check!(is_digit)));
|
||
|
+named!(take_4_digits<CompleteByteSlice,CompleteByteSlice>, flat_map!(take!(4), check!(is_digit)));
|
||
|
|
||
|
// year
|
||
|
-named!(year_prefix, alt!(tag!("+") | tag!("-")));
|
||
|
-named!(year <i32>, do_parse!(
|
||
|
+named!(year_prefix<CompleteByteSlice, CompleteByteSlice>, alt!(tag!("+") | tag!("-")));
|
||
|
+named!(year <CompleteByteSlice, i32>, do_parse!(
|
||
|
pref: opt!(year_prefix) >>
|
||
|
year: call!(take_4_digits) >>
|
||
|
(
|
||
|
match pref {
|
||
|
- Some(b"-") => -buf_to_i32(year),
|
||
|
- _ => buf_to_i32(year)
|
||
|
+ Some(CompleteByteSlice(b"-")) => -buf_to_i32(year.0),
|
||
|
+ _ => buf_to_i32(year.0)
|
||
|
}
|
||
|
)));
|
||
|
|
||
|
// MM
|
||
|
-named!(lower_month <u32>, do_parse!(tag!("0") >> s:char_between!('1', '9') >> (buf_to_u32(s))));
|
||
|
-named!(upper_month <u32>, do_parse!(tag!("1") >> s:char_between!('0', '2') >> (10+buf_to_u32(s))));
|
||
|
-named!(month <u32>, alt!(lower_month | upper_month));
|
||
|
+named!(lower_month <CompleteByteSlice,u32>, do_parse!(tag!("0") >> s:char_between!('1', '9') >> (buf_to_u32(s.0))));
|
||
|
+named!(upper_month <CompleteByteSlice,u32>, do_parse!(tag!("1") >> s:char_between!('0', '2') >> (10+buf_to_u32(s.0))));
|
||
|
+named!(month <CompleteByteSlice,u32>, alt!(lower_month | upper_month));
|
||
|
|
||
|
|
||
|
// DD
|
||
|
-named!(day_zero <u32>, do_parse!(tag!("0") >> s:char_between!('1', '9') >> (buf_to_u32(s))));
|
||
|
-named!(day_one <u32>, do_parse!(tag!("1") >> s:char_between!('0', '9') >> (10+buf_to_u32(s))));
|
||
|
-named!(day_two <u32>, do_parse!(tag!("2") >> s:char_between!('0', '9') >> (20+buf_to_u32(s))));
|
||
|
-named!(day_three <u32>, do_parse!(tag!("3") >> s:char_between!('0', '1') >> (30+buf_to_u32(s))));
|
||
|
-named!(day <u32>, alt!(day_zero | day_one | day_two | day_three));
|
||
|
+named!(day_zero <CompleteByteSlice,u32>, do_parse!(tag!("0") >> s:char_between!('1', '9') >> (buf_to_u32(s.0))));
|
||
|
+named!(day_one <CompleteByteSlice,u32>, do_parse!(tag!("1") >> s:char_between!('0', '9') >> (10+buf_to_u32(s.0))));
|
||
|
+named!(day_two <CompleteByteSlice,u32>, do_parse!(tag!("2") >> s:char_between!('0', '9') >> (20+buf_to_u32(s.0))));
|
||
|
+named!(day_three <CompleteByteSlice,u32>, do_parse!(tag!("3") >> s:char_between!('0', '1') >> (30+buf_to_u32(s.0))));
|
||
|
+named!(day <CompleteByteSlice,u32>, alt!(day_zero | day_one | day_two | day_three));
|
||
|
|
||
|
// WW
|
||
|
// reusing day_N parsers, sorry
|
||
|
-named!(week_three <u32>, do_parse!(tag!("3") >> s:char_between!('0', '9') >> (30+buf_to_u32(s))));
|
||
|
-named!(week_four <u32>, do_parse!(tag!("4") >> s:char_between!('0', '9') >> (40+buf_to_u32(s))));
|
||
|
-named!(week_five <u32>, do_parse!(tag!("5") >> s:char_between!('0', '3') >> (50+buf_to_u32(s))));
|
||
|
+named!(week_three <CompleteByteSlice,u32>, do_parse!(tag!("3") >> s:char_between!('0', '9') >> (30+buf_to_u32(s.0))));
|
||
|
+named!(week_four <CompleteByteSlice,u32>, do_parse!(tag!("4") >> s:char_between!('0', '9') >> (40+buf_to_u32(s.0))));
|
||
|
+named!(week_five <CompleteByteSlice,u32>, do_parse!(tag!("5") >> s:char_between!('0', '3') >> (50+buf_to_u32(s.0))));
|
||
|
|
||
|
-named!(week <u32>, alt!(day_zero | day_one | day_two | week_three| week_four | week_five ));
|
||
|
-named!(week_day <u32>, map!(char_between!('1', '7') , |s| buf_to_u32(s)));
|
||
|
+named!(week <CompleteByteSlice,u32>, alt!(day_zero | day_one | day_two | week_three| week_four | week_five ));
|
||
|
+named!(week_day <CompleteByteSlice,u32>, map!(char_between!('1', '7') , |s| buf_to_u32(s.0)));
|
||
|
|
||
|
// ordinal DDD
|
||
|
-named!(ord_day <u32>, do_parse!(
|
||
|
+named!(ord_day <CompleteByteSlice,u32>, do_parse!(
|
||
|
a:char_between!('0','3') >>
|
||
|
b:char_between!('0','9') >>
|
||
|
c:char_between!('0','9') >>
|
||
|
- ( buf_to_u32(a)*100 + buf_to_u32(b)*10 + buf_to_u32(c) )
|
||
|
+ ( buf_to_u32(a.0)*100 + buf_to_u32(b.0)*10 + buf_to_u32(c.0) )
|
||
|
));
|
||
|
|
||
|
// YYYY-MM-DD
|
||
|
-named!(pub ymd_date <Date>, do_parse!(
|
||
|
+named!(pub ymd_date <CompleteByteSlice,Date>, do_parse!(
|
||
|
y: year >>
|
||
|
opt!(tag!("-")) >>
|
||
|
m: month >>
|
||
|
@@ -116,7 +118,7 @@ named!(pub ymd_date <Date>, do_parse!(
|
||
|
));
|
||
|
|
||
|
// YYYY-MM-DD
|
||
|
-named!(pub ordinal_date <Date>, do_parse!(
|
||
|
+named!(pub ordinal_date <CompleteByteSlice,Date>, do_parse!(
|
||
|
y: year >>
|
||
|
opt!(tag!("-")) >>
|
||
|
d: ord_day >>
|
||
|
@@ -124,7 +126,7 @@ named!(pub ordinal_date <Date>, do_parse!(
|
||
|
));
|
||
|
|
||
|
// YYYY-"W"WW-D
|
||
|
-named!(pub iso_week_date <Date>, do_parse!(
|
||
|
+named!(pub iso_week_date <CompleteByteSlice,Date>, do_parse!(
|
||
|
y: year >>
|
||
|
opt!(tag!("-")) >>
|
||
|
tag!("W") >>
|
||
|
@@ -134,42 +136,42 @@ named!(pub iso_week_date <Date>, do_parse!(
|
||
|
( Date::Week{ year: y, ww: w, d: d } )
|
||
|
));
|
||
|
|
||
|
-named!(pub parse_date <Date>, alt!( ymd_date | iso_week_date | ordinal_date ) );
|
||
|
+named!(pub parse_date <CompleteByteSlice,Date>, alt!( ymd_date | iso_week_date | ordinal_date ) );
|
||
|
|
||
|
// TIME
|
||
|
|
||
|
// HH
|
||
|
-named!(lower_hour <u32>, do_parse!(f:char_between!('0','1') >>
|
||
|
+named!(lower_hour <CompleteByteSlice,u32>, do_parse!(f:char_between!('0','1') >>
|
||
|
s:char_between!('0','9') >>
|
||
|
- ( buf_to_u32(f)*10 + buf_to_u32(s) )));
|
||
|
-named!(upper_hour <u32>, do_parse!(tag!("2") >>
|
||
|
+ ( buf_to_u32(f.0)*10 + buf_to_u32(s.0) )));
|
||
|
+named!(upper_hour <CompleteByteSlice,u32>, do_parse!(tag!("2") >>
|
||
|
s:char_between!('0','4') >>
|
||
|
- (20+buf_to_u32(s))));
|
||
|
-named!(hour <u32>, alt!(lower_hour | upper_hour));
|
||
|
+ (20+buf_to_u32(s.0))));
|
||
|
+named!(hour <CompleteByteSlice,u32>, alt!(lower_hour | upper_hour));
|
||
|
|
||
|
// MM
|
||
|
-named!(below_sixty <u32>, do_parse!(f:char_between!('0','5') >>
|
||
|
+named!(below_sixty <CompleteByteSlice,u32>, do_parse!(f:char_between!('0','5') >>
|
||
|
s:char_between!('0','9') >>
|
||
|
- ( buf_to_u32(f)*10 + buf_to_u32(s) ) ));
|
||
|
-named!(upto_sixty <u32>, alt!(below_sixty | map!(tag!("60"), |_| 60)));
|
||
|
+ ( buf_to_u32(f.0)*10 + buf_to_u32(s.0) ) ));
|
||
|
+named!(upto_sixty <CompleteByteSlice,u32>, alt!(below_sixty | map!(tag!("60"), |_| 60)));
|
||
|
|
||
|
-fn into_fraction_string(digits: &[u8]) -> Result<f32, ::std::num::ParseFloatError> {
|
||
|
+fn into_fraction_string(digits: CompleteByteSlice) -> Result<f32, ::std::num::ParseFloatError> {
|
||
|
let mut s = String::from("0.");
|
||
|
- s += str::from_utf8(digits).unwrap();
|
||
|
+ s += str::from_utf8(digits.0).unwrap();
|
||
|
FromStr::from_str(&s)
|
||
|
}
|
||
|
|
||
|
|
||
|
-named!(minute <u32>, call!(below_sixty));
|
||
|
-named!(second <u32>, call!(upto_sixty));
|
||
|
-named!(fractions <f32>, map_res!(digit, into_fraction_string));
|
||
|
+named!(minute <CompleteByteSlice,u32>, call!(below_sixty));
|
||
|
+named!(second <CompleteByteSlice,u32>, call!(upto_sixty));
|
||
|
+named!(fractions <CompleteByteSlice,f32>, map_res!(digit, into_fraction_string));
|
||
|
|
||
|
fn millisecond(fraction: f32) -> u32 {
|
||
|
(1000.0 * fraction) as u32
|
||
|
}
|
||
|
|
||
|
// HH:MM:[SS][.(m*)][(Z|+...|-...)]
|
||
|
-named!(pub parse_time <Time>, do_parse!(
|
||
|
+named!(pub parse_time <CompleteByteSlice, Time>, do_parse!(
|
||
|
h: hour >>
|
||
|
opt!(tag!(":")) >>
|
||
|
m: minute >>
|
||
|
@@ -188,13 +190,13 @@ named!(pub parse_time <Time>, do_parse!(
|
||
|
)
|
||
|
));
|
||
|
|
||
|
-named!(sign <i32>, alt!(
|
||
|
+named!(sign <CompleteByteSlice,i32>, alt!(
|
||
|
tag!("-") => { |_| -1 } |
|
||
|
tag!("+") => { |_| 1 }
|
||
|
)
|
||
|
);
|
||
|
|
||
|
-named!(timezone_hour <(i32,i32)>, do_parse!(
|
||
|
+named!(timezone_hour <CompleteByteSlice,(i32,i32)>, do_parse!(
|
||
|
s: sign >>
|
||
|
h: hour >>
|
||
|
m: empty_or!(
|
||
|
@@ -203,10 +205,10 @@ named!(timezone_hour <(i32,i32)>, do_parse!(
|
||
|
( (s * (h as i32) , s * (m.unwrap_or(0) as i32)) )
|
||
|
));
|
||
|
|
||
|
-named!(timezone_utc <(i32,i32)>, map!(tag!("Z"), |_| (0,0)));
|
||
|
+named!(timezone_utc <CompleteByteSlice,(i32,i32)>, map!(tag!("Z"), |_| (0,0)));
|
||
|
|
||
|
// Full ISO8601
|
||
|
-named!(pub parse_datetime <DateTime>, do_parse!(
|
||
|
+named!(pub parse_datetime <CompleteByteSlice,DateTime>, do_parse!(
|
||
|
d: parse_date >>
|
||
|
tag!("T") >>
|
||
|
t: parse_time >>
|
||
|
@@ -218,83 +220,86 @@ named!(pub parse_datetime <DateTime>, do_parse!(
|
||
|
)
|
||
|
));
|
||
|
|
||
|
+
|
||
|
#[cfg(test)]
|
||
|
mod tests{
|
||
|
|
||
|
use super::{year, month, day};
|
||
|
use super::{hour, minute, second};
|
||
|
|
||
|
+ use nom::types::CompleteByteSlice;
|
||
|
+
|
||
|
#[test]
|
||
|
fn test_year() {
|
||
|
- assert_eq!(Ok((&[][..], 2015)), year(b"2015"));
|
||
|
- assert_eq!(Ok((&[][..], -0333)), year(b"-0333"));
|
||
|
- assert_eq!(Ok((&b"-"[..], 2015)), year(b"2015-"));
|
||
|
- assert!(year(b"abcd").is_err());
|
||
|
- assert!(year(b"2a03").is_err());
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 2015)), year(CompleteByteSlice(b"2015")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), -0333)), year(CompleteByteSlice(b"-0333")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&b"-"[..]), 2015)), year(CompleteByteSlice(b"2015-")));
|
||
|
+ assert!(year(CompleteByteSlice(b"abcd")).is_err());
|
||
|
+ assert!(year(CompleteByteSlice(b"2a03")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_month() {
|
||
|
- assert_eq!(Ok((&[][..], 1)), month(b"01"));
|
||
|
- assert_eq!(Ok((&[][..], 6)), month(b"06"));
|
||
|
- assert_eq!(Ok((&[][..], 12)), month(b"12"));
|
||
|
- assert_eq!(Ok((&b"-"[..], 12)), month(b"12-"));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 1)), month(CompleteByteSlice(b"01")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 6)), month(CompleteByteSlice(b"06")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 12)), month(CompleteByteSlice(b"12")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&b"-"[..]), 12)), month(CompleteByteSlice(b"12-")));
|
||
|
|
||
|
- assert!(month(b"13").is_err());
|
||
|
- assert!(month(b"00").is_err());
|
||
|
+ assert!(month(CompleteByteSlice(b"13")).is_err());
|
||
|
+ assert!(month(CompleteByteSlice(b"00")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_day() {
|
||
|
- assert_eq!(Ok((&[][..], 1)), day(b"01"));
|
||
|
- assert_eq!(Ok((&[][..], 12)), day(b"12"));
|
||
|
- assert_eq!(Ok((&[][..], 20)), day(b"20"));
|
||
|
- assert_eq!(Ok((&[][..], 28)), day(b"28"));
|
||
|
- assert_eq!(Ok((&[][..], 30)), day(b"30"));
|
||
|
- assert_eq!(Ok((&[][..], 31)), day(b"31"));
|
||
|
- assert_eq!(Ok((&b"-"[..], 31)), day(b"31-"));
|
||
|
-
|
||
|
- assert!(day(b"00").is_err());
|
||
|
- assert!(day(b"32").is_err());
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 1)), day(CompleteByteSlice(b"01")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 12)), day(CompleteByteSlice(b"12")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 20)), day(CompleteByteSlice(b"20")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 28)), day(CompleteByteSlice(b"28")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 30)), day(CompleteByteSlice(b"30")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 31)), day(CompleteByteSlice(b"31")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&b"-"[..]), 31)), day(CompleteByteSlice(b"31-")));
|
||
|
+
|
||
|
+ assert!(day(CompleteByteSlice(b"00")).is_err());
|
||
|
+ assert!(day(CompleteByteSlice(b"32")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_hour() {
|
||
|
- assert_eq!(Ok((&[][..], 0)), hour(b"00"));
|
||
|
- assert_eq!(Ok((&[][..], 1)), hour(b"01"));
|
||
|
- assert_eq!(Ok((&[][..], 6)), hour(b"06"));
|
||
|
- assert_eq!(Ok((&[][..], 12)), hour(b"12"));
|
||
|
- assert_eq!(Ok((&[][..], 13)), hour(b"13"));
|
||
|
- assert_eq!(Ok((&[][..], 20)), hour(b"20"));
|
||
|
- assert_eq!(Ok((&[][..], 24)), hour(b"24"));
|
||
|
-
|
||
|
- assert!(hour(b"25").is_err());
|
||
|
- assert!(hour(b"30").is_err());
|
||
|
- assert!(hour(b"ab").is_err());
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 0)), hour(CompleteByteSlice(b"00")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 1)), hour(CompleteByteSlice(b"01")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 6)), hour(CompleteByteSlice(b"06")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 12)), hour(CompleteByteSlice(b"12")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 13)), hour(CompleteByteSlice(b"13")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 20)), hour(CompleteByteSlice(b"20")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 24)), hour(CompleteByteSlice(b"24")));
|
||
|
+
|
||
|
+ assert!(hour(CompleteByteSlice(b"25")).is_err());
|
||
|
+ assert!(hour(CompleteByteSlice(b"30")).is_err());
|
||
|
+ assert!(hour(CompleteByteSlice(b"ab")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_minute() {
|
||
|
- assert_eq!(Ok((&[][..], 0)), minute(b"00"));
|
||
|
- assert_eq!(Ok((&[][..], 1)), minute(b"01"));
|
||
|
- assert_eq!(Ok((&[][..], 30)), minute(b"30"));
|
||
|
- assert_eq!(Ok((&[][..], 59)), minute(b"59"));
|
||
|
-
|
||
|
- assert!(minute(b"60").is_err());
|
||
|
- assert!(minute(b"61").is_err());
|
||
|
- assert!(minute(b"ab").is_err());
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 0)), minute(CompleteByteSlice(b"00")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 1)), minute(CompleteByteSlice(b"01")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 30)), minute(CompleteByteSlice(b"30")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 59)), minute(CompleteByteSlice(b"59")));
|
||
|
+
|
||
|
+ assert!(minute(CompleteByteSlice(b"60")).is_err());
|
||
|
+ assert!(minute(CompleteByteSlice(b"61")).is_err());
|
||
|
+ assert!(minute(CompleteByteSlice(b"ab")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_second() {
|
||
|
- assert_eq!(Ok((&[][..], 0)), second(b"00"));
|
||
|
- assert_eq!(Ok((&[][..], 1)), second(b"01"));
|
||
|
- assert_eq!(Ok((&[][..], 30)), second(b"30"));
|
||
|
- assert_eq!(Ok((&[][..], 59)), second(b"59"));
|
||
|
- assert_eq!(Ok((&[][..], 60)), second(b"60"));
|
||
|
-
|
||
|
- assert!(second(b"61").is_err());
|
||
|
- assert!(second(b"ab").is_err());
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 0)), second(CompleteByteSlice(b"00")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 1)), second(CompleteByteSlice(b"01")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 30)), second(CompleteByteSlice(b"30")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 59)), second(CompleteByteSlice(b"59")));
|
||
|
+ assert_eq!(Ok((CompleteByteSlice(&[][..]), 60)), second(CompleteByteSlice(b"60")));
|
||
|
+
|
||
|
+ assert!(second(CompleteByteSlice(b"61")).is_err());
|
||
|
+ assert!(second(CompleteByteSlice(b"ab")).is_err());
|
||
|
}
|
||
|
|
||
|
}
|
||
|
diff --git a/tests/lib.rs b/tests/lib.rs
|
||
|
index e7f4155..dfc73e5 100644
|
||
|
--- a/tests/lib.rs
|
||
|
+++ b/tests/lib.rs
|
||
|
@@ -3,53 +3,59 @@ extern crate nom;
|
||
|
|
||
|
use iso8601::*;
|
||
|
use iso8601::parsers::*;
|
||
|
+use nom::types::CompleteByteSlice;
|
||
|
|
||
|
+// alias the type for readability
|
||
|
+type Input<'a> = CompleteByteSlice<'a>;
|
||
|
+pub fn Input<'a>(input:&'a[u8]) -> Input<'a> {
|
||
|
+ CompleteByteSlice(input)
|
||
|
+}
|
||
|
|
||
|
#[test]
|
||
|
fn test_date() {
|
||
|
- assert_eq!(Ok((&[][..], Date::YMD { year: 2015, month: 6, day: 26, })), parse_date(b"2015-06-26"));
|
||
|
- assert_eq!(Ok((&[][..], Date::YMD { year: -333, month: 7, day: 11, })), parse_date(b"-0333-07-11"));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::YMD { year: 2015, month: 6, day: 26, })), parse_date(Input(b"2015-06-26")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::YMD { year: -333, month: 7, day: 11, })), parse_date(Input(b"-0333-07-11")));
|
||
|
|
||
|
- assert!(parse_date(b"201").unwrap_err().is_incomplete());
|
||
|
- assert!(parse_date(b"2015p00p00").is_err());
|
||
|
- assert!(parse_date(b"pppp").is_err());
|
||
|
+ assert!(parse_date(Input(b"201")).is_err());
|
||
|
+ assert!(parse_date(Input(b"2015p00p00")).is_err());
|
||
|
+ assert!(parse_date(Input(b"pppp")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_millisecond() {
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 0, millisecond: 100, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:00.1"));
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 0, millisecond: 120, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:00.12"));
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 0, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:00.123"));
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 0, millisecond: 432, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:00.4321"));
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 0, millisecond: 432, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43.4321"));
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 11, millisecond: 432, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:11.4321"));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 0, millisecond: 100, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:00.1")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 0, millisecond: 120, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:00.12")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 0, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:00.123")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 0, millisecond: 432, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:00.4321")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 0, millisecond: 432, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43.4321")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 11, millisecond: 432, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:11.4321")));
|
||
|
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 0, millisecond: 100, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:00,1"));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 0, millisecond: 100, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:00,1")));
|
||
|
|
||
|
assert_eq!(
|
||
|
- Ok((&[][..], Time{ hour: 04, minute:05, second:06, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})),
|
||
|
- parse_time(b"04:05:06.12345")
|
||
|
+ Ok((Input(&[][..]), Time{ hour: 04, minute:05, second:06, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})),
|
||
|
+ parse_time(Input(b"04:05:06.12345"))
|
||
|
);
|
||
|
|
||
|
assert_eq!(
|
||
|
- Ok((&[][..], DateTime{ date: Date::Week { year: 2001, ww:05, d:6}, time: Time{ hour: 04, minute:05, second:06, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0}})),
|
||
|
- parse_datetime(b"2001-W05-6T04:05:06.12345Z")
|
||
|
+ Ok((Input(&[][..]), DateTime{ date: Date::Week { year: 2001, ww:05, d:6}, time: Time{ hour: 04, minute:05, second:06, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0}})),
|
||
|
+ parse_datetime(Input(b"2001-W05-6T04:05:06.12345Z"))
|
||
|
);
|
||
|
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 16, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:16.123"));
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 16, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:16.123+00:00"));
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 16, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(b"16:43:16.123-00:00"));
|
||
|
- assert_eq!(Ok((&[][..], Time{ hour: 16, minute: 43, second: 16, millisecond: 123, tz_offset_hours: 5, tz_offset_minutes: 0})), parse_time(b"16:43:16.123+05:00"));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 16, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:16.123")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 16, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:16.123+00:00")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 16, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0})), parse_time(Input(b"16:43:16.123-00:00")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time{ hour: 16, minute: 43, second: 16, millisecond: 123, tz_offset_hours: 5, tz_offset_minutes: 0})), parse_time(Input(b"16:43:16.123+05:00")));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_time() {
|
||
|
- assert_eq!(parse_time(b"16:43:16"), Ok((&[][..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })) );
|
||
|
- assert_eq!(parse_time(b"16:43"), Ok((&[][..], Time { hour: 16, minute: 43, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })) );
|
||
|
+ assert_eq!(parse_time(Input(b"16:43:16")), Ok((Input(&[][..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })) );
|
||
|
+ assert_eq!(parse_time(Input(b"16:43")), Ok((Input(&[][..]), Time { hour: 16, minute: 43, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })) );
|
||
|
|
||
|
- assert!(parse_time(b"20:").unwrap_err().is_incomplete());
|
||
|
- assert!(parse_time(b"20p42p16").is_err());
|
||
|
- assert!(parse_time(b"pppp").is_err());
|
||
|
+ assert!(parse_time(Input(b"20:")).is_err());
|
||
|
+ assert!(parse_time(Input(b"20p42p16")).is_err());
|
||
|
+ assert!(parse_time(Input(b"pppp")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
@@ -76,54 +82,54 @@ fn test_time_set_tz() {
|
||
|
|
||
|
#[test]
|
||
|
fn short_time1() {
|
||
|
- assert_eq!(parse_time(b"1648"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"1648")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_time2() {
|
||
|
- assert_eq!(parse_time(b"16:48"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"16:48")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_time3() {
|
||
|
- assert_eq!(parse_time(b"16:48Z"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"16:48Z")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_time4() {
|
||
|
- assert_eq!(parse_time(b"164800"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"164800")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_time5() {
|
||
|
- assert_eq!(parse_time(b"164800.1"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 100, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"164800.1")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 100, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_time6() {
|
||
|
- assert_eq!(parse_time(b"164800.1Z"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 100, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"164800.1Z")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 100, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_time7() {
|
||
|
- assert_eq!(parse_time(b"16:48:00"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"16:48:00")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn short_twtz1() {
|
||
|
- assert_eq!(parse_time(b"1648Z"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"1648Z")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_twtz2() {
|
||
|
- assert_eq!(parse_time(b"16:48Z"), Ok((&[][..], Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
+ assert_eq!(parse_time(Input(b"16:48Z")), Ok((Input(&[][..]), Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn short_dtim1() {
|
||
|
- assert_eq!(parse_datetime(b"20070831T1648"), Ok((&[][..], DateTime { date: Date::YMD { year: 2007, month: 08, day: 31, }, time: Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, } })));
|
||
|
+ assert_eq!(parse_datetime(Input(b"20070831T1648")), Ok((Input(&[][..]), DateTime { date: Date::YMD { year: 2007, month: 08, day: 31, }, time: Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, } })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_dtim2() {
|
||
|
- assert_eq!(parse_datetime(b"20070831T1648Z"), Ok((&[][..], DateTime { date: Date::YMD { year: 2007, month: 08, day: 31, }, time: Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, }, })));
|
||
|
+ assert_eq!(parse_datetime(Input(b"20070831T1648Z")), Ok((Input(&[][..]), DateTime { date: Date::YMD { year: 2007, month: 08, day: 31, }, time: Time { hour: 16, minute: 48, second: 0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, }, })));
|
||
|
}
|
||
|
#[test]
|
||
|
fn short_dtim3() {
|
||
|
- assert_eq!(parse_datetime(b"2008-12-24T18:21Z"),
|
||
|
- Ok((&[][..],
|
||
|
+ assert_eq!(parse_datetime(Input(b"2008-12-24T18:21Z")),
|
||
|
+ Ok((Input(&[][..]),
|
||
|
DateTime {
|
||
|
date: Date::YMD {
|
||
|
year: 2008,
|
||
|
@@ -143,80 +149,80 @@ fn short_dtim3() {
|
||
|
|
||
|
#[test]
|
||
|
fn test_time_with_timezone() {
|
||
|
- assert_eq!(Ok((&[][..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(b"16:43:16"));
|
||
|
- assert_eq!(Ok((&[][..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(b"16:43:16Z"));
|
||
|
- assert_eq!(Ok((&[][..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(b"16:43:16+00:00"));
|
||
|
- assert_eq!(Ok((&[][..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(b"16:43:16-00:00"));
|
||
|
- assert_eq!(Ok((&[][..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 5, tz_offset_minutes: 0, })), parse_time(b"16:43:16+05:00"));
|
||
|
- assert_eq!(Ok((&b"+"[..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(b"16:43:16+"));
|
||
|
- assert_eq!(Ok((&b"+0"[..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(b"16:43:16+0"));
|
||
|
- assert_eq!(Ok((&b"+05:"[..], Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(b"16:43:16+05:"));
|
||
|
-
|
||
|
- assert!(parse_time(b"20:").unwrap_err().is_incomplete());
|
||
|
- assert!(parse_time(b"20p42p16").is_err());
|
||
|
- assert!(parse_time(b"pppp").is_err());
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(Input(b"16:43:16")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(Input(b"16:43:16Z")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(Input(b"16:43:16+00:00")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(Input(b"16:43:16-00:00")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 5, tz_offset_minutes: 0, })), parse_time(Input(b"16:43:16+05:00")));
|
||
|
+ assert_eq!(Ok((Input(&b"+"[..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(Input(b"16:43:16+")));
|
||
|
+ assert_eq!(Ok((Input(&b"+0"[..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(Input(b"16:43:16+0")));
|
||
|
+ assert_eq!(Ok((Input(&b"+05:"[..]), Time { hour: 16, minute: 43, second: 16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0, })), parse_time(Input(b"16:43:16+05:")));
|
||
|
+
|
||
|
+ assert!(parse_time(Input(b"20:")).unwrap_err().is_incomplete());
|
||
|
+ assert!(parse_time(Input(b"20p42p16")).is_err());
|
||
|
+ assert!(parse_time(Input(b"pppp")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_iso_week_date() {
|
||
|
- assert_eq!(Ok((&[][..], Date::Week { year: 2015, ww: 5, d: 7, })), iso_week_date(b"2015-W05-7"));
|
||
|
- assert_eq!(Ok((&[][..], Date::Week { year: 2015, ww: 6, d: 6, })), iso_week_date(b"2015-W06-6"));
|
||
|
- assert_eq!(Ok((&[][..], Date::Week { year: 2015, ww: 6, d: 6, })), iso_week_date(b"2015-W066"));
|
||
|
- assert_eq!(Ok((&[][..], Date::Week { year: 2015, ww: 6, d: 6, })), iso_week_date(b"2015W066"));
|
||
|
- assert_eq!(Ok((&[][..], Date::Week { year: 2015, ww: 43, d: 6, })), iso_week_date(b"2015-W43-6"));
|
||
|
-
|
||
|
- assert!(iso_week_date(b"2015-W06-8").is_err());
|
||
|
- assert!(iso_week_date(b"2015-W068").is_err());
|
||
|
- assert!(iso_week_date(b"2015-W06-0").is_err());
|
||
|
- assert!(iso_week_date(b"2015-W00-2").is_err());
|
||
|
- assert!(iso_week_date(b"2015-W54-2").is_err());
|
||
|
- assert!(iso_week_date(b"2015-W542").is_err());
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Week { year: 2015, ww: 5, d: 7, })), iso_week_date(Input(b"2015-W05-7")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Week { year: 2015, ww: 6, d: 6, })), iso_week_date(Input(b"2015-W06-6")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Week { year: 2015, ww: 6, d: 6, })), iso_week_date(Input(b"2015-W066")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Week { year: 2015, ww: 6, d: 6, })), iso_week_date(Input(b"2015W066")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Week { year: 2015, ww: 43, d: 6, })), iso_week_date(Input(b"2015-W43-6")));
|
||
|
+
|
||
|
+ assert!(iso_week_date(Input(b"2015-W06-8")).is_err());
|
||
|
+ assert!(iso_week_date(Input(b"2015-W068")).is_err());
|
||
|
+ assert!(iso_week_date(Input(b"2015-W06-0")).is_err());
|
||
|
+ assert!(iso_week_date(Input(b"2015-W00-2")).is_err());
|
||
|
+ assert!(iso_week_date(Input(b"2015-W54-2")).is_err());
|
||
|
+ assert!(iso_week_date(Input(b"2015-W542")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_ordinal_date() {
|
||
|
- assert_eq!(Ok((&[][..], Date::Ordinal { year: 2015, ddd: 57, })), ordinal_date(b"2015-057"));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Ordinal { year: 2015, ddd: 57, })), ordinal_date(Input(b"2015-057")));
|
||
|
|
||
|
// not valid, but this should be tested elsewhere
|
||
|
- assert_eq!(Ok((&[][..], Date::Ordinal { year: 2015, ddd: 358, })), ordinal_date(b"2015-358"));
|
||
|
- assert_eq!(Ok((&[][..], Date::Ordinal { year: 2015, ddd: 399, })), ordinal_date(b"2015-399"));
|
||
|
- assert_eq!(Ok((&[][..], Date::Ordinal { year: 2015, ddd: 000, })), ordinal_date(b"2015-000"));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Ordinal { year: 2015, ddd: 358, })), ordinal_date(Input(b"2015-358")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Ordinal { year: 2015, ddd: 399, })), ordinal_date(Input(b"2015-399")));
|
||
|
+ assert_eq!(Ok((Input(&[][..]), Date::Ordinal { year: 2015, ddd: 000, })), ordinal_date(Input(b"2015-000")));
|
||
|
|
||
|
// not valid here either
|
||
|
- assert!(ordinal_date(b"2015-400").is_err());
|
||
|
+ assert!(ordinal_date(Input(b"2015-400")).is_err());
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn format_equivalence() {
|
||
|
- assert_eq!(parse_datetime(b"2001-02-03T04:05:06+07:00"), parse_datetime(b"20010203T040506+0700"));
|
||
|
- assert_eq!(parse_datetime(b"2001-02-03T04:05:06+07:00"), parse_datetime(b"20010203T04:05:06+0700"));
|
||
|
- assert_eq!(parse_datetime(b"2001-02-03T04:05:00+07:00"), parse_datetime(b"20010203T0405+0700"));
|
||
|
- assert_eq!(parse_datetime(b"20010203T0405+0700"), parse_datetime(b"2001-02-03T0405+0700"));
|
||
|
- assert_eq!(parse_datetime(b"20010203T040506+0700"), parse_datetime(b"2001-02-03T040506+0700"));
|
||
|
- assert_eq!(parse_datetime(b"20010203T040506+0000"), parse_datetime(b"20010203T040506Z"));
|
||
|
- assert_eq!(parse_datetime(b"2015W056T04:05:06+07:00"), parse_datetime(b"2015-W05-6T04:05:06+07:00"));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2001-02-03T04:05:06+07:00")), parse_datetime(Input(b"20010203T040506+0700")));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2001-02-03T04:05:06+07:00")), parse_datetime(Input(b"20010203T04:05:06+0700")));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2001-02-03T04:05:00+07:00")), parse_datetime(Input(b"20010203T0405+0700")));
|
||
|
+ assert_eq!(parse_datetime(Input(b"20010203T0405+0700")), parse_datetime(Input(b"2001-02-03T0405+0700")));
|
||
|
+ assert_eq!(parse_datetime(Input(b"20010203T040506+0700")), parse_datetime(Input(b"2001-02-03T040506+0700")));
|
||
|
+ assert_eq!(parse_datetime(Input(b"20010203T040506+0000")), parse_datetime(Input(b"20010203T040506Z")));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015W056T04:05:06+07:00")), parse_datetime(Input(b"2015-W05-6T04:05:06+07:00")));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_datetime_correct() {
|
||
|
- assert_eq!(parse_datetime(b"20060831T16:44+00:00"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2006, month:08, day:31}, time: Time{ hour: 16, minute:44, second:0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2007-08-31T16:45+00:00"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2007, month:08, day:31}, time: Time{ hour: 16, minute:45, second:0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"20070831T1646+00:00"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2007, month:08, day:31}, time: Time{ hour: 16, minute:46, second:0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"20070831T1647+0000"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2007, month:08, day:31}, time: Time{ hour: 16, minute:47, second:0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2009-02-01T09:00:22+05"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2009, month:02, day:01}, time: Time{ hour: 9, minute:0, second:22, millisecond: 0, tz_offset_hours: 5, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2010-01-01T12:00:00+01:00"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2010, month:1, day:1}, time: Time{ hour: 12, minute:0, second:0, millisecond: 0, tz_offset_hours: 1, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2011-06-30T18:30:00+02:00"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2011, month:06, day:30}, time: Time{ hour: 18, minute:30, second:0, millisecond: 0, tz_offset_hours: 2, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015-06-29T23:07+02:00"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2015, month:06, day:29}, time: Time{ hour: 23, minute:07, second:0, millisecond: 0, tz_offset_hours: 2, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015-06-26T16:43:16"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2015, month:06, day:26}, time: Time{ hour: 16, minute:43, second:16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015-06-26T16:43:16"), Ok((&[][..], DateTime{ date: Date::YMD { year: 2015, month:06, day:26}, time: Time{ hour: 16, minute:43, second:16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015-W05-6T04:05:06+07:00"), Ok((&[][..], DateTime{ date: Date::Week { year: 2015, ww:05, d:6}, time: Time{ hour: 04, minute:5, second:6, millisecond: 0, tz_offset_hours: 7, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015W056T04:05:06+07:00"), Ok((&[][..], DateTime{ date: Date::Week { year: 2015, ww:05, d:6}, time: Time{ hour: 04, minute:5, second:6, millisecond: 0, tz_offset_hours: 7, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015-056T04:05:06+07:00"), Ok((&[][..], DateTime{ date: Date::Ordinal{ year: 2015, ddd:56}, time: Time{ hour: 04, minute:5, second:6, millisecond: 0, tz_offset_hours: 7, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015056T04:05:06+07:00"), Ok((&[][..], DateTime{ date: Date::Ordinal{ year: 2015, ddd:56}, time: Time{ hour: 04, minute:5, second:6, millisecond: 0, tz_offset_hours: 7, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015-297T16:30:48Z"), Ok((&[][..], DateTime{ date: Date::Ordinal{ year: 2015, ddd:297}, time: Time{ hour: 16, minute:30, second:48, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2015-W43-6T16:30:48Z"), Ok((&[][..], DateTime{ date: Date::Week { year: 2015, ww:43, d:6}, time: Time{ hour: 16, minute:30, second:48, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2001-W05-6T04:05:06.1234Z"), Ok((&[][..], DateTime{ date: Date::Week { year: 2001, ww:05, d:6}, time: Time{ hour: 04, minute:05, second:06, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
- assert_eq!(parse_datetime(b"2001-W05-6T04:05:06.12345Z"), Ok((&[][..], DateTime{ date: Date::Week { year: 2001, ww:05, d:6}, time: Time{ hour: 04, minute:05, second:06, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"20060831T16:44+00:00")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2006, month:08, day:31}, time: Time{ hour: 16, minute:44, second:0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2007-08-31T16:45+00:00")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2007, month:08, day:31}, time: Time{ hour: 16, minute:45, second:0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"20070831T1646+00:00")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2007, month:08, day:31}, time: Time{ hour: 16, minute:46, second:0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"20070831T1647+0000")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2007, month:08, day:31}, time: Time{ hour: 16, minute:47, second:0, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2009-02-01T09:00:22+05")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2009, month:02, day:01}, time: Time{ hour: 9, minute:0, second:22, millisecond: 0, tz_offset_hours: 5, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2010-01-01T12:00:00+01:00")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2010, month:1, day:1}, time: Time{ hour: 12, minute:0, second:0, millisecond: 0, tz_offset_hours: 1, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2011-06-30T18:30:00+02:00")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2011, month:06, day:30}, time: Time{ hour: 18, minute:30, second:0, millisecond: 0, tz_offset_hours: 2, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015-06-29T23:07+02:00")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2015, month:06, day:29}, time: Time{ hour: 23, minute:07, second:0, millisecond: 0, tz_offset_hours: 2, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015-06-26T16:43:16")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2015, month:06, day:26}, time: Time{ hour: 16, minute:43, second:16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015-06-26T16:43:16")), Ok((Input(&[][..]), DateTime{ date: Date::YMD { year: 2015, month:06, day:26}, time: Time{ hour: 16, minute:43, second:16, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015-W05-6T04:05:06+07:00")), Ok((Input(&[][..]), DateTime{ date: Date::Week { year: 2015, ww:05, d:6}, time: Time{ hour: 04, minute:5, second:6, millisecond: 0, tz_offset_hours: 7, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015W056T04:05:06+07:00")), Ok((Input(&[][..]), DateTime{ date: Date::Week { year: 2015, ww:05, d:6}, time: Time{ hour: 04, minute:5, second:6, millisecond: 0, tz_offset_hours: 7, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015-056T04:05:06+07:00")), Ok((Input(&[][..]), DateTime{ date: Date::Ordinal{ year: 2015, ddd:56}, time: Time{ hour: 04, minute:5, second:6, millisecond: 0, tz_offset_hours: 7, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015056T04:05:06+07:00")), Ok((Input(&[][..]), DateTime{ date: Date::Ordinal{ year: 2015, ddd:56}, time: Time{ hour: 04, minute:5, second:6, millisecond: 0, tz_offset_hours: 7, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015-297T16:30:48Z")), Ok((Input(&[][..]), DateTime{ date: Date::Ordinal{ year: 2015, ddd:297}, time: Time{ hour: 16, minute:30, second:48, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2015-W43-6T16:30:48Z")), Ok((Input(&[][..]), DateTime{ date: Date::Week { year: 2015, ww:43, d:6}, time: Time{ hour: 16, minute:30, second:48, millisecond: 0, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2001-W05-6T04:05:06.1234Z")), Ok((Input(&[][..]), DateTime{ date: Date::Week { year: 2001, ww:05, d:6}, time: Time{ hour: 04, minute:05, second:06, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
+ assert_eq!(parse_datetime(Input(b"2001-W05-6T04:05:06.12345Z")), Ok((Input(&[][..]), DateTime{ date: Date::Week { year: 2001, ww:05, d:6}, time: Time{ hour: 04, minute:05, second:06, millisecond: 123, tz_offset_hours: 0, tz_offset_minutes: 0}})));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
@@ -224,28 +230,28 @@ fn test_datetime_error() {
|
||
|
let test_datetimes = vec!["ppp", "dumd-di-duTmd:iu:m"];
|
||
|
|
||
|
for iso_string in test_datetimes {
|
||
|
- let res = parse_datetime(iso_string.as_bytes());
|
||
|
+ let res = parse_datetime(Input(iso_string.as_bytes()));
|
||
|
assert!(res.is_err());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn disallows_notallowed() {
|
||
|
- assert!(parse_time(b"30:90:90").is_err());
|
||
|
- assert!(parse_date(b"0000-20-40").is_err());
|
||
|
- assert!(parse_datetime(b"2001-w05-6t04:05:06.123z").is_err());
|
||
|
+ assert!(parse_time(Input(b"30:90:90")).is_err());
|
||
|
+ assert!(parse_date(Input(b"0000-20-40")).is_err());
|
||
|
+ assert!(parse_datetime(Input(b"2001-w05-6t04:05:06.123z")).is_err());
|
||
|
}
|
||
|
|
||
|
// #[test]
|
||
|
// fn corner_cases() {
|
||
|
// // how to deal with left overs?
|
||
|
-// assert!(parse_datetime(b"2015-06-26T22:57:09Z00:00").is_done());
|
||
|
+// assert!(parse_datetime(Input(b"2015-06-26T22:57:09Z00:00").is_done());
|
||
|
// assert!(date("2015-06-26T22:57:09Z00:00").is_err());
|
||
|
//
|
||
|
-// assert!(parse_datetime(b"2015-06-26T22:57:09Z+00:00").is_done());
|
||
|
+// assert!(parse_datetime(Input(b"2015-06-26T22:57:09Z+00:00").is_done());
|
||
|
// assert!(datetime("2015-06-26T22:57:09Z+00:00").is_err());
|
||
|
-// assert!(parse_datetime(b"2001-W05-6T04:05:06.123455Z").is_err());
|
||
|
-// assert!(parse_datetime(b"2015-06-26TZ").is_err());
|
||
|
+// assert!(parse_datetime(Input(b"2001-W05-6T04:05:06.123455Z").is_err());
|
||
|
+// assert!(parse_datetime(Input(b"2015-06-26TZ").is_err());
|
||
|
// }
|
||
|
|
||
|
#[test]
|
||
|
--
|
||
|
2.18.0.rc1
|
||
|
|