Skip to content

Commit

Permalink
Merge pull request #409 from lukapeschke/add-is-error
Browse files Browse the repository at this point in the history
feat: added is_error and get_error methods for the DataType trait
  • Loading branch information
tafia committed Feb 27, 2024
2 parents 64beb2d + 32c75e4 commit 10743f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

## Unreleased

- feat: added `is_error` and `get_error` methods to the `DataType` trait

## 0.24.0

- refactor (breaking): rename `DataType` enum to `Data` and `DataTypeRef` to `DataRef`
Expand Down
28 changes: 28 additions & 0 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ impl DataType for Data {
matches!(*self, Data::DateTimeIso(_))
}

fn is_error(&self) -> bool {
matches!(*self, Data::Error(_))
}

fn get_int(&self) -> Option<i64> {
if let Data::Int(v) = self {
Some(*v)
Expand Down Expand Up @@ -129,6 +133,13 @@ impl DataType for Data {
}
}

fn get_error(&self) -> Option<&CellErrorType> {
match self {
Data::Error(e) => Some(e),
_ => None,
}
}

fn as_string(&self) -> Option<String> {
match self {
Data::Float(v) => Some(v.to_string()),
Expand Down Expand Up @@ -382,6 +393,10 @@ impl DataType for DataRef<'_> {
matches!(*self, DataRef::DateTimeIso(_))
}

fn is_error(&self) -> bool {
matches!(*self, DataRef::Error(_))
}

fn get_int(&self) -> Option<i64> {
if let DataRef::Int(v) = self {
Some(*v)
Expand Down Expand Up @@ -438,6 +453,13 @@ impl DataType for DataRef<'_> {
}
}

fn get_error(&self) -> Option<&CellErrorType> {
match self {
DataRef::Error(e) => Some(e),
_ => None,
}
}

fn as_string(&self) -> Option<String> {
match self {
DataRef::Float(v) => Some(v.to_string()),
Expand Down Expand Up @@ -489,6 +511,9 @@ pub trait DataType {
/// Assess if datatype is a string
fn is_string(&self) -> bool;

/// Assess if datatype is a CellErrorType
fn is_error(&self) -> bool;

/// Assess if datatype is an ISO8601 duration
#[cfg(feature = "dates")]
fn is_duration_iso(&self) -> bool;
Expand Down Expand Up @@ -525,6 +550,9 @@ pub trait DataType {
#[cfg(feature = "dates")]
fn get_duration_iso(&self) -> Option<&str>;

/// Try getting Error value
fn get_error(&self) -> Option<&CellErrorType>;

/// Try converting data type into a string
fn as_string(&self) -> Option<String>;

Expand Down

0 comments on commit 10743f2

Please sign in to comment.