feat: allow any serializable object as response

This commit is contained in:
Valerio Ageno
2024-05-19 20:32:19 +02:00
parent 208c18c94a
commit 4ed6c9902f
5 changed files with 27 additions and 13 deletions
+7 -6
View File
@@ -1,24 +1,25 @@
use serde::Serialize;
use erased_serde::Serialize;
use serde::Serialize as SerdeSerialize;
use crate::request::Location;
use crate::Request;
#[derive(Serialize)]
#[derive(SerdeSerialize)]
/// This is the data shared to the client
pub struct Payload<'a> {
router: Location<'a>,
props: String,
props: Box<dyn Serialize>,
}
impl<'a> Payload<'a> {
pub fn new(req: &Request<'a>, props: String) -> Payload<'a> {
pub fn new(req: &Request<'a>, props: Box<dyn Serialize>) -> Payload<'a> {
Payload {
router: req.location(),
props,
}
}
pub fn client_payload(&self) -> String {
serde_json::to_string(&self).unwrap()
pub fn client_payload(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(&self)
}
}