From c0cbf635f145de7ad9b8ca860d50184901fed1d8 Mon Sep 17 00:00:00 2001 From: Itsusinn Date: Sun, 10 Jul 2022 16:48:59 +0800 Subject: [PATCH] style: cargo fmt --- rustfmt.toml | 23 ++++++++++++++++++++++- src/bot.rs | 18 +++++++++++------- src/command.rs | 12 ++++++------ src/config.rs | 2 ++ src/dispatch.rs | 3 +-- src/ext/db.rs | 9 +++++++++ src/ext/mod.rs | 3 +-- src/main.rs | 2 +- src/message/handlers/receive.rs | 20 +++++++++++--------- src/message/handlers/send.rs | 29 ++++++++++++++++++----------- 10 files changed, 82 insertions(+), 39 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index 6f2e075..31e1e27 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1,22 @@ -tab_spaces = 2 \ No newline at end of file +unstable_features = true + + +version = "Two" + +tab_spaces = 2 + +group_imports = "StdExternalCrate" +imports_granularity = "Crate" +reorder_imports = true + +wrap_comments = true +normalize_comments = true + +reorder_impl_items = true +condense_wildcard_suffixes = true +enum_discrim_align_threshold = 20 +use_field_init_shorthand = true + +format_strings = true +format_code_in_doc_comments = true +format_macro_matchers = true diff --git a/src/bot.rs b/src/bot.rs index dea2af1..b5dc87a 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,21 +1,20 @@ -use crate::message::handlers; -use crate::{command::Command, config::CONFIG}; +use std::ops::Deref; + use arcstr::ArcStr; use lateinit::LateInit; use mesagisto_client::{cache::CACHE, net::NET, res::RES}; -use std::ops::Deref; -use teloxide::payloads::SendAnimationSetters; -use teloxide::types::Message; use teloxide::{ adaptors::{AutoSend, DefaultParseMode}, - payloads::{SendMessageSetters, SendPhotoSetters}, + payloads::{SendAnimationSetters, SendMessageSetters, SendPhotoSetters}, prelude::Requester, - types::{File as TgFile, InputFile}, + types::{File as TgFile, InputFile, Message}, utils::command::BotCommands, Bot, }; use teloxide_core::types::ChatId; use tracing::warn; + +use crate::{command::Command, config::CONFIG, message::handlers}; pub type BotRequester = AutoSend>; #[derive(Singleton, Default)] @@ -28,6 +27,7 @@ impl TgBot { self.inner.init(bot); Ok(()) } + // fixme use this-error pub async fn file(&self, uid: &Vec, id: &Vec) -> anyhow::Result<()> { let id_str: ArcStr = base64_url::encode(id).into(); @@ -41,6 +41,7 @@ impl TgBot { CACHE.put_file(uid, &tmp_path).await?; Ok(()) } + pub fn get_url_by_path(&self, file_path: String) -> ArcStr { format!( "https://api.telegram.org/file/bot{token}/{file}", @@ -49,6 +50,7 @@ impl TgBot { ) .into() } + pub async fn send_text( &self, chat_id: ChatId, @@ -88,6 +90,7 @@ impl TgBot { }, } } + pub async fn send_image( &self, chat_id: ChatId, @@ -157,6 +160,7 @@ impl TgBot { impl Deref for TgBot { type Target = BotRequester; + fn deref(&self) -> &Self::Target { &self.inner } diff --git a/src/command.rs b/src/command.rs index 56e15f2..cca65c2 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,10 +1,10 @@ -use crate::bot::BotRequester; -use crate::config::CONFIG; -use crate::message::handlers; use arcstr::ArcStr; -use teloxide::prelude::*; -use teloxide::utils::command::BotCommands; -use teloxide::utils::html; +use teloxide::{ + prelude::*, + utils::{command::BotCommands, html}, +}; + +use crate::{bot::BotRequester, config::CONFIG, message::handlers}; #[derive(BotCommands, Clone)] #[command(rename = "lowercase", description = "信使Bot支持以下命令")] diff --git a/src/config.rs b/src/config.rs index 187bc10..f07b9fd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,12 +22,14 @@ impl Config { pub fn mapper(&self, target: &i64) -> Option { self.bindings.get(target).map(|v| v.clone()) } + pub fn migrate(&self) { for pair in &self.target_address_mapper { self.bindings.insert(*pair.key(), pair.value().clone()); } self.target_address_mapper.clear(); } + pub fn migrate_chat(&self, old_chat_id: &i64, new_chat_id: &i64) -> Option { if let Some((_, address)) = self.bindings.remove(old_chat_id) { self.bindings.insert(*new_chat_id, address.clone()); diff --git a/src/dispatch.rs b/src/dispatch.rs index e1d96ad..6f624c9 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -1,8 +1,7 @@ use teloxide::prelude::*; use tracing::info; -use crate::bot::BotRequester; -use crate::{command::Command, message::handlers}; +use crate::{bot::BotRequester, command::Command, message::handlers}; pub async fn start(bot: &BotRequester) { let message_handler = Update::filter_message() diff --git a/src/ext/db.rs b/src/ext/db.rs index 7c8b75f..d60a178 100644 --- a/src/ext/db.rs +++ b/src/ext/db.rs @@ -29,6 +29,7 @@ impl DbExt for Db { true, ) } + // no reverse #[inline] fn put_msg_id_ir_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()> { @@ -39,6 +40,7 @@ impl DbExt for Db { false, ) } + #[inline] fn put_msg_id_1(&self, target: &i64, uid: &[u8], id: &i32) -> anyhow::Result<()> { self.put_msg_id( @@ -48,6 +50,7 @@ impl DbExt for Db { true, ) } + #[inline] fn put_msg_id_ir_1(&self, target: &i64, uid: &[u8], id: &i32) -> anyhow::Result<()> { self.put_msg_id( @@ -57,6 +60,7 @@ impl DbExt for Db { false, ) } + #[inline] fn put_msg_id_2(&self, target: &i64, uid: &i32, id: &[u8]) -> anyhow::Result<()> { self.put_msg_id( @@ -66,6 +70,7 @@ impl DbExt for Db { true, ) } + #[inline] fn put_msg_id_ir_2(&self, target: &i64, uid: &i32, id: &[u8]) -> anyhow::Result<()> { self.put_msg_id( @@ -75,6 +80,7 @@ impl DbExt for Db { false, ) } + #[inline] fn put_msg_id_3(&self, target: &u64, uid: &u64, id: &[u8]) -> anyhow::Result<()> { self.put_msg_id( @@ -84,6 +90,7 @@ impl DbExt for Db { true, ) } + #[inline] fn put_msg_id_ir_3(&self, target: &u64, uid: &u64, id: &[u8]) -> anyhow::Result<()> { self.put_msg_id( @@ -93,6 +100,7 @@ impl DbExt for Db { false, ) } + #[inline] fn get_msg_id_1(&self, target: &i64, id: &[u8]) -> anyhow::Result> { let be_bytes = match self.get_msg_id(target.to_be_bytes().as_ref(), id)? { @@ -104,6 +112,7 @@ impl DbExt for Db { }; i32::from_be_bytes(be_bytes.try_into().unwrap()).some().ok() } + #[inline] fn get_msg_id_2(&self, target: &i64, id: &[u8]) -> anyhow::Result>> { self.get_msg_id(target.to_be_bytes().as_ref(), id) diff --git a/src/ext/mod.rs b/src/ext/mod.rs index 2aa239b..4e9df1b 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -1,5 +1,4 @@ -use arcstr::ArcStr; -use arcstr::Substr; +use arcstr::{ArcStr, Substr}; pub mod db; pub mod err; diff --git a/src/main.rs b/src/main.rs index e6bae49..8e58c20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![allow(incomplete_features)] #![feature(backtrace, capture_disjoint_fields, let_chains)] +use bot::TG_BOT; use futures::FutureExt; use mesagisto_client::MesagistoConfig; use teloxide::{prelude::*, types::ParseMode, Bot}; @@ -8,7 +9,6 @@ use tracing::*; use self::message::handlers; use crate::config::{Config, CONFIG}; -use bot::TG_BOT; #[macro_use] extern crate educe; diff --git a/src/message/handlers/receive.rs b/src/message/handlers/receive.rs index 14f58ca..7301c33 100644 --- a/src/message/handlers/receive.rs +++ b/src/message/handlers/receive.rs @@ -1,21 +1,23 @@ -use crate::ext::db::DbExt; -use crate::ext::err::LogResultExt; -use crate::CONFIG; -use crate::TG_BOT; use arcstr::ArcStr; use lateinit::LateInit; use mesagisto_client::{ cache::CACHE, - data::{message::Message, message::MessageType, Packet}, + data::{ + message::{Message, MessageType}, + Packet, + }, db::DB, server::SERVER, }; - -use teloxide::types::ChatId; -use teloxide::utils::html; +use teloxide::{types::ChatId, utils::html}; use tokio::sync::mpsc::UnboundedSender; use tracing::trace; +use crate::{ + ext::{db::DbExt, err::LogResultExt}, + CONFIG, TG_BOT, +}; + static CHANNEL: LateInit> = LateInit::new(); pub fn recover() -> anyhow::Result<()> { @@ -59,7 +61,7 @@ pub async fn server_msg_handler(message: nats::Message, target: ArcStr) -> anyho let packet = match packet { Ok(v) => v, Err(_e) => { - //todo logging + // todo logging tracing::warn!("未知的数据包类型,请更新本消息源,若已是最新请等待适配"); return Ok(()); } diff --git a/src/message/handlers/send.rs b/src/message/handlers/send.rs index 62a2af4..6f4b6c5 100644 --- a/src/message/handlers/send.rs +++ b/src/message/handlers/send.rs @@ -1,16 +1,23 @@ -use crate::bot::{BotRequester, TG_BOT}; -use crate::config::CONFIG; -use crate::ext::db::DbExt; - use arcstr::ArcStr; -use mesagisto_client::data::message::{MessageType, Profile}; -use mesagisto_client::data::{message, Packet}; -use mesagisto_client::db::DB; -use mesagisto_client::res::RES; -use mesagisto_client::server::SERVER; -use mesagisto_client::EitherExt; +use mesagisto_client::{ + data::{ + message, + message::{MessageType, Profile}, + Packet, + }, + db::DB, + res::RES, + server::SERVER, + EitherExt, +}; use teloxide::prelude::*; +use crate::{ + bot::{BotRequester, TG_BOT}, + config::CONFIG, + ext::db::DbExt, +}; + pub async fn answer_common(msg: Message, _bot: BotRequester) -> anyhow::Result<()> { let target = msg.chat.id.0; if !CONFIG.bindings.contains_key(&target) { @@ -19,7 +26,7 @@ pub async fn answer_common(msg: Message, _bot: BotRequester) -> anyhow::Result<( let address = CONFIG.bindings.get(&target).unwrap().clone(); let sender = match msg.from() { Some(v) => v, - //fixme + // fixme None => return Ok(()), }; if sender.is_bot {