#How do you deal with error inside of .find

41 messages · Page 1 of 1 (latest)

junior onyx
#
slices
  .into_iter()
  .find(|&slice| SliceName::try_from_str(slice.name) == SliceName::TopLeft) // issue right here
  .ok_or_else(|| {
      SimpleError::new("repeatable_3x3_sprite module: No top left slice data!".into())
  })?
--> src/ui/repeatable_3x3_sprite.rs:103:71
|
103 |                 .find(|&slice| SliceName::try_from_str(slice.name) == SliceName::TopLeft)
|                                                                       ^^^^^^^^^^^^^^^^^^ expected enum `Result`, found enum `SliceName`
|
= note: expected enum `Result<SliceName, SimpleError>`
found enum `SliceName```
proven grove
#

but find returns an Option<T>

junior onyx
proven grove
#

what's the type of the iterator?

junior onyx
ocean flume
#

@junior onyx I'm assuming that try_from_str returns a Result?

ocean flume
#

There's your problem

proven grove
#

yeah you can't compare a Result with a SliceName

ocean flume
#

You're trying to compare a Result<SliceName, _> to a SliceName

junior onyx
#

yea, How do I deal with the Result?

#
    fn try_from_str(string: String) -> Result<Self, SimpleError> {
        match string.trim().to_lowercase().as_str() {
            "top_left" => Ok(Self::TopLeft), "top left" => Ok(Self::TopLeft), "topleft" => Ok(Self::TopLeft),
            "top" => Ok(Self::Top),
            "top_right" => Ok(Self::TopRight),
            "top right" => Ok(Self::TopRight),
            "topright" => Ok(Self::TopRight),
            "left" => Ok(Self::Left),
            "middle" => Ok(Self::Middle),
            "right" => Ok(Self::Right),
            "bottom_left" => Ok(Self::BottomLeft),
            "bottom left" => Ok(Self::BottomLeft),
            "bottomleft" => Ok(Self::BottomLeft),
            "bottom" => Ok(Self::Bottom),
            "bottom_right" => Ok(Self::BottomRight),
            "bottom right" => Ok(Self::BottomRight),
            "bottomright" => Ok(Self::BottomRight),
            _ => Err(SimpleError::new("Failed to convert slice name text into SliceName enum in module repeatable_3x3_sprite".into())),
        }
    }
glossy sleet
#

Do you want to return an error if find returns None?

Are you sure you want to produce the error inside find rather than do

slices
  .into_iter()
  .find(...)
  .ok_or_else(...)?;
junior onyx
glossy sleet
#

No, I mean, why inside find?

#

As opposed to after it

#

You want to do something if find fails, right?

proven grove
#

honestly you could return an Option<Self>

glossy sleet
#

Wouldn't you have to wait for it to be done to know if it failed?

ocean flume
junior onyx
#

If it fails inside of the find, also return an error

ocean flume
#

Their error is that they can't compare the fallible result to anything

glossy sleet
#

Oof, my bad

glossy sleet
#

Did we stabilize that yet, btw?

proven grove
#
    fn from_str(v: &str) -> Option<Self> {
        match v.trim().to_lowercase().as_str() {
            "top_left" | "top left" | "topleft" => Some(Self::TopLeft),
            // etc etc
            _ => None
        }
    }
ocean flume
#

@junior onyx Have you tried map + unwrap_or?
SliceName::try_from_str(slice.name).map(|slice_name| slice_name == SliceName::TopLeft).unwrap_or(false)

proven grove
#

and do SliceName::from_str(slice.name).map_or(false, |n| n == SliceName::TopLeft)

ocean flume
proven grove
#

right, should be from_str or something

#

done

ocean flume
#

Anyway, Dog, I've provided something that might work

#

As has Leo

junior onyx
proven grove
#
SliceName::try_from_str(slice.name).map_or(false, |slice_name| slice_name == SliceName::TopLeft)
ocean flume
#

I didn't bother looking, was working from memory

proven grove
#

lol tbf i only realized that it exists when clippy::pedantic shouted at me

junior onyx
#

Thanks! That worked. I wonder when I'll be able to come up with solutions like that lol.