Skip to content

Commit cc40d3e

Browse files
committed
trim whitespace when parsing to typed values
1 parent e9adcb4 commit cc40d3e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/parse/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,25 @@ impl TagDecoder {
106106
) -> Result<Datum, Error> {
107107
match typ {
108108
Some("n") | Some("N") => {
109-
let num =
110-
Decimal::from_str(v).map_err(|_| self.invalid_tag(tag))?;
109+
let num = Decimal::from_str(v.trim())
110+
.map_err(|_| self.invalid_tag(tag))?;
111111
Ok(Datum::Number(num))
112112
}
113113
Some("b") | Some("B") => {
114-
let b = match v {
114+
let b = match v.trim() {
115115
"Y" | "y" => true,
116116
"N" | "n" => false,
117117
_ => return Err(self.invalid_tag(tag)),
118118
};
119119
Ok(Datum::Boolean(b))
120120
}
121121
Some("d") | Some("D") => {
122-
let date = NaiveDate::parse_from_str(v, "%Y%m%d")
122+
let date = NaiveDate::parse_from_str(v.trim(), "%Y%m%d")
123123
.map_err(|_| self.invalid_tag(tag))?;
124124
Ok(Datum::Date(date))
125125
}
126126
Some("t") | Some("T") => {
127-
let time = NaiveTime::parse_from_str(v, "%H%M%S")
127+
let time = NaiveTime::parse_from_str(v.trim(), "%H%M%S")
128128
.map_err(|_| self.invalid_tag(tag))?;
129129
Ok(Datum::Time(time))
130130
}

src/parse/test.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,38 @@ async fn underscore() {
192192
no_tags(&mut f).await;
193193
}
194194

195+
#[tokio::test]
196+
async fn trim_typed() {
197+
let mut f = tags("<a:5:n> 1.0 <b:3:b> Y <c:10:d> 20200101 <d:8:t> 123456 ");
198+
199+
let field = next_field(&mut f).await;
200+
assert_eq!(field.name(), "a");
201+
assert_eq!(
202+
field.value().as_number().unwrap(),
203+
Decimal::from_str("1.0").unwrap()
204+
);
205+
206+
let field = next_field(&mut f).await;
207+
assert_eq!(field.name(), "b");
208+
assert!(field.value().as_bool().unwrap());
209+
210+
let field = next_field(&mut f).await;
211+
assert_eq!(field.name(), "c");
212+
assert_eq!(
213+
field.value().as_date().unwrap(),
214+
NaiveDate::from_ymd_opt(2020, 1, 1).unwrap()
215+
);
216+
217+
let field = next_field(&mut f).await;
218+
assert_eq!(field.name(), "d");
219+
assert_eq!(
220+
field.value().as_time().unwrap(),
221+
NaiveTime::from_hms_opt(12, 34, 56).unwrap()
222+
);
223+
224+
no_tags(&mut f).await;
225+
}
226+
195227
#[tokio::test]
196228
async fn case_insensitive_lookup() {
197229
let mut s = RecordStream::new("<FOO:3>Bar<eor>".as_bytes(), true);

0 commit comments

Comments
 (0)