feat(axum): parse typed interaction values

Add InteractionForm::parse for common typed field extraction and use it in example interaction handlers to avoid repeated string parsing plumbing.

req: form/004

req: dx/003

req: examples/003
This commit is contained in:
slhx agent
2026-06-02 00:31:32 +02:00
parent a462e7f097
commit 1e53a6f7df
4 changed files with 33 additions and 11 deletions
+7
View File
@@ -243,6 +243,13 @@ impl InteractionForm {
.find_map(|(field, value)| (field == name).then_some(value.as_str()))
}
pub fn parse<T>(&self, name: &str) -> Option<T>
where
T: std::str::FromStr,
{
self.value(name).and_then(|value| value.parse().ok())
}
pub fn values<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a str> + 'a {
self.fields
.iter()
+11
View File
@@ -117,6 +117,17 @@ fn interaction_form_parses_handle_and_fields() {
assert_eq!(form.values("tag").collect::<Vec<_>>(), ["a", "b/c"]);
}
#[test]
fn interaction_form_parses_typed_values() {
// req: form/004 req: dx/003
let form = InteractionForm::parse_urlencoded(b"__h=42&count=7&bad=nope")
.expect("form should parse");
assert_eq!(form.parse::<u32>("count"), Some(7));
assert_eq!(form.parse::<u32>("bad"), None);
assert_eq!(form.parse::<u32>("missing"), None);
}
#[test]
fn interaction_form_requires_numeric_handle() {
assert_eq!(