diff --git a/src/serde/response.rs b/src/serde/response.rs index e759d34..b27cf34 100644 --- a/src/serde/response.rs +++ b/src/serde/response.rs @@ -4,7 +4,7 @@ use serde::Serialize; pub struct BaseResponse { pub version: String, #[serde(flatten)] - pub body: T, // T must be a struct (or enum?) + pub body: T, // T must be a struct (or enum?) TODO from! macro that validates T on compile time } impl BaseResponse { @@ -16,10 +16,11 @@ impl BaseResponse { } } -impl From for BaseResponse { - fn from(body: T) -> Self { - Self::new(env!("CARGO_PKG_VERSION"), body) - } +#[macro_export] +macro_rules! from { + ($body:expr) => { + BaseResponse::new(env!("CARGO_PKG_VERSION"), $body) + }; } #[cfg(test)] @@ -41,4 +42,14 @@ mod tests { ); assert_eq!(response.body.message, "Hi".to_string()); } + + #[test] + fn test_from_macro() { + let response = from!(Response { + message: "Hi".to_string(), + }); + from!(1); // Should not be allowed + assert_eq!(response.version, env!("CARGO_PKG_VERSION")); + assert_eq!(response.body.message, "Hi".to_string()); + } }