diff --git a/src/bin/ion/commands/timestamp_conversion.rs b/src/bin/ion/commands/timestamp_conversion.rs index 4c53f455..ce58e5cb 100644 --- a/src/bin/ion/commands/timestamp_conversion.rs +++ b/src/bin/ion/commands/timestamp_conversion.rs @@ -7,20 +7,9 @@ struct TimestampConverter; impl ElementMapper for TimestampConverter { fn map(&self, element: Element) -> Result { - Ok(match element.ion_type() { - IonType::String => { - let s = element.as_string().unwrap(); - if is_timestamp_like(s) { - if let Ok(timestamp_element) = Element::read_one(s.as_bytes()) { - if timestamp_element.ion_type() == IonType::Timestamp { - return Ok(timestamp_element); - } - } - } - element - } - _ => element, - }) + Ok(element.as_text() + .and_then(as_timestamp) + .unwrap_or(element)) } } @@ -65,3 +54,11 @@ fn is_timestamp_like(s: &str) -> bool { _ => len > 10 && bytes[10] == b'T', } } + +fn as_timestamp(s: &str) -> Option { + if !is_timestamp_like(s) { + return None; + } + Element::read_one(s.as_bytes()).ok() + .filter(|e| e.ion_type() == IonType::Timestamp) +}