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

Fallible extractors #23

Open
jonahlund opened this issue Nov 10, 2024 · 1 comment
Open

Fallible extractors #23

jonahlund opened this issue Nov 10, 2024 · 1 comment

Comments

@jonahlund
Copy link

Having infallible extractors makes them less flexible, it's generally better to let the user decide if they want the extraction to fail or not, especially considering the fact that you can use the Option extractor for any infallible extractions, i.e:

Option::<HxTarget>::from_request_parts(..)
@jonahlund
Copy link
Author

If you aren't convinced, consider this, say I want to write my custom extractor wrapping HxTarget, this is how I would want to write it:

pub enum MyTarget {
    SomeTarget,
    OtherTarget,
}

impl FromStr for MyTarget { .. }

impl FromRequestParts<AppState> for MyTarget {
    type Rejection = Error;

    async fn from_request_parts(
        parts: &mut Parts,
        state: &AppState,
    ) -> Result<Self, Self::Rejection> {
        let HxTarget(target) = HxTarget::from_request_parts(parts, state).await?;

        Ok(target.parse()?)
    }
}

Then if I wanted to do an infallible extraction here I would just do

Option::<MyTarget>::from_request_parts(..)

But since HxTarget is always infallible, I have to explicitly handle the case where it might be None even though we just want to propagate whatever error the extraction would return in case the header wasn't present.

It basically breaks the chain of extractors by having an infallible extractor that otherwise could be considered fallible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant