Add RequestBody and ResponseBody structs.
This commit is contained in:
parent
06388333af
commit
ef3ee2d2f3
@ -6,6 +6,12 @@ pub struct Request {
|
||||
fields: Vec<RequestField>,
|
||||
}
|
||||
|
||||
impl Request {
|
||||
pub fn has_body_fields(&self) -> bool {
|
||||
self.fields.iter().any(|field| field.is_body())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Field>> for Request {
|
||||
fn from(fields: Vec<Field>) -> Self {
|
||||
let request_fields = fields.into_iter().map(|field| {
|
||||
@ -68,6 +74,25 @@ impl ToTokens for Request {
|
||||
|
||||
tokens.append("}");
|
||||
}
|
||||
|
||||
if self.has_body_fields() {
|
||||
tokens.append(quote! {
|
||||
/// Data in the request body.
|
||||
#[derive(Debug, Serialize)]
|
||||
struct RequestBody
|
||||
});
|
||||
|
||||
tokens.append("{");
|
||||
|
||||
for request_field in self.fields.iter() {
|
||||
match *request_field {
|
||||
RequestField::Body(ref field) => field.to_tokens(&mut tokens),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
tokens.append("}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,3 +103,12 @@ pub enum RequestField {
|
||||
Path(String, Field),
|
||||
Query(Field),
|
||||
}
|
||||
|
||||
impl RequestField {
|
||||
fn is_body(&self) -> bool {
|
||||
match *self {
|
||||
RequestField::Body(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,12 @@ pub struct Response {
|
||||
fields: Vec<ResponseField>,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn has_body_fields(&self) -> bool {
|
||||
self.fields.iter().any(|field| field.is_body())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Field>> for Response {
|
||||
fn from(fields: Vec<Field>) -> Self {
|
||||
let response_fields = fields.into_iter().map(|field| {
|
||||
@ -55,6 +61,25 @@ impl ToTokens for Response {
|
||||
|
||||
tokens.append("}");
|
||||
}
|
||||
|
||||
if self.has_body_fields() {
|
||||
tokens.append(quote! {
|
||||
/// Data in the response body.
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ResponseBody
|
||||
});
|
||||
|
||||
tokens.append("{");
|
||||
|
||||
for response_field in self.fields.iter() {
|
||||
match *response_field {
|
||||
ResponseField::Body(ref field) => field.to_tokens(&mut tokens),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
tokens.append("}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,3 +88,12 @@ pub enum ResponseField {
|
||||
Body(Field),
|
||||
Header(String, Field),
|
||||
}
|
||||
|
||||
impl ResponseField {
|
||||
fn is_body(&self) -> bool {
|
||||
match *self {
|
||||
ResponseField::Body(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user