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

Implement FromForm for Range #2759

Closed
RuboGubo opened this issue Mar 25, 2024 · 1 comment
Closed

Implement FromForm for Range #2759

RuboGubo opened this issue Mar 25, 2024 · 1 comment
Assignees
Labels
accepted An accepted request or suggestion good first issue Beginner-friendly for new contributors help wanted Contributions to this issue are needed request Request for new functionality
Milestone

Comments

@RuboGubo
Copy link

Hi, I'm back, I had to temporarily step away from Foss, but i'm back and happy to respond.

In terms of what you said: I ment FromFormField, which would not work anyway as Range<T> would most likely have to be implemented over two fields.
In terms of what it should look like in a form, you would most likely have two fields in the form <range_var_name>.start and <range_var_name>.end. I chose those names as that is what the Range<T> type uses internally.

Example HTML Rendered:
grafik

Example HTML Source:

<form class="Search" id="PrimeNumberSearch" action="{{https_url_for(request, 'SearchPrimeResult')}}" target="PrimeNumberSearchIF">
    <input type="number" name="prime_range.start" placeholder="min prime">
    <input type="number" name="prime_range.end" placeholder="max prime">
    <button class="submit" type="submit"></button>
</form>

Example Rocket Function:

#[derive(Debug, FromForm)]
struct FormData {
    prime_range: Range<i64>,
}

#[get("/prime", data="<form>")]
async fn get_prime_form(db: &PrimeDB, form: Form<FormData>) -> Result<Json<Vec<i64>>, Status> {
    Ok(Json(get_primes(db, form.into_inner().prime_range).await?))
}

And it would of course also work outside of a struct:

#[get("/prime", data="<form>")]
async fn get_prime_form(db: &PrimeDB, form: Form<Range<i64>>) -> Result<Json<Vec<i64>>, Status> {
    Ok(Json(get_primes(db, form.into_inner()).await?))
}
<form class="Search" id="PrimeNumberSearch" action="{{https_url_for(request, 'SearchPrimeResult')}}" target="PrimeNumberSearchIF">
    <input type="number" name="start" placeholder="min prime">
    <input type="number" name="end" placeholder="max prime">
    <button class="submit" type="submit"></button>
</form>

Originally posted by @RuboGubo in #2736 (comment)

@SergioBenitez
Copy link
Member

Sure. I'm happy to accept an impl.

@SergioBenitez SergioBenitez added help wanted Contributions to this issue are needed request Request for new functionality accepted An accepted request or suggestion labels Apr 3, 2024
@SergioBenitez SergioBenitez added good first issue Beginner-friendly for new contributors request Request for new functionality and removed request Request for new functionality labels Apr 17, 2024
@SergioBenitez SergioBenitez changed the title Re-open request: Allow from form macro to work on struct with range Implement FromForm for Range Apr 17, 2024
@SergioBenitez SergioBenitez self-assigned this May 2, 2024
@SergioBenitez SergioBenitez added this to the 0.6.0 milestone May 2, 2024
@github-project-automation github-project-automation bot moved this to Backlog in Rocket v0.6 May 2, 2024
@SergioBenitez SergioBenitez moved this from Backlog to In progress in Rocket v0.6 May 2, 2024
@github-project-automation github-project-automation bot moved this from In progress to Done in Rocket v0.6 May 3, 2024
SergioBenitez added a commit that referenced this issue May 22, 2024
Implements 'FromForm' for:

  * `char`
  * `Range<T: FromForm>`
  * `RangeFrom<T: FromForm>`
  * `RangeTo<T: FromForm>`
  * `RangeToInclusive<T: FromForm>`

Resolves #2759.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted An accepted request or suggestion good first issue Beginner-friendly for new contributors help wanted Contributions to this issue are needed request Request for new functionality
Projects
Status: Done
Development

No branches or pull requests

2 participants