FromForm: Shared parameters? #2678
-
Hi! I'm getting more confident with Rocket and Rust and porting of our backend is going well. But I noticed I am going into "too much copy-paste" (which leads to human error) and wish to consult if there are better ways: I have the following: #[derive(Debug, FromForm, Serialize)]
#[serde(crate = "rocket::serde")]
struct BaseParams<'a>
{
#[field(name = "userId")]
pub user_id: Option<u64>,
pub game: Option<&'a str>,
} Many (but not all) of our APIs take user_id & game. And many take more parameters: #[derive(Debug, FromForm, Serialize)]
#[serde(crate = "rocket::serde")]
struct GetObjs<'a> {
#[field(name = "userId")]
pub user_id: Option<u64>,
pub game: Option<&'a str>,
pub region_id: u16,
} Normally in Rust to avoid so much repetition this would be solved through composition, something like: #[derive(Debug, FromForm, Serialize)]
#[serde(crate = "rocket::serde")]
struct GetObjs<'a> {
pub base: BaseParams<'a>,
pub region_id: u16,
} However as far as I can tell, Rocket's FromForm does not support seeing into "base". And even if it does, I would need Rocket to look for "game" and not "base_game" or similar. I tried with a macro that emulates inheritance: macro_rules! AddAuthenticationFields {
($pub:vis struct $name:ident<$lifetime:tt> { $( $field:ident: $ty:ty ),* $(,)* }) => {
#[derive(Debug, FromForm, Serialize)]
#[serde(crate = "rocket::serde")]
$pub struct $name<$lifetime> {
#[field(name = "userId")]
pub user_id: Option<u64>,
pub game: Option<&$lifetime str>,
$( $field: $ty ),*
}
};
}
AddAuthenticationFields! {
pub struct EmptyParams<'a> {}
}
AddAuthenticationFields! {
pub struct GetObjs<'a> {
pub region_id: u16,
} } The first one However the second one, Is there a way to support this? Maybe I need to implement a trait? If the answer is no then I think it's probably less error prone for me to parse it by hand using Cheers and Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think what you are looking for is called "flatten" in Serde world. Actually you can do this already with responses (e.g. for JSON) see: https://serde.rs/attr-flatten.html Unfortunately this logic is not (yet) implemented for FromForm, at least as far as I know. There was an attempt to implement "flatten" for Forms a couple years back, but it got stuck in Draft, see: #1916 I can't really help you with the macro implementation though, I'm just trying provide some (hopefully helpful) context. |
Beta Was this translation helpful? Give feedback.
I think what you are looking for is called "flatten" in Serde world. Actually you can do this already with responses (e.g. for JSON) see: https://serde.rs/attr-flatten.html
Unfortunately this logic is not (yet) implemented for FromForm, at least as far as I know. There was an attempt to implement "flatten" for Forms a couple years back, but it got stuck in Draft, see: #1916
I can't really help you with the macro implementation though, I'm just trying provide some (hopefully helpful) context.