1
0
mirror of https://github.com/bspeice/dtparse synced 2025-07-13 03:39:40 -04:00

10 Commits

Author SHA1 Message Date
ed919e84ef #38: Release new dateutil version 2022-06-15 22:21:25 -04:00
4d8ade4b05 #36: Add test to assert no longer panics 2022-06-15 22:20:37 -04:00
6a88885ef5 #35: Add test to make sure it does not panic 2022-06-15 22:18:20 -04:00
a193a79afa Add a test for issue 34 2022-06-15 22:16:15 -04:00
5b3be160f6 Merge pull request #37 from ahkrr/master
upgrade dependency rust_decimal ^0.10.1 -> 1.17.0
2022-03-24 21:47:10 -04:00
8f6a3b179d upgrade dependency rust_decimal ^0.10.1 -> 1.17.0 2021-11-14 14:14:11 +01:00
028c45e3fe Fix #32 and #33 2021-08-23 22:36:03 -04:00
f1ca4e4129 Merge pull request #31 from sbrocket/delete-unneeded-dep
Delete unused chrono-tz dependency
2021-06-03 22:56:34 -04:00
61d3ed025e Delete unused chrono-tz dependency 2021-06-03 19:45:47 -07:00
440ba918f6 Version 1.2.0 2020-10-04 20:10:30 -04:00
3 changed files with 53 additions and 15 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "dtparse"
version = "1.1.0"
version = "1.3.0"
authors = ["Bradlee Speice <bradlee@speice.io>"]
description = "A dateutil-compatible timestamp parser for Rust"
repository = "https://github.com/bspeice/dtparse.git"
@ -17,7 +17,9 @@ name = "dtparse"
[dependencies]
chrono = "0.4"
chrono-tz = "0.5"
lazy_static = "1.1"
num-traits = "0.2"
rust_decimal = "^0.10.1"
rust_decimal = { version = "1.17.0", default-features = false }
[dev-dependencies]
base64 = "0.13"

View File

@ -74,7 +74,6 @@
extern crate lazy_static;
extern crate chrono;
extern crate chrono_tz;
extern crate num_traits;
extern crate rust_decimal;
@ -1092,9 +1091,8 @@ impl Parser {
} else if let Some(hms_idx) = self.find_hms_index(idx, tokens, info, true) {
// HH[ ]h or MM[ ]m or SS[.ss][ ]s
let (new_idx, hms) = self.parse_hms(idx, tokens, info, Some(hms_idx));
if hms.is_some() {
// TODO: This unwrap is unjustified.
self.assign_hms(res, value_repr, hms.unwrap());
if let Some(hms) = hms {
self.assign_hms(res, value_repr, hms)?;
}
idx = new_idx;
} else if idx + 2 < len_l && tokens[idx + 1] == ":" {
@ -1102,7 +1100,7 @@ impl Parser {
// TODO: Better story around Decimal handling
res.hour = Some(value.floor().to_i64().unwrap() as i32);
// TODO: Rescope `value` here?
value = self.to_decimal(&tokens[idx + 2]);
value = self.to_decimal(&tokens[idx + 2])?;
let min_sec = self.parse_min_sec(value);
res.minute = Some(min_sec.0);
res.second = min_sec.1;
@ -1153,7 +1151,9 @@ impl Parser {
res.hour = Some(self.adjust_ampm(hour, ampm));
idx += 1;
} else {
ymd.append(value.floor().to_i64().unwrap() as i32, &value_repr, None)?;
//let value = value.floor().to_i32().ok_or(Err(ParseError::InvalidNumeric()))
let value = value.floor().to_i32().ok_or_else(|| ParseError::InvalidNumeric(value_repr.to_owned()))?;
ymd.append(value, &value_repr, None)?;
}
idx += 1;
@ -1266,11 +1266,11 @@ impl Parser {
}
}
fn assign_hms(&self, res: &mut ParsingResult, value_repr: &str, hms: usize) {
let value = self.to_decimal(value_repr);
fn assign_hms(&self, res: &mut ParsingResult, value_repr: &str, hms: usize) -> ParseResult<()> {
let value = self.to_decimal(value_repr)?;
if hms == 0 {
res.hour = Some(value.to_i64().unwrap() as i32);
res.hour = value.to_i32();
if !close_to_integer(&value) {
res.minute = Some((*SIXTY * (value % *ONE)).to_i64().unwrap() as i32);
}
@ -1283,11 +1283,12 @@ impl Parser {
res.second = Some(sec);
res.microsecond = Some(micro);
}
Ok(())
}
fn to_decimal(&self, value: &str) -> Decimal {
// TODO: Justify unwrap
Decimal::from_str(value).unwrap()
fn to_decimal(&self, value: &str) -> ParseResult<Decimal> {
Decimal::from_str(value).or_else(|_| Err(ParseError::InvalidNumeric(value.to_owned())))
}
fn parse_min_sec(&self, value: Decimal) -> (i32, Option<i32>) {

View File

@ -1,5 +1,6 @@
use chrono::NaiveDate;
use std::collections::HashMap;
use std::str;
use parse;
use ParseError;
@ -60,3 +61,37 @@ fn an_even_larger_int() {
fn empty_string() {
assert_eq!(parse(""), Err(ParseError::NoDate))
}
#[test]
fn github_33() {
assert_eq!(parse("66:'"), Err(ParseError::InvalidNumeric("'".to_owned())))
}
#[test]
fn github_32() {
assert_eq!(parse("99999999999999999999999"), Err(ParseError::InvalidNumeric("99999999999999999999999".to_owned())))
}
#[test]
fn github_34() {
let parse_vec = base64::decode("KTMuLjYpGDYvLjZTNiouNjYuHzZpLjY/NkwuNh42Ry42PzYnKTMuNk02NjY2NjA2NjY2NjY2NjYTNjY2Ni82NjY2NlAuNlAuNlNI").unwrap();
let parse_str = str::from_utf8(&parse_vec).unwrap();
let parse_result = parse(parse_str);
assert!(parse_result.is_err());
}
#[test]
fn github_35() {
let parse_vec = base64::decode("KTY6LjYqNio6KjYn").unwrap();
let parse_str = str::from_utf8(&parse_vec).unwrap();
let parse_result = parse(parse_str);
assert!(parse_result.is_err());
}
#[test]
fn github_36() {
let parse_vec = base64::decode("KTYuLg==").unwrap();
let parse_str = str::from_utf8(&parse_vec).unwrap();
let parse_result = parse(parse_str);
assert!(parse_result.is_err());
}