use serde::{Deserialize, Serialize};
use crate::{EmptyContent, Event};
pub type ActionResps = ActionResp<ActionRespContent>;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ActionResp<T> {
pub status: String,
pub retcode: i64,
pub data: T,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum ActionRespContent {
SendMessage(SendMessageRespContent),
LatestEvents(Vec<Event>),
SupportActions(Vec<String>),
Status(StatusContent),
Version(VersionContent),
UserInfo(UserInfoContent),
FriendList(Vec<UserInfoContent>),
GroupInfo(GroupInfoContent),
GroupList(Vec<GroupInfoContent>),
FileId(String),
PrepareFileFragmented(FileFragmentedHead),
TransferFileFragmented(Vec<u8>),
Empty(EmptyContent), }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum ExtendedActionRespContent<T> {
Standard(ActionRespContent),
Extended(T),
}
impl ActionRespContent {
pub fn empty() -> Self {
Self::Empty(EmptyContent::default())
}
}
impl<T> ActionResp<T> {
#[allow(dead_code)]
pub fn success(data: T) -> Self {
ActionResp {
status: "ok".to_owned(),
retcode: 0,
data,
message: "".to_owned(),
}
}
#[allow(dead_code)]
pub fn fail(data: T, retcode: i64, message: String) -> Self {
ActionResp {
status: "failed".to_owned(),
retcode,
data,
message,
}
}
#[allow(dead_code)]
pub fn tired(data: T) -> Self {
Self::fail(data, 36000, "I Am Tired!".to_owned())
}
}
macro_rules! empty_err_resp {
($fn_name: ident, $retcode: expr, $message: expr) => {
#[allow(dead_code)]
pub fn $fn_name() -> Self {
Self::empty_fail($retcode, $message.to_owned())
}
};
}
impl ActionResp<ActionRespContent> {
#[allow(dead_code)]
pub fn empty_success() -> Self {
Self::success(ActionRespContent::Empty(EmptyContent::default()))
}
#[allow(dead_code)]
pub fn empty_fail(retcode: i64, message: String) -> Self {
Self::fail(
ActionRespContent::Empty(EmptyContent::default()),
retcode,
message,
)
}
empty_err_resp!(bad_request, 10001, "无效的动作请求");
empty_err_resp!(unsupported_action, 10002, "不支持的动作请求");
empty_err_resp!(bad_param, 10003, "无效的动作请求参数");
empty_err_resp!(unsupported_param, 10004, "不支持的动作请求参数");
empty_err_resp!(unsupported_segment, 10005, "不支持的消息段类型");
empty_err_resp!(bad_segment_data, 10006, "无效的消息段参数");
empty_err_resp!(unsupported_segment_data, 10007, "不支持的消息段参数");
}
#[cfg(feature = "echo")]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct EchoActionResp<T> {
pub status: String,
pub retcode: i64,
pub data: T,
pub message: String,
pub echo: String,
}
pub type LatestEvents = ActionResp<Vec<Event>>;
pub type SupportActions = ActionResp<Vec<String>>;
pub type Status = ActionResp<StatusContent>;
pub type Version = ActionResp<VersionContent>;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct StatusContent {
pub good: bool,
pub online: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VersionContent {
pub r#impl: String,
pub platform: String,
pub version: String,
pub onebot_version: String,
}
impl Default for VersionContent {
fn default() -> Self {
VersionContent {
r#impl: "AbrasOneBot".to_owned(),
platform: "RustOneBot".to_owned(),
version: "0.0.1".to_owned(),
onebot_version: "12".to_owned(),
}
}
}
pub type SendMessageResp = ActionResp<SendMessageRespContent>;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SendMessageRespContent {
pub message_id: String,
pub time: i64,
}
pub type UserInfo = ActionResp<UserInfoContent>;
pub type FriendList = ActionResp<Vec<UserInfoContent>>;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UserInfoContent {
pub user_id: String,
pub nickname: String,
}
pub type GroupInfo = ActionResp<GroupInfoContent>;
pub type GroupList = ActionResp<Vec<GroupInfoContent>>;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GroupInfoContent {
pub group_id: String,
pub group_name: String,
}
pub type FileId = ActionResp<String>;
pub type PrepareFileFragmented = ActionResp<FileFragmentedHead>;
pub type TransferFileFragmented = ActionResp<Vec<u8>>;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FileContent {
pub name: String,
pub url: Option<String>,
pub headers: Option<std::collections::HashMap<String, String>>,
pub path: Option<String>,
pub data: Option<Vec<u8>>,
pub sha256: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FileFragmentedHead {
pub name: String,
pub total_size: i64,
pub sha256: String,
}