obsidian-rs / obsidian

Ergonomic async http framework for reliable and efficient web
MIT License
26 stars 4 forks source link

Enhance params parsing #36

Closed plwai closed 4 years ago

plwai commented 4 years ago

Enable Option as the type.

Proposal

Params

app.get("/paramtest/:id", |ctx: Context| async move {
    let param_test: Option<i32> = ctx.param("id")?;

    Ok(response::json(param_test, StatusCode::OK))
});

If :id cannot be parsed, it will be None.

Forms/Query

app.post("/formtest", |mut ctx: Context| async move{
    let param_test: Option<i32>= ctx.form().await?;

    Ok(response::json(param_test, StatusCode::OK))
});

If item cannot be found, it will be None. Apply to fields in struct as well.

plwai commented 4 years ago

Close this issue since this is not applicable.

Reason

For the first case, the handler will not be hit if :id is not found. For the second case, forms, json and string query come with keyvalue pair. If there is no struct, it is not possible to map the value.