Skip to content

Commit

Permalink
feat: introduce is_x methods for date and time variants
Browse files Browse the repository at this point in the history
  • Loading branch information
lukapeschke committed Dec 22, 2023
1 parent 96b4561 commit 6ceb976
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ impl DataTypeTrait for DataType {
matches!(*self, DataType::String(_))
}

#[cfg(feature = "dates")]
fn is_duration(&self) -> bool {
matches!(*self, DataType::Duration(_))
}

#[cfg(feature = "dates")]
fn is_duration_iso(&self) -> bool {
matches!(*self, DataType::DurationIso(_))
}

#[cfg(feature = "dates")]
fn is_datetime(&self) -> bool {
matches!(*self, DataType::DateTime(_))
}

#[cfg(feature = "dates")]
fn is_datetime_iso(&self) -> bool {
matches!(*self, DataType::DateTimeIso(_))
}

fn get_int(&self) -> Option<i64> {
if let DataType::Int(v) = self {
Some(*v)
Expand Down Expand Up @@ -398,6 +418,26 @@ impl DataTypeTrait for DataTypeRef<'_> {
matches!(*self, DataTypeRef::String(_) | DataTypeRef::SharedString(_))
}

#[cfg(feature = "dates")]
fn is_duration(&self) -> bool {
matches!(*self, DataTypeRef::Duration(_))
}

#[cfg(feature = "dates")]
fn is_duration_iso(&self) -> bool {
matches!(*self, DataTypeRef::DurationIso(_))
}

#[cfg(feature = "dates")]
fn is_datetime(&self) -> bool {
matches!(*self, DataTypeRef::DateTime(_))
}

#[cfg(feature = "dates")]
fn is_datetime_iso(&self) -> bool {
matches!(*self, DataTypeRef::DateTimeIso(_))
}

fn get_int(&self) -> Option<i64> {
if let DataTypeRef::Int(v) = self {
Some(*v)
Expand Down Expand Up @@ -553,6 +593,22 @@ pub trait DataTypeTrait {
/// Assess if datatype is a string
fn is_string(&self) -> bool;

/// Assess if datatype is a duration
#[cfg(feature = "dates")]
fn is_duration(&self) -> bool;

/// Assess if datatype is an ISO8601 duration
#[cfg(feature = "dates")]
fn is_duration_iso(&self) -> bool;

/// Assess if datatype is a datetime
#[cfg(feature = "dates")]
fn is_datetime(&self) -> bool;

/// Assess if datatype is an ISO8601 datetime
#[cfg(feature = "dates")]
fn is_datetime_iso(&self) -> bool;

/// Try getting int value
fn get_int(&self) -> Option<i64>;

Expand Down
3 changes: 2 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use calamine::DataType::{
Bool, DateTime, DateTimeIso, Duration, DurationIso, Empty, Error, Float, String,
};
use calamine::{
open_workbook, open_workbook_auto, Ods, Reader, Sheet, SheetType, SheetVisible, Xls, Xlsb, Xlsx,
open_workbook, open_workbook_auto, DataTypeTrait, Ods, Reader, Sheet, SheetType, SheetVisible,
Xls, Xlsb, Xlsx,
};
use calamine::{CellErrorType::*, DataType};
use std::io::Cursor;
Expand Down

0 comments on commit 6ceb976

Please sign in to comment.