Files
yaoayanui/src-tauri/src/asr_store.rs
fengchuanhn@gmail.com d3af2e1124 11
2026-05-22 18:42:26 +08:00

84 lines
2.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 语音识别历史SQLite 访问与 Tauri 状态
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::asr_db::{AsrDb, AsrHistoryRecord};
#[derive(Debug, Default)]
pub struct AsrStore {
inner: Arc<RwLock<Option<Arc<AsrDb>>>>,
}
impl AsrStore {
pub fn new() -> Self {
Self::default()
}
pub async fn init(&self, db_path: PathBuf) -> Result<(), String> {
let db = tokio::task::spawn_blocking(move || {
AsrDb::open(db_path).map_err(|e| e.to_string())
})
.await
.map_err(|e| e.to_string())??;
let count = tokio::task::spawn_blocking({
let db = Arc::new(db);
let db2 = db.clone();
move || db2.list().map(|l| (db, l.len())).map_err(|e| e.to_string())
})
.await
.map_err(|e| e.to_string())??;
let (db, n) = count;
*self.inner.write().await = Some(db);
log::info!(target: "asr", "ASR 历史数据库已初始化,共 {n} 条记录");
Ok(())
}
async fn with_db<F, T>(&self, f: F) -> Result<T, String>
where
F: FnOnce(Arc<AsrDb>) -> Result<T, String> + Send + 'static,
T: Send + 'static,
{
let db = self
.inner
.read()
.await
.clone()
.ok_or_else(|| "ASR 数据库未初始化".to_string())?;
tokio::task::spawn_blocking(move || f(db))
.await
.map_err(|e| e.to_string())?
}
pub async fn list(&self) -> Result<Vec<AsrHistoryRecord>, String> {
self.with_db(|db| db.list().map_err(|e| e.to_string())).await
}
pub async fn insert(
&self,
name: String,
source_path: String,
result_text: String,
) -> Result<AsrHistoryRecord, String> {
self.with_db(move |db| {
db.insert(&name, &source_path, &result_text)
.map_err(|e| e.to_string())
})
.await
}
pub async fn update_name(&self, id: i64, name: String) -> Result<(), String> {
self.with_db(move |db| db.update_name(id, &name).map_err(|e| e.to_string()))
.await
}
pub async fn delete(&self, id: i64) -> Result<bool, String> {
self.with_db(move |db| db.delete(id).map_err(|e| e.to_string()))
.await
}
}