This commit is contained in:
1 parent
bc32432808
commit
12bedd5de7
11 files changed
+303
-4
No files matched your search
@@ -0,0 +1,22 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{path::Path, sync::OnceLock};
|
||||
|
||||
static CONFIG: OnceLock<Config> = OnceLock::new();
|
||||
pub fn get_config() -> &'static Config {
|
||||
CONFIG.get_or_init(|| {
|
||||
toml::from_str(&std::fs::read_to_string(Path::new("config.toml")).expect("无法读配置文件"))
|
||||
.expect("解析配置文件时发生错误")
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub matrix: Option<MatrixBot>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct MatrixBot {
|
||||
pub home_server: String,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
|
||||
//底层存储引擎实现
|
||||
#[async_trait]
|
||||
pub trait StorageEngine: Sized + Send + Sync {
|
||||
async fn open(path: &Path) -> Result<Self>;
|
||||
async fn get_raw(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
|
||||
async fn set_raw(&self, key: &[u8], value: &[u8]) -> Result<()>;
|
||||
async fn delete_raw(&self, key: &[u8]) -> Result<()>;
|
||||
}
|
||||
|
||||
//上层语义化存储实现
|
||||
#[async_trait]
|
||||
pub trait DailyBotStore: Send + Sync {
|
||||
async fn save(&self, scope: &str, id: &str, value: &[u8]) -> Result<()>;
|
||||
async fn load(&self, scope: &str, id: &str) -> Result<Option<Vec<u8>>>;
|
||||
async fn remove(&self, scope: &str, id: &str) -> Result<()>;
|
||||
}
|
||||
|
||||
//给所有底层引擎实现专门对底层引擎的特有方法
|
||||
#[async_trait]
|
||||
impl<T> DailyBotStore for T
|
||||
where
|
||||
T: StorageEngine + Send + Sync,
|
||||
{
|
||||
async fn save(&self, scope: &str, id: &str, value: &[u8]) -> Result<()> {
|
||||
let key = format!("{scope}:{id}");
|
||||
self.set_raw(key.as_bytes(), value).await
|
||||
}
|
||||
|
||||
async fn load(&self, scope: &str, id: &str) -> Result<Option<Vec<u8>>> {
|
||||
let key = format!("{scope}:{id}");
|
||||
self.get_raw(key.as_bytes()).await
|
||||
}
|
||||
|
||||
async fn remove(&self, scope: &str, id: &str) -> Result<()> {
|
||||
let key = format!("{scope}:{id}");
|
||||
self.delete_raw(key.as_bytes()).await
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
pub mod define;
|
||||
pub mod sled_db;
|
||||
pub mod define;
|
||||
@@ -0,0 +1,111 @@
|
||||
use std::path::Path;
|
||||
|
||||
use super::define::StorageEngine;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
|
||||
pub struct SledDB {
|
||||
pub sled_db: sled::Db,
|
||||
}
|
||||
|
||||
impl SledDB {
|
||||
pub fn new(path: &Path) -> Result<Self> {
|
||||
let db = sled::open(path)?;
|
||||
Ok(Self { sled_db: db })
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl StorageEngine for SledDB {
|
||||
async fn open(path: &Path) -> Result<Self> {
|
||||
Self::new(path)
|
||||
}
|
||||
|
||||
async fn set_raw(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
self.sled_db.insert(key, value)?;
|
||||
self.sled_db.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_raw(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
||||
let result = self.sled_db.get(key)?;
|
||||
Ok(result.map(|i| i.to_vec()))
|
||||
}
|
||||
|
||||
async fn delete_raw(&self, key: &[u8]) -> Result<()> {
|
||||
self.sled_db.remove(key)?;
|
||||
self.sled_db.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//INFO: AI写的集成测试
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use super::super::define::{DailyBotStore, StorageEngine};
|
||||
use super::SledDB;
|
||||
|
||||
fn unique_db_path() -> PathBuf {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("time went backwards")
|
||||
.as_nanos();
|
||||
std::env::temp_dir().join(format!(
|
||||
"daily_bot_sled_test_{}_{}",
|
||||
std::process::id(),
|
||||
nanos
|
||||
))
|
||||
}
|
||||
|
||||
fn cleanup_db_path(path: &PathBuf) {
|
||||
if path.exists() {
|
||||
let _ = std::fs::remove_dir_all(path);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn raw_crud_roundtrip_works() -> Result<()> {
|
||||
let db_path = unique_db_path();
|
||||
let db = <SledDB as StorageEngine>::open(&db_path).await?;
|
||||
|
||||
db.set_raw(b"k1", b"v1").await?;
|
||||
let got = db.get_raw(b"k1").await?;
|
||||
assert_eq!(got, Some(b"v1".to_vec()));
|
||||
|
||||
db.delete_raw(b"k1").await?;
|
||||
let deleted = db.get_raw(b"k1").await?;
|
||||
assert_eq!(deleted, None);
|
||||
|
||||
drop(db);
|
||||
cleanup_db_path(&db_path);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn daily_bot_store_namespace_works() -> Result<()> {
|
||||
let db_path = unique_db_path();
|
||||
let db = SledDB::new(&db_path)?;
|
||||
|
||||
db.save("user", "alice", b"hello").await?;
|
||||
|
||||
let loaded = db.load("user", "alice").await?;
|
||||
assert_eq!(loaded, Some(b"hello".to_vec()));
|
||||
|
||||
let raw = db.get_raw(b"user:alice").await?;
|
||||
assert_eq!(raw, Some(b"hello".to_vec()));
|
||||
|
||||
db.remove("user", "alice").await?;
|
||||
let removed = db.load("user", "alice").await?;
|
||||
assert_eq!(removed, None);
|
||||
|
||||
drop(db);
|
||||
cleanup_db_path(&db_path);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use crate::config::get_config;
|
||||
use anyhow::{Ok, Result};
|
||||
pub async fn init() -> Result<()> {
|
||||
let _ = get_config();//初始化日志系统
|
||||
Ok(())
|
||||
}
|
||||
+7
-2
@@ -1,3 +1,8 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use anyhow::Ok;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
daily_bot::init::init().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1 +1,35 @@
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Sender {
|
||||
async fn send(&self, message: OutboundMessage) -> Result<String>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait Receiver {
|
||||
// Pull user inputs from upstream and return a cursor for incremental sync.
|
||||
async fn poll_inputs(&self, cursor: Option<&str>, limit: usize) -> Result<InputBatch>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OutboundMessage {
|
||||
pub channel_id: String,
|
||||
pub text: String,
|
||||
pub reply_to_event_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InboundMessage {
|
||||
pub event_id: String,
|
||||
pub channel_id: String,
|
||||
pub user_id: String,
|
||||
pub text: String,
|
||||
pub timestamp_ms: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct InputBatch {
|
||||
pub messages: Vec<InboundMessage>,
|
||||
pub next_cursor: Option<String>,
|
||||
}
|
||||
Reference in new issue
Block a user