Replaced From<T> with from! for BaseResponse
This commit is contained in:
parent
cd558eebfa
commit
10d8535b27
@ -4,7 +4,7 @@ use serde::Serialize;
|
|||||||
pub struct BaseResponse<T: Serialize> {
|
pub struct BaseResponse<T: Serialize> {
|
||||||
pub version: String,
|
pub version: String,
|
||||||
#[serde(flatten)]
|
#[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<T: Serialize> BaseResponse<T> {
|
impl<T: Serialize> BaseResponse<T> {
|
||||||
@ -16,10 +16,11 @@ impl<T: Serialize> BaseResponse<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Serialize> From<T> for BaseResponse<T> {
|
#[macro_export]
|
||||||
fn from(body: T) -> Self {
|
macro_rules! from {
|
||||||
Self::new(env!("CARGO_PKG_VERSION"), body)
|
($body:expr) => {
|
||||||
}
|
BaseResponse::new(env!("CARGO_PKG_VERSION"), $body)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -41,4 +42,14 @@ mod tests {
|
|||||||
);
|
);
|
||||||
assert_eq!(response.body.message, "Hi".to_string());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user