Compare commits
3
Commits
bc32432808
...
275ac9d609
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
275ac9d609 | ||
|
|
3798358112 | ||
|
|
12bedd5de7 |
No files matched your search
@@ -1 +1,2 @@
|
||||
/target
|
||||
config.toml
|
||||
Generated
+75
-1
@@ -2,6 +2,23 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.102"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-waker"
|
||||
version = "1.1.2"
|
||||
@@ -169,10 +186,13 @@ checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
name = "daily_bot"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"sled",
|
||||
"tokio",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -214,7 +234,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1136,6 +1156,15 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
@@ -1370,6 +1399,45 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "1.0.7+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd28d57d8a6f6e458bc0b8784f8fdcc4b99a437936056fa122cb234f18656a96"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"serde_core",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_parser",
|
||||
"toml_writer",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "1.0.1+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b320e741db58cac564e26c607d3cc1fdc4a88fd36c879568c07856ed83ff3e9"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_parser"
|
||||
version = "1.0.10+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7df25b4befd31c4816df190124375d5a20c6b6921e2cad937316de3fccd63420"
|
||||
dependencies = [
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_writer"
|
||||
version = "1.0.7+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f17aaa1c6e3dc22b1da4b6bba97d066e354c7945cac2f7852d4e4e7ca7a6b56d"
|
||||
|
||||
[[package]]
|
||||
name = "tower"
|
||||
version = "0.5.3"
|
||||
@@ -1880,6 +1948,12 @@ version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a90e88e4667264a994d34e6d1ab2d26d398dcdca8b7f52bec8668957517fc7d8"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen"
|
||||
version = "0.51.0"
|
||||
|
||||
@@ -4,7 +4,10 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.102"
|
||||
async-trait = "0.1.89"
|
||||
reqwest = "0.13.2"
|
||||
serde = {version="1.0.228",features=["derive"]}
|
||||
sled = "0.34.7"
|
||||
tokio = {version="1.50.0",features=["full"]}
|
||||
toml = "1.0.7"
|
||||
Whitespace-only changes.
@@ -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