From 2a2119131b43fbae5567489801fc52042962f4a5 Mon Sep 17 00:00:00 2001 From: itsusinn Date: Sun, 14 Nov 2021 15:58:24 +0800 Subject: [PATCH] feat: extension module --- src/ext.rs | 104 --------------------------------------------- src/ext/db.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ext/mod.rs | 2 + src/ext/res.rs | 4 ++ 4 files changed, 117 insertions(+), 104 deletions(-) delete mode 100644 src/ext.rs create mode 100644 src/ext/db.rs create mode 100644 src/ext/mod.rs create mode 100644 src/ext/res.rs diff --git a/src/ext.rs b/src/ext.rs deleted file mode 100644 index a2a7cb4..0000000 --- a/src/ext.rs +++ /dev/null @@ -1,104 +0,0 @@ -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, 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, 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) -> 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) -> 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) -> 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) -> 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) -> anyhow::Result> { - 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) -> anyhow::Result>> { - 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 - } -} diff --git a/src/ext/db.rs b/src/ext/db.rs new file mode 100644 index 0000000..e74af14 --- /dev/null +++ b/src/ext/db.rs @@ -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, id: &i32) -> anyhow::Result<()>; + fn put_msg_id_ir_1(&self, target: &i64, uid: &Vec, id: &i32) -> anyhow::Result<()>; + + fn put_msg_id_2(&self, target: &i64, uid: &i32, id: &Vec) -> anyhow::Result<()>; + fn put_msg_id_ir_2(&self, target: &i64, uid: &i32, id: &Vec) -> anyhow::Result<()>; + + fn put_msg_id_3(&self, target: &u64, uid: &u64, id: &Vec) -> anyhow::Result<()>; + fn put_msg_id_ir_3(&self, target: &u64, uid: &u64, id: &Vec) -> anyhow::Result<()>; + + fn get_msg_id_1(&self, target: &i64, id: &Vec) -> anyhow::Result>; + fn get_msg_id_2(&self, target: &i64, id: &Vec) -> anyhow::Result>>; +} + +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, 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, 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) -> 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) -> 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) -> 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) -> 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) -> anyhow::Result> { + 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) -> anyhow::Result>> { + self.get_msg_id(&target.to_be_bytes().to_vec(), id) + } +} diff --git a/src/ext/mod.rs b/src/ext/mod.rs new file mode 100644 index 0000000..f19a28c --- /dev/null +++ b/src/ext/mod.rs @@ -0,0 +1,2 @@ +pub mod db; +pub mod res; diff --git a/src/ext/res.rs b/src/ext/res.rs new file mode 100644 index 0000000..c7fb1dc --- /dev/null +++ b/src/ext/res.rs @@ -0,0 +1,4 @@ +use mesagisto_client::res::Res; + +pub trait ResExt {} +impl ResExt for Res {}