mod dict_importer; mod infrastructure; pub mod service; use futures::stream::BoxStream; use crate::common::errors::RepositoryError; pub use self::dict_importer::DictImporter; pub use self::infrastructure::json_file_dict_source::JsonFileDictSource; pub use self::infrastructure::sqlite_dict_repository::SqliteDictRepository; use std::collections::HashMap; pub type DictEntryId = u64; #[derive(Debug, Clone, PartialEq)] pub struct DictEntry { pub id: Option, pub text: String, pub metadata: HashMap, } impl DictEntry { pub fn new(id: Option, text: String) -> Self { DictEntry { id, text, metadata: HashMap::new(), } } } #[derive(Debug, Clone)] pub struct Dict { pub name: String, pub entries: HashMap, } impl Dict { pub fn new(name: String) -> Self { Dict { name, entries: HashMap::new(), } } pub fn add_entry(&mut self, entry: DictEntry) { self.entries.insert(entry.id.unwrap(), entry); } } #[async_trait::async_trait] pub trait DictRepository: Send + Sync { fn use_dict(&mut self, name: &str); async fn create_dict(&self) -> Result<(), RepositoryError>; async fn fetch_dicts(&self) -> Result, RepositoryError>; async fn count_entries(&self) -> Result; async fn save_entries(&self, entries: &[DictEntry]) -> Result<(), RepositoryError>; async fn fetch_many(&self, limit: usize, offset: usize) -> Result; async fn stream_batches( &self, batch_size: usize, ) -> Result, RepositoryError>>, RepositoryError>; } #[async_trait::async_trait] pub trait DictRepositoryFactory: Send + Sync { async fn create(&self, dict_name: &str) -> Result, RepositoryError>; async fn list_all(&self) -> Result, RepositoryError>; } pub trait DictSource { fn next_entry(&mut self) -> Option>; }