From 347631e39530275647e154184688c75e35d783da Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Fri, 9 May 2025 17:58:56 -0400 Subject: [PATCH] Add helper methods to ColumnData to help convert it to underlying data It's slightly awkward to get data out of this type, because you have to match it usually. I have added to_x() members instead to make it much easier. --- src/exd.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/exd.rs b/src/exd.rs index 78ddb54..ac90623 100644 --- a/src/exd.rs +++ b/src/exd.rs @@ -200,6 +200,96 @@ pub enum ColumnData { UInt64(u64), } +impl ColumnData { + // Returns a Some(String) if this column was a String, otherwise None. + pub fn into_string(&self) -> Option<&String> { + if let ColumnData::String(value) = self { + return Some(value); + } + None + } + + // Returns a Some(bool) if this column was a Bool, otherwise None. + pub fn into_bool(&self) -> Option<&bool> { + if let ColumnData::Bool(value) = self { + return Some(value); + } + None + } + + // Returns a Some(i8) if this column was a Int8, otherwise None. + pub fn into_i8(&self) -> Option<&i8> { + if let ColumnData::Int8(value) = self { + return Some(value); + } + None + } + + // Returns a Some(u8) if this column was a UInt8, otherwise None. + pub fn into_u8(&self) -> Option<&u8> { + if let ColumnData::UInt8(value) = self { + return Some(value); + } + None + } + + // Returns a Some(i16) if this column was a Int16, otherwise None. + pub fn into_i16(&self) -> Option<&i16> { + if let ColumnData::Int16(value) = self { + return Some(value); + } + None + } + + // Returns a Some(u16) if this column was a UInt16, otherwise None. + pub fn into_u16(&self) -> Option<&u16> { + if let ColumnData::UInt16(value) = self { + return Some(value); + } + None + } + + // Returns a Some(i32) if this column was a Int32, otherwise None. + pub fn into_i32(&self) -> Option<&i32> { + if let ColumnData::Int32(value) = self { + return Some(value); + } + None + } + + // Returns a Some(u32) if this column was a UInt32, otherwise None. + pub fn into_u32(&self) -> Option<&u32> { + if let ColumnData::UInt32(value) = self { + return Some(value); + } + None + } + + // Returns a Some(f32) if this column was a Float32, otherwise None. + pub fn into_f32(&self) -> Option<&f32> { + if let ColumnData::Float32(value) = self { + return Some(value); + } + None + } + + // Returns a Some(i64) if this column was a Int64, otherwise None. + pub fn into_i64(&self) -> Option<&i64> { + if let ColumnData::Int64(value) = self { + return Some(value); + } + None + } + + // Returns a Some(u64) if this column was a UInt64, otherwise None. + pub fn into_u64(&self) -> Option<&u64> { + if let ColumnData::UInt64(value) = self { + return Some(value); + } + None + } +} + #[derive(Debug, Clone)] pub struct ExcelSingleRow { pub columns: Vec,