Skip to content

Commit

Permalink
bump calamine to master
Browse files Browse the repository at this point in the history
  • Loading branch information
dimastbk committed Jan 20, 2024
1 parent b28a210 commit e6dd1a6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ name = "python_calamine"
crate-type = ["cdylib"]

[dependencies]
calamine = { version = "0.23.1", features = ["dates"] }
calamine = { git = "https://github.com/tafia/calamine/", rev = "2620510783cb150c84c4b174d3bdf461f1f68264", features = ["dates"] }
pyo3 = { version = "0.19.2", features = [
"extension-module",
"chrono",
Expand Down
95 changes: 55 additions & 40 deletions src/types/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use std::convert::From;
use calamine::DataType;
use pyo3::prelude::*;

/// https://learn.microsoft.com/en-us/office/troubleshoot/excel/1900-and-1904-date-system
static EXCEL_1900_1904_DIFF: f64 = 1462.0;

#[derive(Debug)]
pub enum CellValue {
Int(i64),
Expand Down Expand Up @@ -41,45 +38,63 @@ impl ToPyObject for CellValue {
}
}

impl From<&DataType> for CellValue {
fn from(value: &DataType) -> Self {
match value {
DataType::Int(v) => CellValue::Int(v.to_owned()),
DataType::Float(v) => CellValue::Float(v.to_owned()),
DataType::String(v) => CellValue::String(String::from(v)),
DataType::DateTime(v) => {
// FIXME: need to fix after fixing in calamine
if v < &1.0 || (*v - EXCEL_1900_1904_DIFF < 1.0 && *v - EXCEL_1900_1904_DIFF > 0.0)
{
value.as_time().map(CellValue::Time)
} else if *v == (*v as u64) as f64 {
value.as_date().map(CellValue::Date)
} else {
value.as_datetime().map(CellValue::DateTime)
}
impl<DT> From<&DT> for CellValue
where
DT: DataType,
{
fn from(value: &DT) -> Self {
if value.is_int() {
value
.get_int()
.map(CellValue::Int)
.unwrap_or(CellValue::Empty)
} else if value.is_float() {
value
.get_float()
.map(CellValue::Float)
.unwrap_or(CellValue::Empty)
} else if value.is_string() {
value
.get_string()
.map(|s| CellValue::String(s.to_owned()))
.unwrap_or(CellValue::Empty)
} else if value.is_datetime() {
let dt = value.get_datetime().unwrap();
let v = dt.as_f64();
if dt.is_duration() {
value.as_duration().map(CellValue::Timedelta)
} else if v < 1.0 {
value.as_time().map(CellValue::Time)
} else if v == (v as u64) as f64 {
value.as_date().map(CellValue::Date)
} else {
value.as_datetime().map(CellValue::DateTime)
}
.unwrap_or(CellValue::Float(v.to_owned())),
DataType::DateTimeIso(v) => {
if v.contains('T') {
value.as_datetime().map(CellValue::DateTime)
} else if v.contains(':') {
value.as_time().map(CellValue::Time)
} else {
value.as_date().map(CellValue::Date)
}
.unwrap_or(CellValue::Float(v))
} else if value.is_datetime_iso() {
let v = value.get_datetime_iso().unwrap();
if v.contains('T') {
value.as_datetime().map(CellValue::DateTime)
} else if v.contains(':') {
value.as_time().map(CellValue::Time)
} else {
value.as_date().map(CellValue::Date)
}
.unwrap_or(CellValue::String(v.to_owned())),
DataType::Duration(v) => value
.as_duration()
.map(CellValue::Timedelta)
.unwrap_or(CellValue::Float(v.to_owned())),
DataType::DurationIso(v) => value
.as_time()
.map(CellValue::Time)
.unwrap_or(CellValue::String(v.to_owned())),
DataType::Bool(v) => CellValue::Bool(v.to_owned()),
DataType::Error(_) => CellValue::Empty,
DataType::Empty => CellValue::Empty,
.unwrap_or(CellValue::String(v.to_owned()))
} else if value.is_duration_iso() {
value.as_time().map(CellValue::Time).unwrap_or(
value
.get_duration_iso()
.map(|s| CellValue::String(s.to_owned()))
.unwrap_or(CellValue::Empty),
)
} else if value.is_bool() {
value
.get_bool()
.map(CellValue::Bool)
.unwrap_or(CellValue::Empty)
} else {
CellValue::Empty
}
}
}
16 changes: 7 additions & 9 deletions src/types/sheet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Display;
use std::sync::Arc;

use calamine::{DataType, Range, SheetType, SheetVisible};
use calamine::{Data, Range, SheetType, SheetVisible};
use pyo3::class::basic::CompareOp;
use pyo3::prelude::*;
use pyo3::types::PyList;
Expand Down Expand Up @@ -119,11 +119,11 @@ impl SheetMetadata {
pub struct CalamineSheet {
#[pyo3(get)]
name: String,
range: Arc<Range<DataType>>,
range: Arc<Range<Data>>,
}

impl CalamineSheet {
pub fn new(name: String, range: Range<DataType>) -> Self {
pub fn new(name: String, range: Range<Data>) -> Self {
CalamineSheet {
name,
range: Arc::new(range),
Expand Down Expand Up @@ -191,12 +191,10 @@ impl CalamineSheet {

Ok(PyList::new(
slf.py(),
range.rows().take(nrows as usize).map(|row| {
PyList::new(
slf.py(),
row.iter().map(<&DataType as Into<CellValue>>::into),
)
}),
range
.rows()
.take(nrows as usize)
.map(|row| PyList::new(slf.py(), row.iter().map(<&Data as Into<CellValue>>::into))),
))
}
}
7 changes: 2 additions & 5 deletions src/types/workbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ impl SheetsEnum {
}
}

fn worksheet_range(
&mut self,
name: &str,
) -> Result<calamine::Range<calamine::DataType>, Error> {
fn worksheet_range(&mut self, name: &str) -> Result<calamine::Range<calamine::Data>, Error> {
match self {
SheetsEnum::File(f) => f.worksheet_range(name),
SheetsEnum::FileLike(f) => f.worksheet_range(name),
Expand All @@ -47,7 +44,7 @@ impl SheetsEnum {
fn worksheet_range_at(
&mut self,
index: usize,
) -> Option<Result<calamine::Range<calamine::DataType>, Error>> {
) -> Option<Result<calamine::Range<calamine::Data>, Error>> {
match self {
SheetsEnum::File(f) => f.worksheet_range_at(index),
SheetsEnum::FileLike(f) => f.worksheet_range_at(index),
Expand Down

0 comments on commit e6dd1a6

Please sign in to comment.