Skip to content

Commit 9ef1058

Browse files
authored
Clean up timestamp conversion function (#211)
1 parent f1e3a0a commit 9ef1058

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

src/bin/ion/commands/timestamp_conversion.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,9 @@ struct TimestampConverter;
77

88
impl ElementMapper for TimestampConverter {
99
fn map(&self, element: Element) -> Result<Element> {
10-
Ok(match element.ion_type() {
11-
IonType::String => {
12-
let s = element.as_string().unwrap();
13-
if is_timestamp_like(s) {
14-
if let Ok(timestamp_element) = Element::read_one(s.as_bytes()) {
15-
if timestamp_element.ion_type() == IonType::Timestamp {
16-
return Ok(timestamp_element);
17-
}
18-
}
19-
}
20-
element
21-
}
22-
_ => element,
23-
})
10+
Ok(element.as_text()
11+
.and_then(as_timestamp)
12+
.unwrap_or(element))
2413
}
2514
}
2615

@@ -65,3 +54,11 @@ fn is_timestamp_like(s: &str) -> bool {
6554
_ => len > 10 && bytes[10] == b'T',
6655
}
6756
}
57+
58+
fn as_timestamp(s: &str) -> Option<Element> {
59+
if !is_timestamp_like(s) {
60+
return None;
61+
}
62+
Element::read_one(s.as_bytes()).ok()
63+
.filter(|e| e.ion_type() == IonType::Timestamp)
64+
}

0 commit comments

Comments
 (0)