use serde::Serialize;
use serde_json::{json, Value};
use std::fmt::Display;
use super::{Message, Segment};
#[cfg(feature = "cqstring")]
use super::CQMessage;
impl Message {
pub fn add_text<T>(mut self, text: T) -> Self
where
String: From<T>,
T: Serialize + Display,
{
self.push(Segment {
type_: "text".to_string(),
data: json!({ "text": text }),
});
self
}
pub fn add_at(mut self, id: &str) -> Self {
self.0.push(Segment {
type_: "at".to_string(),
data: json!({ "qq": id }),
});
self
}
pub fn add_reply(mut self, message_id: i32) -> Self {
self.0.insert(0, Segment {
type_: "reply".to_string(),
data: json!({ "id": message_id.to_string() }),
});
self
}
pub fn add_face(mut self, id: i64) -> Self {
self.0.push(Segment {
type_: "face".to_string(),
data: json!({ "id": id.to_string() }),
});
self
}
pub fn add_image(mut self, file: &str) -> Self {
self.0.push(Segment {
type_: "image".to_string(),
data: json!({ "file": file }),
});
self
}
pub fn add_segment<T>(mut self, segment: T) -> Self
where
Value: From<T>,
T: Serialize,
{
let value = Value::from(segment);
if let Ok(segment) = serde_json::from_value(value) {
self.0.push(segment);
}
self
}
}
#[cfg(feature = "cqstring")]
impl CQMessage {
pub fn add_text<T>(mut self, text: T) -> Self
where
String: From<T>,
T: Serialize + Display,
{
self.0.push_str(&format!("[CQ:text,text={}]", text));
self
}
pub fn add_at(mut self, id: &str) -> Self {
self.0.push_str(&format!("[CQ:at,qq={}]", id));
self
}
pub fn add_reply(mut self, message_id: i32) -> Self {
self.0
.insert_str(0, &format!("[CQ:reply,id={}]", message_id));
self
}
pub fn add_face(mut self, id: i64) -> Self {
self.0.push_str(&format!("[CQ:face,id={}]", id));
self
}
pub fn add_image(mut self, file: &str) -> Self {
self.0.push_str(&format!("[CQ:image,file={}]", file));
self
}
pub fn add_segment<T>(mut self, segment: T) -> Self
where
Value: From<T>,
T: Serialize,
{
let value = Value::from(segment);
if let Ok(segment) = serde_json::from_value::<Segment>(value) {
self.0.push_str(&super::parse_cq_code(&segment));
}
self
}
}