1use std::ops::Deref;
6use std::sync::Arc;
7
8use memmap2::Mmap;
9
10use crate::dictionary::grammar::read_grammar;
11use crate::dictionary::header::read_dictionary_header;
12use crate::dictionary::lexicon::Lexicon;
13use crate::dictionary::trie::DoubleArrayTrie;
14use crate::types::{has_synonym_group_ids, Grammar, DICTIONARY_HEADER_SIZE};
15
16pub enum DictData {
20 Owned(Vec<u8>),
21 Mapped(Mmap),
22}
23
24impl Deref for DictData {
25 type Target = [u8];
26
27 fn deref(&self) -> &[u8] {
28 match self {
29 DictData::Owned(v) => v,
30 DictData::Mapped(m) => m,
31 }
32 }
33}
34
35pub struct SharedDictionary {
42 data: Arc<DictData>,
43 trie: Arc<DoubleArrayTrie>,
44 trie_bytes: usize,
45 has_synonyms: bool,
46 lexicon_offset: usize,
47}
48
49impl SharedDictionary {
50 pub fn new(data: Vec<u8>) -> Result<Self, String> {
54 Self::from_dict_data(DictData::Owned(data))
55 }
56
57 pub fn from_mmap(mmap: Mmap) -> Result<Self, String> {
62 Self::from_dict_data(DictData::Mapped(mmap))
63 }
64
65 fn from_dict_data(dict_data: DictData) -> Result<Self, String> {
66 let header = read_dictionary_header(&dict_data, 0)?;
67 let has_synonyms = has_synonym_group_ids(header.version);
68
69 let (_, grammar_bytes) = read_grammar(&dict_data, DICTIONARY_HEADER_SIZE)?;
70 let lexicon_offset = DICTIONARY_HEADER_SIZE + grammar_bytes;
71
72 let (trie, trie_bytes) = DoubleArrayTrie::from_bytes(&dict_data, lexicon_offset);
74
75 Ok(Self {
76 data: Arc::new(dict_data),
77 trie: Arc::new(trie),
78 trie_bytes,
79 has_synonyms,
80 lexicon_offset,
81 })
82 }
83
84 pub fn create_lexicon(&self) -> (Lexicon, usize) {
86 Lexicon::from_shared(
87 &self.data,
88 self.lexicon_offset,
89 Arc::clone(&self.trie),
90 self.trie_bytes,
91 self.has_synonyms,
92 )
93 }
94
95 pub fn create_grammar(&self) -> Result<Grammar, String> {
97 let (grammar, _) = read_grammar(&self.data, DICTIONARY_HEADER_SIZE)?;
98 Ok(grammar)
99 }
100
101 pub fn data(&self) -> &Arc<DictData> {
103 &self.data
104 }
105
106 pub fn has_synonyms(&self) -> bool {
108 self.has_synonyms
109 }
110}