use serde::{Deserialize, Serialize};
use crate::field_access;
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Request {
content: String,
}
impl Request {
pub const fn new() -> Self {
Self {
content: String::new(),
}
}
}
field_access! {
Request {
content: cow { str },
}
}
impl Default for Request {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Display for Request {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
serde_json::to_string(self)
.map_err(|_| std::fmt::Error)
.and_then(|s| write!(f, "{s}"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_request() {
let req = Request::new().with_content("test message");
let req_json = req.to_string();
assert_eq!(serde_json::to_string(&req).unwrap(), req_json);
assert_eq!(serde_json::from_str::<Request>(&req_json).unwrap(), req);
}
}