koute / stdweb

A standard library for the client-side Web

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: implement try_into for FormDataEntry

norci opened this issue · comments

commented

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()
...