Understanding Into<>

Hi, I'm new to Rust and so far I am loving it. For a little project I'm doing, I found this crate that looks really useful: event_parser. The to_event method that I want to use returns an icalendar::components::Event. For the life of me, I cannot figure out how to do anything useful with this, as icalendar::components module is marked as private. Anything I try to do to convert it or use it yields compiler errors I don't really understand like

the trait `From<icalendar::components::Event>` is not implemented for `icalendar::calendar::CalendarElement`

In this case, I'm less concerned with getting this to work, and more learning enough to interpret the docs or read the source code if necessary to unstick myself when I get stuck like this. Anyone have some pointers for me? Thanks in advance!

To start, take a look at the function type signature for to_event (docs):

pub fn to_event(text: &str) -> Event

This is a function (not a method, nor trait-method) that takes a reference to str. It will return an Event.

Based on the error, it looks like you tried to call a trait method that requires implementation of the From trait - not what we want.

The "red herring" was likely the to_event name that conjured up something like to_string. The latter is a trait method you are likely familiar with.

Update

Per the subsequent responses and seeing the updated docs I think most of what was said here is unhelpful.

I think you've primary ran into a documentation error, where their icalendar links attempt to link locally and not to the separate crate. On my first pass, when the link gave a 404, I went to look at the source instead and saw that icalendar was a separate crate. If I had kept reading the front page though, I would have seen some examples to help clue me in:

use icalendar::{Component, Event};

Either way, now that I know it's a different crate, I can go to crates.io and look up icalendar, and find the crate including a link to the docs. From the documentation for Event, it looks like the only interesting functionality in terms of getting something useful with it will be due to the implemention of the Component trait. So I would start with reading up on that trait.

(To make use of the trait, you will have to bring it in to scope, as is done in the example in the code block above.)

Thank you so much for these replies! @EdmundsEcho I understood the documented type signature, but thanks for correcting me on terminology. It is indeed a function and not a method.

@quinedot , I got that far. I'm stuck on the fact that I want an icalendar::Event but to_event() gives me an icalendar::components::Event and I cannot figure out how to do anything remotely useful with that.

Looking at the lib.rs for icalendar 0.6.0, we can see:

pub use crate::components::{Event, Todo, Component};

Which tells us that icalendar::Event and icalendar::components::Event are in fact the same thing.

Oh neat, thank you so much for this hint but I am still stuck. I feel dumb.

I tried

use icalendar::{Calendar, Component, Event};
...
    let cal_event = Event::from(event);

and got

 --> src/main.rs:7:30
  |
7 |     let cevent = Event::from(event);
  |                              ^^^^^ expected struct `Event`, found struct `icalendar::components::Event`
  |
  = note: perhaps two different versions of crate `icalendar` are being used?

That is kind of what I expected to be the root problem here. Is that right? How would you confirm this and/or work around it?

A bit of a reboot on my part. Given the latest error, may you confirm what you have as a dependency in the Cargo file?

Yeah, that's right. You can run

cargo tree --duplicates

to see what's pulling in the multiple versions. Then you may be able to adjust the version in your Cargo.toml, or upgrade your dependencies, etc. If you need the newer verson of icalendar and there's no version of your other dependency that supports it, you may have to submit a request or such.

Yay, cargo tree --duplicates and documentation about resolving dependency version mismatch is what I needed but didn't know how to ask for. Thank you so much for pointing me in the right direction!

Closing the loop here, with a PR :smiley: Update icalendar by wolffiex · Pull Request #3 · isaacrlee/event-parser · GitHub

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.