use serde::{Deserialize, Serialize};
use crate::filter::Anchor;
use crate::message::Message;
use crate::response::BaseResponse;
use crate::{field_access, impl_default, impl_display, impl_response};
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Response {
#[serde(flatten)]
base: BaseResponse,
#[serde(skip_serializing_if = "Option::is_none")]
anchor: Option<Anchor>,
#[serde(skip_serializing_if = "Option::is_none")]
found_newest: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
found_oldest: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
found_anchor: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
history_limited: Option<bool>,
messages: Vec<Message>,
}
impl Response {
pub const fn new() -> Self {
Self {
base: BaseResponse::new(),
anchor: None,
found_newest: None,
found_oldest: None,
found_anchor: None,
history_limited: None,
messages: Vec::new(),
}
}
}
field_access! {
Response {
anchor: option { Anchor },
found_newest: option { bool },
found_oldest: option { bool },
found_anchor: option { bool },
history_limited: option { bool },
}
}
field_access! {
Response {
messages: cow { [Message] },
}
}
impl_default!(Response);
impl_display!(Response);
impl_response!(Response, base);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_response_minimal() {
let response = Response::new();
let response_json = response.to_string();
assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
assert_eq!(
serde_json::from_str::<Response>(&response_json).unwrap(),
response
);
}
#[test]
fn test_response_full() {
let anchor = Anchor::Newest;
let found_newest = true;
let found_oldest = false;
let found_anchor = true;
let messages = [Message::new().with_content("test content").with_id(1)];
let response = Response::new()
.with_anchor(anchor)
.with_found_newest(found_newest)
.with_found_oldest(found_oldest)
.with_found_anchor(found_anchor)
.with_messages(&messages);
let response_json = response.to_string();
assert_eq!(response.anchor(), Some(anchor));
assert_eq!(response.found_newest(), Some(found_newest));
assert_eq!(response.found_oldest(), Some(found_oldest));
assert_eq!(response.found_anchor(), Some(found_anchor));
assert_eq!(response.messages(), messages.as_ref());
assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
assert_eq!(
serde_json::from_str::<Response>(&response_json).unwrap(),
response
);
}
}