use std::borrow::Cow; use heed::Result as ZResult; use heed::types::ByteSlice; use crate::database::MainT; use crate::{FstSetCow, MResult}; #[derive(Copy, Clone)] pub struct Synonyms { pub(crate) synonyms: heed::Database, } impl Synonyms { pub fn put_synonyms(self, writer: &mut heed::RwTxn, word: &[u8], synonyms: &fst::Set) -> ZResult<()> where A: AsRef<[u8]>, { let bytes = synonyms.as_fst().as_bytes(); self.synonyms.put(writer, word, bytes) } pub fn del_synonyms(self, writer: &mut heed::RwTxn, word: &[u8]) -> ZResult { self.synonyms.delete(writer, word) } pub fn clear(self, writer: &mut heed::RwTxn) -> ZResult<()> { self.synonyms.clear(writer) } pub fn synonyms_fst<'txn>(self, reader: &'txn heed::RoTxn, word: &[u8]) -> ZResult> { match self.synonyms.get(reader, word)? { Some(bytes) => Ok(fst::Set::new(bytes).unwrap().map_data(Cow::Borrowed).unwrap()), None => Ok(fst::Set::default().map_data(Cow::Owned).unwrap()), } } pub fn synonyms(self, reader: &heed::RoTxn, word: &[u8]) -> MResult>> { let synonyms = self .synonyms_fst(&reader, word)? .map(|list| list.stream().into_strs()) .transpose()?; Ok(synonyms) } }