tafia / calamine

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

RangeDeserializerBuilder example does not works #434

Open latot opened 1 month ago

latot commented 1 month ago

Hi, I'm following the example to read a columns from a xlsx:

  let mut excel: Xlsx<_> = open_workbook(path)?;

  let sheet_names = excel.sheet_names();

  if sheet_names.len() != 1 {
    todo!()
  }

  let range = excel.worksheet_range(sheet_names[0].as_str())?;
  let iter_records =
    RangeDeserializerBuilder::with_headers(&["column"])
    .from_range(&range)?;

But it fails with:

error[E0283]: type annotations needed for `RangeDeserializer<'_, Data, D>

Please fix the examples :3

Thx!

tafia commented 1 month ago

I suppose you are trying to deserialize into unknown type?

In the example, in the loop, there is some explicit type set :

 if let Some(result) = iter.next() {
        let (label, value): (String, f64) = result?;  // <-- HERE
        assert_eq!(label, "celsius");
        assert_eq!(value, 22.2222);
        Ok(())
    } else {
        Err(From::from("expected at least one record but got none"))
    }

Alternatively you can set it on the builder:

  let iter_records =
    RangeDeserializerBuilder::<_, String>::with_headers(&["column"])
    .from_range(&range)?;
latot commented 1 month ago

Hi yes, it is set after, but the code fails when creating the RangeDeserializerBuilder, actually the only way to call it is specifying the types, or the compiler will complains.

McDonnellJoseph commented 1 month ago

Hello, I'm also running into the same issue and I'm not quite sure I understand what's happening.

Following examples from calamine ReadMe:

I have the following code :

    let mut workbook: Xlsx<_> = open_workbook(file_path).unwrap();
    let mut sheet = workbook.worksheet_range("Feuil1").unwrap();
    let mut iter = RangeDeserializerBuilder::new()
            .from_range(&sheet)?;

and this doesn't compile and returns the following error.

error[E0283]: type annotations needed for `RangeDeserializer<'_, calamine::Data, D>`
   --> src/xlsx_reader.rs:13:9
    |
13  |     let mut iter = RangeDeserializerBuilder::new()
    |         ^^^^^^^^
14  |             .from_range(&sheet)?;
    |              ---------- type must be known at this point
    |
    = note: cannot satisfy `_: serde::de::DeserializeOwned`
note: required by a bound in `RangeDeserializerBuilder::<'h, H>::from_range`
   --> /home/joseph/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calamine-0.24.0/src/de.rs:205:12
    |
199 |     pub fn from_range<'cell, T, D>(
    |            ---------- required by a bound in this associated function
...
205 |         D: DeserializeOwned,
    |            ^^^^^^^^^^^^^^^^ required by this bound in `RangeDeserializerBuilder::<'h, H>::from_range`
help: consider giving `iter` an explicit type, where the type for type parameter `D` is specified
    |
13  |     let mut iter: RangeDeserializer<'_, calamine::Data, D> = RangeDeserializerBuilder::new()
    |                 ++++++++++++++++++++++++++++++++++++++++++
I'm new to rust so this may be just a rookie mistake but AFAIK the example code shown for calamine does not compile. Thanks for any help :smile: