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

Add query_parsed method #1

Open
rousan opened this issue May 20, 2020 · 5 comments
Open

Add query_parsed method #1

rousan opened this issue May 20, 2020 · 5 comments

Comments

@rousan
Copy link
Member

rousan commented May 20, 2020

Impl

pub fn query_parsed<T: FromStr + 'static>(req: &HttpRequest, name: &str) -> crate::Result<Option<T>>
where
    <T as FromStr>::Err: std::error::Error + Send + Sync + 'static,
{
    match req.query(name) {
        Some(q) => q
            .parse::<T>()
            .context(format!("Couldn't parse query value: {} for query `{}`", q, name))
            .map(|q| Some(q)),
        None => Ok(None),
    }
}

Use:

let page = req.query_parsed::<u64>("page")?.unwrap_or_else(0);
@seanpianka
Copy link
Member

Is there a PR for this?

@rousan
Copy link
Member Author

rousan commented Dec 4, 2020

Not yet.

ta3pks pushed a commit that referenced this issue Mar 28, 2021
@ta3pks
Copy link
Contributor

ta3pks commented Mar 30, 2021

do you intend to create a pr for this @seanpianka ? this is pretty useful stuff

@seanpianka
Copy link
Member

No.

If you would like this feature, please contribute.

@ta3pks
Copy link
Contributor

ta3pks commented Mar 30, 2021

sure

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

3 participants