Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added is_error and get_error methods for the DataType trait #409

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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