From 653c87e64f37f59a8e8d3132bbbcc43a9ad9895a Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 01:45:03 +0900 Subject: [PATCH 1/9] =?UTF-8?q?[#15]=20serde=20re-export=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rupring/src/lib.rs b/rupring/src/lib.rs index 4370ccb..4a0f826 100644 --- a/rupring/src/lib.rs +++ b/rupring/src/lib.rs @@ -618,3 +618,6 @@ impl RupringFactory { #[cfg(test)] mod test_proc_macro; + +pub use serde; +pub use serde_json; From 11da7e1436258dc488e7c5771be0b28b7cc965be Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 01:50:04 +0900 Subject: [PATCH 2/9] =?UTF-8?q?[#15]=20Json=20=EC=A7=81=EB=A0=AC=ED=99=94/?= =?UTF-8?q?=EC=97=AD=EC=A7=81=EB=A0=AC=ED=99=94=EC=9A=A9=20=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=EC=B2=B4=20=EC=BD=94=EB=93=9C=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring_macro/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rupring_macro/src/lib.rs b/rupring_macro/src/lib.rs index bf8cae1..68f0f9e 100644 --- a/rupring_macro/src/lib.rs +++ b/rupring_macro/src/lib.rs @@ -633,6 +633,11 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { code += format!(r#"query_parameters: vec![],"#).as_str(); code += "};"; + let mut define_struct_for_json = "".to_string(); + define_struct_for_json += + format!(r#"#[derive(rupring::serde::Serialize, rupring::serde::Deserialize)]"#).as_str(); + define_struct_for_json += format!(r#"pub struct {struct_name}__JSON {{"#).as_str(); + for field in ast.fields.iter() { let mut description = "".to_string(); let mut example = r#""""#.to_string(); @@ -791,6 +796,13 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { continue; } + define_struct_for_json += format!( + r#" + pub {field_name}: {field_type}, + "# + ) + .as_str(); + // Body 파라미터 생성 구현 code += format!(r#"let property_of_type = {field_type}::to_swagger_definition(context);"#) .as_str(); @@ -831,11 +843,15 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { .as_str(); } + define_struct_for_json += format!(r#"}}"#).as_str(); + code += "rupring::swagger::macros::SwaggerDefinitionNode::Object(swagger_definition)"; code += "}"; code += "}"; + code += define_struct_for_json.as_str(); + return TokenStream::from_str(code.as_str()).unwrap(); } From 4c6409ff7154730e1eb4ed4a826bd9e9b15314e8 Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 02:21:34 +0900 Subject: [PATCH 3/9] =?UTF-8?q?[#15]=20body=20bind=20=EB=B0=8F=20path=20pa?= =?UTF-8?q?ram=20bind=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring/src/lib.rs | 1 + rupring/src/request.rs | 6 +++++ rupring_macro/src/lib.rs | 49 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/rupring/src/lib.rs b/rupring/src/lib.rs index 4a0f826..d592538 100644 --- a/rupring/src/lib.rs +++ b/rupring/src/lib.rs @@ -619,5 +619,6 @@ impl RupringFactory { #[cfg(test)] mod test_proc_macro; +pub use anyhow; pub use serde; pub use serde_json; diff --git a/rupring/src/request.rs b/rupring/src/request.rs index aad93a5..3a15fd1 100644 --- a/rupring/src/request.rs +++ b/rupring/src/request.rs @@ -13,6 +13,12 @@ pub struct Request { pub(crate) di_context: Arc, } +pub trait BindFromRequest { + fn bind(&mut self, request: Request) -> anyhow::Result + where + Self: Sized; +} + impl UnwindSafe for Request {} impl Request { diff --git a/rupring_macro/src/lib.rs b/rupring_macro/src/lib.rs index 68f0f9e..ce448d4 100644 --- a/rupring_macro/src/lib.rs +++ b/rupring_macro/src/lib.rs @@ -638,6 +638,10 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { format!(r#"#[derive(rupring::serde::Serialize, rupring::serde::Deserialize)]"#).as_str(); define_struct_for_json += format!(r#"pub struct {struct_name}__JSON {{"#).as_str(); + let mut json_field_names = vec![]; + let mut path_field_names = vec![]; + let mut query_field_names = vec![]; + for field in ast.fields.iter() { let mut description = "".to_string(); let mut example = r#""""#.to_string(); @@ -759,6 +763,8 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { } if is_path_parameter { + path_field_names.push(field_name.clone()); + code += format!( r#"swagger_definition.path_parameters.push(rupring::swagger::json::SwaggerParameter {{ name: "{field_name}".to_string(), @@ -778,6 +784,8 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { } if is_query_parameter { + query_field_names.push(field_name.clone()); + code += format!( r#"swagger_definition.query_parameters.push(rupring::swagger::json::SwaggerParameter {{ name: "{field_name}".to_string(), @@ -796,6 +804,8 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { continue; } + json_field_names.push(field_name.clone()); + define_struct_for_json += format!( r#" pub {field_name}: {field_type}, @@ -853,5 +863,44 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { code += define_struct_for_json.as_str(); + let mut request_bind_code = "".to_string(); + request_bind_code += + format!(r#"impl rupring::request::BindFromRequest for {struct_name} {{"#).as_str(); + + request_bind_code += + "fn bind(&mut self, request: rupring::request::Request) -> rupring::anyhow::Result {"; + request_bind_code += "use rupring::ParamStringDeserializer;"; + + request_bind_code += format!("let mut json_bound = rupring::serde_json::from_str::<{struct_name}__JSON>(request.body.as_str())?;").as_str(); + + request_bind_code += format!("let mut bound = {struct_name} {{").as_str(); + + for field_name in json_field_names { + request_bind_code += format!("{field_name}: json_bound.{field_name},").as_str(); + } + + for field_name in path_field_names { + request_bind_code += format!( + r#"{field_name}: rupring::ParamString( + request.path_parameters["{field_name}"].clone() + ). + deserialize(). + map_err( + |_|Err(rupring::anyhow::anyhow!("{field_name} is invalid")) + )?, + "# + ) + .as_str(); + } + + request_bind_code += format!("}};").as_str(); + + request_bind_code += "Ok(bound)"; + request_bind_code += "}"; + + request_bind_code += format!(r#"}}"#).as_str(); + + code += request_bind_code.as_str(); + return TokenStream::from_str(code.as_str()).unwrap(); } From f5ca56ef2d7608ef9dcedb95026e7b916bdb2c7b Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 02:23:13 +0900 Subject: [PATCH 4/9] =?UTF-8?q?[#15]=20QueryStringDeserializer=20trait=20?= =?UTF-8?q?=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring/src/request.rs | 9 +++++++++ rupring_macro/src/lib.rs | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/rupring/src/request.rs b/rupring/src/request.rs index 3a15fd1..9c2f7d4 100644 --- a/rupring/src/request.rs +++ b/rupring/src/request.rs @@ -27,6 +27,15 @@ impl Request { } } +#[derive(Debug, Clone)] +pub struct QueryString(pub Vec); + +pub trait QueryStringDeserializer: Sized { + type Error; + + fn deserialize(&self) -> Result; +} + #[derive(Debug, Clone)] pub struct ParamString(pub String); diff --git a/rupring_macro/src/lib.rs b/rupring_macro/src/lib.rs index ce448d4..e6d263d 100644 --- a/rupring_macro/src/lib.rs +++ b/rupring_macro/src/lib.rs @@ -893,6 +893,20 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { .as_str(); } + for field_name in query_field_names { + request_bind_code += format!( + r#"{field_name}: rupring::ParamString( + request.query_parameters["{field_name}"].clone() + ). + deserialize(). + map_err( + |_|Err(rupring::anyhow::anyhow!("{field_name} is invalid")) + )?, + "# + ) + .as_str(); + } + request_bind_code += format!("}};").as_str(); request_bind_code += "Ok(bound)"; From fccf21961f787d7b375835b72a79c8f5ef146a13 Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 02:24:03 +0900 Subject: [PATCH 5/9] [#15] impl QueryStringDeserializer> for QueryString --- rupring/src/request.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rupring/src/request.rs b/rupring/src/request.rs index 9c2f7d4..03aee3f 100644 --- a/rupring/src/request.rs +++ b/rupring/src/request.rs @@ -36,6 +36,21 @@ pub trait QueryStringDeserializer: Sized { fn deserialize(&self) -> Result; } +impl QueryStringDeserializer> for QueryString +where + QueryString: QueryStringDeserializer, +{ + type Error = (); + + fn deserialize(&self) -> Result, Self::Error> { + let result = Self::deserialize(self); + match result { + Ok(v) => Ok(Some(v)), + Err(_) => Ok(None), + } + } +} + #[derive(Debug, Clone)] pub struct ParamString(pub String); From 15fef4b17702c8fb883f75c8879bb77c0c74df3d Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 02:27:26 +0900 Subject: [PATCH 6/9] =?UTF-8?q?[#15]=20impl=20QueryString=20=EC=A0=95?= =?UTF-8?q?=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring/src/request.rs | 144 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/rupring/src/request.rs b/rupring/src/request.rs index 03aee3f..bf32f5a 100644 --- a/rupring/src/request.rs +++ b/rupring/src/request.rs @@ -51,6 +51,150 @@ where } } +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + #[derive(Debug, Clone)] pub struct ParamString(pub String); From d4c90ba8f3e4d69412ad782a40acf6fe6814a7bf Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 02:27:39 +0900 Subject: [PATCH 7/9] =?UTF-8?q?[#15]=20impl=20QueryString=20=EC=8B=A4?= =?UTF-8?q?=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring/src/request.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rupring/src/request.rs b/rupring/src/request.rs index bf32f5a..81766c9 100644 --- a/rupring/src/request.rs +++ b/rupring/src/request.rs @@ -195,6 +195,30 @@ impl QueryStringDeserializer for QueryString { } } +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + #[derive(Debug, Clone)] pub struct ParamString(pub String); From c86ef1aa79b7d3f52d669fabed7f66c4bfcfb4bb Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 02:27:50 +0900 Subject: [PATCH 8/9] =?UTF-8?q?[#15]=20impl=20QueryString=20bool,=20String?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring/src/request.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rupring/src/request.rs b/rupring/src/request.rs index 81766c9..2414e14 100644 --- a/rupring/src/request.rs +++ b/rupring/src/request.rs @@ -219,6 +219,30 @@ impl QueryStringDeserializer for QueryString { } } +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + e.parse::().map_err(|_| ()) + } else { + Err(()) + } + } +} + +impl QueryStringDeserializer for QueryString { + type Error = (); + + fn deserialize(&self) -> Result { + if let Some(e) = self.0.get(0) { + Ok(e.clone()) + } else { + Err(()) + } + } +} + #[derive(Debug, Clone)] pub struct ParamString(pub String); From 63b88359841acf79dae8a0b818a74748cdf64fec Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 27 Aug 2024 03:21:25 +0900 Subject: [PATCH 9/9] =?UTF-8?q?[#15]=20BindFromRequest=20trait=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=20=EC=83=9D=EC=84=B1=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring/src/request.rs | 38 ++++++++++++------------- rupring_macro/src/lib.rs | 61 ++++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 43 deletions(-) diff --git a/rupring/src/request.rs b/rupring/src/request.rs index 2414e14..55b3c1f 100644 --- a/rupring/src/request.rs +++ b/rupring/src/request.rs @@ -33,7 +33,7 @@ pub struct QueryString(pub Vec); pub trait QueryStringDeserializer: Sized { type Error; - fn deserialize(&self) -> Result; + fn deserialize_query_string(&self) -> Result; } impl QueryStringDeserializer> for QueryString @@ -42,8 +42,8 @@ where { type Error = (); - fn deserialize(&self) -> Result, Self::Error> { - let result = Self::deserialize(self); + fn deserialize_query_string(&self) -> Result, Self::Error> { + let result = Self::deserialize_query_string(self); match result { Ok(v) => Ok(Some(v)), Err(_) => Ok(None), @@ -54,7 +54,7 @@ where impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -66,7 +66,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -78,7 +78,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -90,7 +90,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -102,7 +102,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -114,7 +114,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -126,7 +126,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -138,7 +138,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -150,7 +150,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -162,7 +162,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -174,7 +174,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -186,7 +186,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -198,7 +198,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -210,7 +210,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -222,7 +222,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { e.parse::().map_err(|_| ()) } else { @@ -234,7 +234,7 @@ impl QueryStringDeserializer for QueryString { impl QueryStringDeserializer for QueryString { type Error = (); - fn deserialize(&self) -> Result { + fn deserialize_query_string(&self) -> Result { if let Some(e) = self.0.get(0) { Ok(e.clone()) } else { diff --git a/rupring_macro/src/lib.rs b/rupring_macro/src/lib.rs index e6d263d..0e1dc87 100644 --- a/rupring_macro/src/lib.rs +++ b/rupring_macro/src/lib.rs @@ -649,6 +649,8 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { let mut field_name = field.ident.as_ref().unwrap().to_string(); let mut field_type = field.ty.to_token_stream().to_string().replace(" ", ""); + let original_field_name = field_name.clone(); + let attributes = field.attrs.clone(); let mut is_required = true; @@ -763,7 +765,7 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { } if is_path_parameter { - path_field_names.push(field_name.clone()); + path_field_names.push((original_field_name.clone(), field_type.clone())); code += format!( r#"swagger_definition.path_parameters.push(rupring::swagger::json::SwaggerParameter {{ @@ -784,7 +786,7 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { } if is_query_parameter { - query_field_names.push(field_name.clone()); + query_field_names.push((original_field_name.clone(), field_type.clone())); code += format!( r#"swagger_definition.query_parameters.push(rupring::swagger::json::SwaggerParameter {{ @@ -804,11 +806,11 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { continue; } - json_field_names.push(field_name.clone()); + json_field_names.push((original_field_name.clone(), field_type.clone())); define_struct_for_json += format!( r#" - pub {field_name}: {field_type}, + pub {original_field_name}: {field_type}, "# ) .as_str(); @@ -869,39 +871,50 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream { request_bind_code += "fn bind(&mut self, request: rupring::request::Request) -> rupring::anyhow::Result {"; - request_bind_code += "use rupring::ParamStringDeserializer;"; + request_bind_code += "use rupring::request::ParamStringDeserializer;"; + request_bind_code += "use rupring::request::QueryStringDeserializer;"; - request_bind_code += format!("let mut json_bound = rupring::serde_json::from_str::<{struct_name}__JSON>(request.body.as_str())?;").as_str(); + request_bind_code += format!("let mut json_bound = rupring::serde_json::from_str::<{struct_name}__JSON>(request.body.as_str()).unwrap();").as_str(); - request_bind_code += format!("let mut bound = {struct_name} {{").as_str(); + request_bind_code += format!("let bound = {struct_name} {{").as_str(); - for field_name in json_field_names { + for (field_name, _) in json_field_names { request_bind_code += format!("{field_name}: json_bound.{field_name},").as_str(); } - for field_name in path_field_names { + for (field_name, field_type) in path_field_names { request_bind_code += format!( - r#"{field_name}: rupring::ParamString( - request.path_parameters["{field_name}"].clone() - ). - deserialize(). - map_err( - |_|Err(rupring::anyhow::anyhow!("{field_name} is invalid")) - )?, + r#"{field_name}: {{ + let param = rupring::request::ParamString( + request.path_parameters["{field_name}"].clone() + ); + + let deserialized: {field_type} = match param.deserialize() {{ + Ok(v) => v, + Err(_) => return Err(rupring::anyhow::anyhow!("invalid parameter: {field_name}")), + }}; + + deserialized + }} "# ) .as_str(); } - for field_name in query_field_names { + for (field_name, field_type) in query_field_names { request_bind_code += format!( - r#"{field_name}: rupring::ParamString( - request.query_parameters["{field_name}"].clone() - ). - deserialize(). - map_err( - |_|Err(rupring::anyhow::anyhow!("{field_name} is invalid")) - )?, + r#"{field_name}: {{ + let query = rupring::request::QueryString( + request.query_parameters["{field_name}"].clone() + ); + + let deserialized: {field_type} = match query.deserialize_query_string() {{ + Ok(v) => v, + Err(_) => return Err(rupring::anyhow::anyhow!("invalid parameter: {field_name}")), + }}; + + deserialized + }}, "# ) .as_str();