I probably overlooked something obvious, but I can't find a way to serialize a single form value with serde. Essentially I'm looking for a de-facto standard lib for something like this:
A URL-encoded value is by definition a collection of key-value pairs. Serializing an atomic value directly results in the serializer returning an error: "top-level serializer supports only maps and structs". I don't think what you want is possible.
What do you need this for? I'm pretty sure there is a better solution for what you are actually trying to achieve.
What kind of types are you anticipating? If it's always a simple enum with unit variants, then you have a lot of options, the two simplest I can think of are:
serialize to a serde_json::Value and extract the resulting Value::String, or
use strum to #[derive(AsRefStr)] and get a &'static string directly out of the enum.
That's likely not the case – what if you have a complex type, e.g. a HashMap, a struct, or even just some sort of sequence? What do you want to do in that case?
If you want to URL-escape a string, then use form_urlencoded for that purpose. However, for displaying in HTML, you probably want HTMLescaping instead.
That's likely not the case – what if you have a complex type, e.g. a HashMap , a struct, or even just some sort of sequence? What do you want to do in that case?
In my case I'm using htmx to pass the form as axum::extract::Json<MyInput> to a handler, so serde is already there and all fields of MyInput implement Serialize. So I'm ok for the encoder to accept any serializable even if output won't make sense for complex types.