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

Feature request: implement try_into for FormDataEntry #388

Open
norci opened this issue Jan 14, 2020 · 0 comments
Open

Feature request: implement try_into for FormDataEntry #388

norci opened this issue Jan 14, 2020 · 0 comments

Comments

@norci
Copy link

norci commented Jan 14, 2020

I'm using FormData to get some input values, and convert these values to i64.
currently I used this method to reduce duplicate code:

#[derive(Debug, Default)]
struct Number(i64);
impl TryFrom<Option<FormDataEntry>> for Number {
    type Error = ();
    fn try_from(form: Option<FormDataEntry>) -> Result<Self, Self::Error> {
        let form = form.unwrap();
        match form {
            FormDataEntry::String(s) => Ok(Number(s.parse().expect("Error: Not a number!"))),
            _ => Err(()),
        }
    }
}
fn get_num(form: &FormData, name: &str) -> i64 {
    Number::try_from(form.get(name)).unwrap().0
}

I'm not familiar with Rust, and I feel this code is ugly.

I think it's better to add similar functions in FormData. Then we can get the values easily.
e.g.

form.get(name).to_i64()
form.get(name).to_string()
...
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