From dd6afa26af70697b56bdce92fbd4cd4310f4ec0c Mon Sep 17 00:00:00 2001 From: itsusinn Date: Sun, 14 Nov 2021 15:57:30 +0800 Subject: [PATCH] style: cargo fmt --- src/bot.rs | 11 ++++++----- src/command.rs | 7 ++----- src/config.rs | 8 ++++---- src/despatch.rs | 7 ++++++- src/main.rs | 23 +++++++++++------------ src/message/handlers/receive.rs | 28 +++++++++++++++++----------- src/message/handlers/send.rs | 4 ++-- 7 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index b78212a..a858a33 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,9 +1,9 @@ use std::{ops::Deref, sync::Arc}; -use arcstr::ArcStr; use crate::config::CONFIG; -use mesagisto_client::{LateInit, cache::CACHE, res::RES}; -use teloxide::{Bot, adaptors::AutoSend, net::Download, prelude::Requester, types::File as TgFile}; +use arcstr::ArcStr; +use mesagisto_client::{cache::CACHE, res::RES, LateInit}; +use teloxide::{adaptors::AutoSend, net::Download, prelude::Requester, types::File as TgFile, Bot}; #[derive(Singleton, Default)] pub struct TgBot { @@ -23,12 +23,13 @@ impl TgBot { CACHE.put_file(uid, &tmp_path).await?; Ok(()) } - pub fn get_url_by_path(&self,file_path: String) -> ArcStr{ + pub fn get_url_by_path(&self, file_path: String) -> ArcStr { format!( "https://api.telegram.org/file/bot{token}/{file}", token = CONFIG.telegram.token, file = file_path - ).into() + ) + .into() } } impl Deref for TgBot { diff --git a/src/command.rs b/src/command.rs index b8e9ab4..fcc776d 100644 --- a/src/command.rs +++ b/src/command.rs @@ -34,7 +34,7 @@ pub async fn answer( Command::SetAddress { address } => { let sender_id = cx.update.from().unwrap().id; let chat_id = cx.chat_id(); - let admins =cx.requester.get_chat_administrators(chat_id).await?; + let admins = cx.requester.get_chat_administrators(chat_id).await?; let mut is_admin = false; for admin in admins { if admin.user.id == sender_id { @@ -43,14 +43,11 @@ pub async fn answer( } } if is_admin { - CONFIG - .target_address_mapper - .insert(chat_id, address.into()); + CONFIG.target_address_mapper.insert(chat_id, address.into()); cx.answer("成功设置当前Group的信使地址").await?; } else { cx.answer("权限不足,拒绝设置信使频道").await?; } - } }; Ok(()) diff --git a/src/config.rs b/src/config.rs index 908139a..4f7a68a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,10 +17,10 @@ pub struct Config { } impl Config { - pub fn mapper(&self,target: &i64) -> Option{ + pub fn mapper(&self, target: &i64) -> Option { match self.target_address_mapper.get(target) { - Some(v) => return Some(v.clone()), - None => return None, + Some(v) => return Some(v.clone()), + None => return None, } } } @@ -48,7 +48,7 @@ pub struct CipherConfig { #[educe(Default = "this-is-an-example-key")] pub key: String, #[educe(Default = true)] - pub refuse_plain: bool + pub refuse_plain: bool, } #[basic_derive] diff --git a/src/despatch.rs b/src/despatch.rs index 37986b3..aaba8e8 100644 --- a/src/despatch.rs +++ b/src/despatch.rs @@ -106,7 +106,12 @@ impl TracingErrorHandler { use futures::future::BoxFuture; impl ErrorHandler for TracingErrorHandler { fn handle_error(self: Arc, error: anyhow::Error) -> BoxFuture<'static, ()> { - log::error!("{}:{}, \n Backtrace {}",self.text,error,error.backtrace()); + log::error!( + "{}:{}, \n Backtrace {}", + self.text, + error, + error.backtrace() + ); Box::pin(async {}) } } diff --git a/src/main.rs b/src/main.rs index a62505f..64534c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ #![allow(incomplete_features)] -#![feature(backtrace,capture_disjoint_fields)] +#![feature(backtrace, capture_disjoint_fields)] use std::sync::Arc; use arcstr::ArcStr; use futures::FutureExt; -use mesagisto_client::{OptionExt, cache::CACHE, cipher::CIPHER, db::DB, res::RES, server::SERVER}; -use teloxide::{Bot, prelude::*}; +use mesagisto_client::{cache::CACHE, cipher::CIPHER, db::DB, res::RES, server::SERVER, OptionExt}; +use teloxide::{prelude::*, Bot}; use bot::TG_BOT; use config::CONFIG; @@ -22,10 +22,10 @@ mod bot; mod command; mod config; mod despatch; +pub mod ext; mod message; mod net; mod webhook; -pub mod ext; fn main() { std::env::set_var("RUST_BACKTRACE", "1"); @@ -39,7 +39,7 @@ fn main() { .filter(Some("teloxide"), log::LevelFilter::Info) .init(); tokio::runtime::Builder::new_multi_thread() - // fixme: how many do we need + // fixme: how many do we need .worker_threads(5) .enable_all() .build() @@ -59,7 +59,7 @@ async fn run() -> Result<(), anyhow::Error> { } CACHE.init(); if CONFIG.cipher.enable { - CIPHER.init(&CONFIG.cipher.key,&CONFIG.cipher.refuse_plain); + CIPHER.init(&CONFIG.cipher.key, &CONFIG.cipher.refuse_plain); } else { CIPHER.deinit(); } @@ -71,14 +71,12 @@ async fn run() -> Result<(), anyhow::Error> { async { let file_path = TG_BOT.get_file(id_pair.1.as_str()).await.unwrap().file_path; Ok(TG_BOT.get_url_by_path(file_path)) - }.boxed() + } + .boxed() }); SERVER.init(&CONFIG.nats.address).await; - let bot = Bot::with_client( - CONFIG.telegram.token.clone(), - net::client_from_config() - ).auto_send(); + let bot = Bot::with_client(CONFIG.telegram.token.clone(), net::client_from_config()).auto_send(); TG_BOT.init(Arc::new(bot)); @@ -87,7 +85,8 @@ async fn run() -> Result<(), anyhow::Error> { &*CONFIG.telegram.bot_name, command::answer, message::handler::answer_msg, - ).await; + ) + .await; CONFIG.save(); log::info!("Mesagisto Bot is going to shut down"); diff --git a/src/message/handlers/receive.rs b/src/message/handlers/receive.rs index dfd08e6..02dc31b 100644 --- a/src/message/handlers/receive.rs +++ b/src/message/handlers/receive.rs @@ -3,7 +3,7 @@ use crate::TG_BOT; use crate::ext::DB; use mesagisto_client::{ cache::CACHE, - data::{message::MessageType, message::Message,Packet}, + data::{message::Message, message::MessageType, Packet}, }; use teloxide::payloads::SendMessageSetters; use teloxide::prelude::Requester; @@ -11,11 +11,11 @@ use teloxide::prelude::Requester; use teloxide::types::InputFile; pub async fn receive_from_server(message: nats::asynk::Message, target: i64) -> anyhow::Result<()> { - log::trace!("Receive from {}",target); + log::trace!("Receive from {}", target); let packet = Packet::from_cbor(&message.data)?; match packet { either::Left(msg) => { - handle_receive_message(msg,target).await?; + handle_receive_message(msg, target).await?; } either::Right(_) => {} } @@ -23,7 +23,6 @@ pub async fn receive_from_server(message: nats::asynk::Message, target: i64) -> } pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow::Result<()> { - for single in message.chain { log::trace!("handling element in chain"); let sender_name = if message.profile.nick.is_some() { @@ -39,19 +38,26 @@ pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow let receipt = if let Some(reply_to) = &message.reply { let local_id = DB.get_msg_id_1(&target, reply_to)?; match local_id { - Some(local_id) => TG_BOT.send_message(target, content).reply_to_message_id(local_id).await?, - None => TG_BOT.send_message(target, content).await? + Some(local_id) => { + TG_BOT + .send_message(target, content) + .reply_to_message_id(local_id) + .await? + } + None => TG_BOT.send_message(target, content).await?, } } else { TG_BOT.send_message(target, content).await? }; DB.put_msg_id_1(&target, &message.id, &receipt.id)?; - }, - MessageType::Image { id,url } => { + } + MessageType::Image { id, url } => { let channel = CONFIG.mapper(&target).expect("Channel don't exist"); let path = CACHE.file(&id, &url, &channel).await?; - let receipt = TG_BOT.send_message(target, format!("{} :",sender_name)).await?; - DB.put_msg_id_ir_2(&target, &receipt.id,&message.id)?; + let receipt = TG_BOT + .send_message(target, format!("{} :", sender_name)) + .await?; + DB.put_msg_id_ir_2(&target, &receipt.id, &message.id)?; let receipt = TG_BOT.send_photo(target, InputFile::File(path)).await?; DB.put_msg_id_1(&target, &message.id, &receipt.id)?; } @@ -59,4 +65,4 @@ pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow } Ok(()) -} \ No newline at end of file +} diff --git a/src/message/handlers/send.rs b/src/message/handlers/send.rs index 4a93cd9..8dd4e7a 100644 --- a/src/message/handlers/send.rs +++ b/src/message/handlers/send.rs @@ -42,10 +42,10 @@ pub async fn answer_common(cx: Arc) -> anyhow::Result<()> { return Ok(()); } let address = CONFIG.target_address_mapper.get(&target).unwrap().clone(); - let sender = match cx.update.from(){ + let sender = match cx.update.from() { Some(v) => v, //fixme - None => return Ok(()) + None => return Ok(()), }; // let avatar = bot_client().get_user_profile_photos(sender.id).await?; let profile = Profile {