It’d be a game changer to have an easy internal mapping to convert server data structures to URIs and vice versa. This would help a ton with maintenance. I’ve always wanted such a thing but never fully implemented it. It could be pretty doable in Ruby, but much harder in a language like Rust
What sucks about this is server framework support. They generally accept direct templated URI strings, but some don’t.
The rest of this page will be focused on a hypothetical Rust package, but other languages can work too
Here’s a hypothetical example of what the Toolkit might produce. This would be a generated Rust macro invocation
uri_mapping! Mapping {
"/users" -> Users
"/users/" user_id -> User(user_id: string) {
"/friends" -> Friends,
"/add-friend" -> AddFriend
}
}
// or something like
enum Mapping {
#[mapping("/users/{id}")]
User(id: string)
}
that would evaluate to something like this
enum MappingUser {
Friends,
AddFriend,
}
enum Mapping {
User(string, Option<MappingUser>)
}
impl MappingTrait for Mapping {
fn from_uri(uri: string) -> Self {
}
fn to_uri(&self) -> String {
match self {
Mapping::User(user_id, None) => "/users/{user_id}",
Mapping::User(user_id, Friends) => "/users/{user_id}/friends",
}
}
}