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

Rename DataType methods as_f64 as_i64 into as_float as_int #389

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
D: serde::Deserializer<'de>,
{
let data_type = calamine::DataType::deserialize(deserializer)?;
if let Some(float) = data_type.as_f64() {
if let Some(float) = data_type.as_float() {
Ok(Some(float))
} else {
Ok(None)
Expand All @@ -85,7 +85,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
```


### Reader: Simple

```rust
Expand Down Expand Up @@ -160,7 +159,7 @@ for s in sheets {

## Features

- `dates`: Add date related fn to `DataType`.
- `dates`: Add date related fn to `DataType`.
- `picture`: Extract picture data.

### Others
Expand Down
2 changes: 1 addition & 1 deletion examples/excel_to_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn write_range<W: Write>(dest: &mut W, range: &Range<Data>) -> std::io::Result<(
write!(dest, "{}", s)
}
Data::Float(ref f) => write!(dest, "{}", f),
Data::DateTime(ref d) => write!(dest, "{}", d.as_f64()),
Data::DateTime(ref d) => write!(dest, "{}", d.as_float()),
Data::Int(ref i) => write!(dest, "{}", i),
Data::Error(ref e) => write!(dest, "{:?}", e),
Data::Bool(ref b) => write!(dest, "{}", b),
Expand Down
16 changes: 8 additions & 8 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl DataType for Data {
}
}

fn as_i64(&self) -> Option<i64> {
fn as_int(&self) -> Option<i64> {
match self {
Data::Int(v) => Some(*v),
Data::Float(v) => Some(*v as i64),
Expand All @@ -147,7 +147,7 @@ impl DataType for Data {
}
}

fn as_f64(&self) -> Option<f64> {
fn as_float(&self) -> Option<f64> {
match self {
Data::Int(v) => Some(*v as f64),
Data::Float(v) => Some(*v),
Expand Down Expand Up @@ -446,7 +446,7 @@ impl DataType for DataRef<'_> {
}
}

fn as_i64(&self) -> Option<i64> {
fn as_int(&self) -> Option<i64> {
match self {
DataRef::Int(v) => Some(*v),
DataRef::Float(v) => Some(*v as i64),
Expand All @@ -456,7 +456,7 @@ impl DataType for DataRef<'_> {
}
}

fn as_f64(&self) -> Option<f64> {
fn as_float(&self) -> Option<f64> {
match self {
DataRef::Int(v) => Some(*v as f64),
DataRef::Float(v) => Some(*v),
Expand Down Expand Up @@ -525,10 +525,10 @@ pub trait DataType {
fn as_string(&self) -> Option<String>;

/// Try converting data type into an int
fn as_i64(&self) -> Option<i64>;
fn as_int(&self) -> Option<i64>;

/// Try converting data type into a float
fn as_f64(&self) -> Option<f64>;
fn as_float(&self) -> Option<f64>;

/// Try converting data type into a date
#[cfg(feature = "dates")]
Expand Down Expand Up @@ -590,7 +590,7 @@ pub trait DataType {
use std::str::FromStr;

if self.is_int() || self.is_float() {
self.as_f64()
self.as_float()
.map(|f| ExcelDateTime::from_value_only(f).as_datetime())
} else if self.is_datetime() {
self.get_datetime().map(|d| d.as_datetime())
Expand Down Expand Up @@ -671,7 +671,7 @@ impl ExcelDateTime {
}

/// Converting data type into a float
pub fn as_f64(&self) -> f64 {
pub fn as_float(&self) -> f64 {
self.value
}

Expand Down
4 changes: 2 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ impl<'a, 'de> serde::Deserializer<'de> for DataDeserializer<'a> {
Data::Bool(v) => visitor.visit_bool(*v),
Data::Int(v) => visitor.visit_i64(*v),
Data::Empty => visitor.visit_unit(),
Data::DateTime(v) => visitor.visit_f64(v.as_f64()),
Data::DateTime(v) => visitor.visit_f64(v.as_float()),
Data::DateTimeIso(v) => visitor.visit_str(v),
Data::DurationIso(v) => visitor.visit_str(v),
Data::Error(ref err) => Err(DeError::CellError {
Expand Down Expand Up @@ -633,7 +633,7 @@ impl<'a, 'de> serde::Deserializer<'de> for DataDeserializer<'a> {
Data::Empty => visitor.visit_bool(false),
Data::Float(v) => visitor.visit_bool(*v != 0.),
Data::Int(v) => visitor.visit_bool(*v != 0),
Data::DateTime(v) => visitor.visit_bool(v.as_f64() != 0.),
Data::DateTime(v) => visitor.visit_bool(v.as_float() != 0.),
Data::DateTimeIso(_) => visitor.visit_bool(true),
Data::DurationIso(_) => visitor.visit_bool(true),
Data::Error(ref err) => Err(DeError::CellError {
Expand Down