Skip to content

Commit

Permalink
style: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Jul 10, 2022
1 parent b828b6d commit c0cbf63
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 39 deletions.
23 changes: 22 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
tab_spaces = 2
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
18 changes: 11 additions & 7 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -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<DefaultParseMode<Bot>>;

#[derive(Singleton, Default)]
Expand All @@ -28,6 +27,7 @@ impl TgBot {
self.inner.init(bot);
Ok(())
}

// fixme use this-error
pub async fn file(&self, uid: &Vec<u8>, id: &Vec<u8>) -> anyhow::Result<()> {
let id_str: ArcStr = base64_url::encode(id).into();
Expand All @@ -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}",
Expand All @@ -49,6 +50,7 @@ impl TgBot {
)
.into()
}

pub async fn send_text<T>(
&self,
chat_id: ChatId,
Expand Down Expand Up @@ -88,6 +90,7 @@ impl TgBot {
},
}
}

pub async fn send_image(
&self,
chat_id: ChatId,
Expand Down Expand Up @@ -157,6 +160,7 @@ impl TgBot {

impl Deref for TgBot {
type Target = BotRequester;

fn deref(&self) -> &Self::Target {
&self.inner
}
Expand Down
12 changes: 6 additions & 6 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -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支持以下命令")]
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ impl Config {
pub fn mapper(&self, target: &i64) -> Option<ArcStr> {
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<ArcStr> {
if let Some((_, address)) = self.bindings.remove(old_chat_id) {
self.bindings.insert(*new_chat_id, address.clone());
Expand Down
3 changes: 1 addition & 2 deletions src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
9 changes: 9 additions & 0 deletions src/ext/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -93,6 +100,7 @@ impl DbExt for Db {
false,
)
}

#[inline]
fn get_msg_id_1(&self, target: &i64, id: &[u8]) -> anyhow::Result<Option<i32>> {
let be_bytes = match self.get_msg_id(target.to_be_bytes().as_ref(), id)? {
Expand All @@ -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<Option<Vec<u8>>> {
self.get_msg_id(target.to_be_bytes().as_ref(), id)
Expand Down
3 changes: 1 addition & 2 deletions src/ext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use arcstr::ArcStr;
use arcstr::Substr;
use arcstr::{ArcStr, Substr};

pub mod db;
pub mod err;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#![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};
use tracing::*;

use self::message::handlers;
use crate::config::{Config, CONFIG};
use bot::TG_BOT;

#[macro_use]
extern crate educe;
Expand Down
20 changes: 11 additions & 9 deletions src/message/handlers/receive.rs
Original file line number Diff line number Diff line change
@@ -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<UnboundedSender<(i64, ArcStr)>> = LateInit::new();

pub fn recover() -> anyhow::Result<()> {
Expand Down Expand Up @@ -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(());
}
Expand Down
29 changes: 18 additions & 11 deletions src/message/handlers/send.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit c0cbf63

Please sign in to comment.