use serde::{Deserialize, Serialize};
use crate::{field_access, ReactionType};
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Request {
message_id: u64,
emoji_name: String,
emoji_code: Option<String>,
reaction_type: Option<ReactionType>,
}
impl Request {
pub const fn new() -> Self {
Self {
message_id: 0,
emoji_name: String::new(),
emoji_code: None,
reaction_type: None,
}
}
}
impl Default for Request {
fn default() -> Self {
Self::new()
}
}
field_access! {
Request {
message_id: u64,
}
}
field_access! {
Request {
emoji_name: cow { str },
}
}
field_access! {
Request {
emoji_code: option_cow_deref { str, String },
}
}
field_access! {
Request {
reaction_type: option { ReactionType },
}
}
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_minimal() {
let message_id = 1;
let emoji_name = "+1";
let req = Request::new()
.with_message_id(message_id)
.with_emoji_name(emoji_name);
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);
assert_eq!(req.message_id(), message_id);
assert_eq!(req.emoji_name(), emoji_name);
assert_eq!(req.emoji_code(), None);
assert_eq!(req.reaction_type(), None);
}
#[test]
fn test_request_full() {
let message_id = 1;
let emoji_name = "+1";
let emoji_code = "U+1F44D";
let reaction_type = ReactionType::UnicodeEmoji;
let req = Request::new()
.with_message_id(message_id)
.with_emoji_name(emoji_name)
.with_emoji_code(emoji_code)
.with_reaction_type(reaction_type);
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);
assert_eq!(req.message_id(), message_id);
assert_eq!(req.emoji_name(), emoji_name);
assert_eq!(req.emoji_code(), Some(emoji_code));
assert_eq!(req.reaction_type(), Some(reaction_type));
}
}