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

RangeDeserializerBuilder example does not works #434

Open
latot opened this issue May 15, 2024 · 3 comments
Open

RangeDeserializerBuilder example does not works #434

latot opened this issue May 15, 2024 · 3 comments
Labels

Comments

@latot
Copy link

latot commented May 15, 2024

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
Copy link
Owner

tafia commented May 16, 2024

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
Copy link
Author

latot commented May 16, 2024

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
Copy link

McDonnellJoseph commented May 23, 2024

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: 

@tafia tafia added the doc label May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants