tafia / calamine

A pure Rust Excel/OpenDocument SpreadSheets file reader: rust on metal sheets
MIT License
1.72k stars 159 forks source link

Deserialize DateTime etc with serde #224

Open markcatley opened 3 years ago

markcatley commented 3 years ago

Are you interested in including functions for use with the Deserialize derive? eg:

#[serde(deserialize_with = "deserialize_calamine_date")]
fn deserialize_calamine_date<'de, D: serde::de::Deserializer<'de>>(
    deserializer: D,
) -> Result<chrono::NaiveDate, D::Error> {
    struct Visitor;
    impl<'de> serde::de::Visitor<'de> for Visitor {
        type Value = chrono::NaiveDate;

        fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "expecting Date cell")
        }

        fn visit_f64<E: serde::de::Error>(self, v: f64) -> Result<Self::Value, E> {
            Ok(calamine::DataType::Float(v).as_date().unwrap())
        }
    }

    deserializer.deserialize_any(Visitor)
}

I tried to fix it so it just works but couldn't figure it out. I think you'd need to reliably detect the cell formatting and then provide a formatted string when asked for it via serde - currently, it provides an f64 as a string which doesn't work.

If you'd like them then let me know where to put them and what to call them and I'll create a PR for all the accepted date types.

tafia commented 3 years ago

I am definitely interested in something like this but detecting the formatting etc is a lot of work, in particular if you want to support non-xlsx formats.

tafia commented 1 year ago

We're starting to have some support for Date format parsing fyi.