Skip to content

Add query_parsed method #1

@rousan

Description

@rousan

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions