Skip to content

Commit

Permalink
refactor: helper functions of Database
Browse files Browse the repository at this point in the history
  • Loading branch information
itsusinn committed Oct 30, 2021
1 parent 10a2b52 commit 72d1554
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
104 changes: 104 additions & 0 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::{convert::TryInto, ops::Deref};

use mesagisto_client::{OkExt, OptionExt, db::DB as INNER};


#[derive(Singleton, Default)]
pub struct Db{}
impl Db {
#[inline]
pub fn put_msg_id_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()> {
INNER.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.to_be_bytes().to_vec(),
true,
)
}
// no reverse
#[inline]
pub fn put_msg_id_ir_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()> {
INNER.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.to_be_bytes().to_vec(),
false,
)
}
#[inline]
pub fn put_msg_id_1(&self, target: &i64, uid: &Vec<u8>, id: &i32) -> anyhow::Result<()> {
INNER.put_msg_id(
target.to_be_bytes().to_vec(),
uid.clone(),
id.to_be_bytes().to_vec(),
true,
)
}
#[inline]
pub fn put_msg_id_ir_1(&self, target: &i64, uid: &Vec<u8>, id: &i32) -> anyhow::Result<()> {
INNER.put_msg_id(
target.to_be_bytes().to_vec(),
uid.clone(),
id.to_be_bytes().to_vec(),
false,
)
}
#[inline]
pub fn put_msg_id_2(&self, target: &i64, uid: &i32, id: &Vec<u8>) -> anyhow::Result<()> {
INNER.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.clone(),
true,
)
}
#[inline]
pub fn put_msg_id_ir_2(&self, target: &i64, uid: &i32, id: &Vec<u8>) -> anyhow::Result<()> {
INNER.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.clone(),
false,
)
}
#[inline]
pub fn put_msg_id_3(&self, target: &u64, uid: &u64, id: &Vec<u8>) -> anyhow::Result<()> {
INNER.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.clone(),
true,
)
}
#[inline]
pub fn put_msg_id_ir_3(&self, target: &u64, uid: &u64, id: &Vec<u8>) -> anyhow::Result<()> {
INNER.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.clone(),
false,
)
}
#[inline]
pub fn get_msg_id_1(&self, target: &i64, id: &Vec<u8>) -> anyhow::Result<Option<i32>> {
let be_bytes = match INNER.get_msg_id(&target.to_be_bytes().to_vec(), id)? {
Some(v) => match v.len() {
4 => v,
_ => return Ok(None),
},
None => return Ok(None),
};
i32::from_be_bytes(be_bytes.try_into().unwrap()).some().ok()
}
#[inline]
pub fn get_msg_id_2(&self, target: &i64, id: &Vec<u8>) -> anyhow::Result<Option<Vec<u8>>> {
INNER.get_msg_id(&target.to_be_bytes().to_vec(), id)
}
}
impl Deref for Db {
type Target = mesagisto_client::db::Db;

fn deref(&self) -> &Self::Target {
&*INNER
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod despatch;
mod message;
mod net;
mod webhook;
pub mod ext;

fn main() {
std::env::set_var("RUST_BACKTRACE", "1");
Expand Down
2 changes: 1 addition & 1 deletion src/message/handlers/receive.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::CONFIG;
use crate::TG_BOT;
use crate::ext::DB;
use mesagisto_client::{
cache::CACHE,
data::{message::MessageType, message::Message,Packet},
db::DB
};
use teloxide::payloads::SendMessageSetters;
use teloxide::prelude::Requester;
Expand Down
2 changes: 1 addition & 1 deletion src/message/handlers/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::config::CONFIG;
use crate::bot::TG_BOT;
use crate::message::handlers::receive::receive_from_server;
use crate::message::Cx;
use crate::ext::DB;
use arcstr::ArcStr;
use mesagisto_client::EitherExt;
use mesagisto_client::data::message::{MessageType, Profile};
use mesagisto_client::data::{message, Packet};
use mesagisto_client::server::SERVER;
use mesagisto_client::res::RES;
use mesagisto_client::db::DB;
use std::sync::Arc;
use teloxide::prelude::*;

Expand Down

0 comments on commit 72d1554

Please sign in to comment.