Skip to content

Commit

Permalink
feat: extension module
Browse files Browse the repository at this point in the history
  • Loading branch information
itsusinn committed Nov 14, 2021
1 parent 96eaeee commit 2a21191
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 104 deletions.
104 changes: 0 additions & 104 deletions src/ext.rs

This file was deleted.

111 changes: 111 additions & 0 deletions src/ext/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::convert::TryInto;

use mesagisto_client::{db::Db, OkExt, OptionExt};

pub trait DbExt {
fn put_msg_id_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()>;
fn put_msg_id_ir_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()>;

fn put_msg_id_1(&self, target: &i64, uid: &Vec<u8>, id: &i32) -> anyhow::Result<()>;
fn put_msg_id_ir_1(&self, target: &i64, uid: &Vec<u8>, id: &i32) -> anyhow::Result<()>;

fn put_msg_id_2(&self, target: &i64, uid: &i32, id: &Vec<u8>) -> anyhow::Result<()>;
fn put_msg_id_ir_2(&self, target: &i64, uid: &i32, id: &Vec<u8>) -> anyhow::Result<()>;

fn put_msg_id_3(&self, target: &u64, uid: &u64, id: &Vec<u8>) -> anyhow::Result<()>;
fn put_msg_id_ir_3(&self, target: &u64, uid: &u64, id: &Vec<u8>) -> anyhow::Result<()>;

fn get_msg_id_1(&self, target: &i64, id: &Vec<u8>) -> anyhow::Result<Option<i32>>;
fn get_msg_id_2(&self, target: &i64, id: &Vec<u8>) -> anyhow::Result<Option<Vec<u8>>>;
}

impl DbExt for Db {
#[inline]
fn put_msg_id_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()> {
self.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]
fn put_msg_id_ir_0(&self, target: &i64, uid: &i32, id: &i32) -> anyhow::Result<()> {
self.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.to_be_bytes().to_vec(),
false,
)
}
#[inline]
fn put_msg_id_1(&self, target: &i64, uid: &Vec<u8>, id: &i32) -> anyhow::Result<()> {
self.put_msg_id(
target.to_be_bytes().to_vec(),
uid.clone(),
id.to_be_bytes().to_vec(),
true,
)
}
#[inline]
fn put_msg_id_ir_1(&self, target: &i64, uid: &Vec<u8>, id: &i32) -> anyhow::Result<()> {
self.put_msg_id(
target.to_be_bytes().to_vec(),
uid.clone(),
id.to_be_bytes().to_vec(),
false,
)
}
#[inline]
fn put_msg_id_2(&self, target: &i64, uid: &i32, id: &Vec<u8>) -> anyhow::Result<()> {
self.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.clone(),
true,
)
}
#[inline]
fn put_msg_id_ir_2(&self, target: &i64, uid: &i32, id: &Vec<u8>) -> anyhow::Result<()> {
self.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.clone(),
false,
)
}
#[inline]
fn put_msg_id_3(&self, target: &u64, uid: &u64, id: &Vec<u8>) -> anyhow::Result<()> {
self.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.clone(),
true,
)
}
#[inline]
fn put_msg_id_ir_3(&self, target: &u64, uid: &u64, id: &Vec<u8>) -> anyhow::Result<()> {
self.put_msg_id(
target.to_be_bytes().to_vec(),
uid.to_be_bytes().to_vec(),
id.clone(),
false,
)
}
#[inline]
fn get_msg_id_1(&self, target: &i64, id: &Vec<u8>) -> anyhow::Result<Option<i32>> {
let be_bytes = match self.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]
fn get_msg_id_2(&self, target: &i64, id: &Vec<u8>) -> anyhow::Result<Option<Vec<u8>>> {
self.get_msg_id(&target.to_be_bytes().to_vec(), id)
}
}
2 changes: 2 additions & 0 deletions src/ext/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod db;
pub mod res;
4 changes: 4 additions & 0 deletions src/ext/res.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use mesagisto_client::res::Res;

pub trait ResExt {}
impl ResExt for Res {}

0 comments on commit 2a21191

Please sign in to comment.