From d7aac3380e15c06797a1670bd7e9cec727315d96 Mon Sep 17 00:00:00 2001 From: Joshua Barr Date: Wed, 1 Oct 2025 08:37:03 -0700 Subject: [PATCH] Clean up timestamp conversion function --- src/bin/ion/commands/timestamp_conversion.rs | 25 +++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) 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) +}