sudachi/dic/storage.rs
1/*
2 * Copyright (c) 2021-2024 Works Applications Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use memmap2::Mmap;
18use nom::AsBytes;
19
20pub enum Storage {
21 File(Mmap),
22 Borrowed(&'static [u8]),
23 Owned(Vec<u8>),
24}
25
26impl AsRef<[u8]> for Storage {
27 fn as_ref(&self) -> &[u8] {
28 match self {
29 Storage::File(m) => m.as_bytes(),
30 Storage::Borrowed(b) => b,
31 Storage::Owned(v) => v,
32 }
33 }
34}
35
36pub struct SudachiDicData {
37 // system dictionary
38 system: Storage,
39 // user dictionaries
40 user: Vec<Storage>,
41}
42
43impl SudachiDicData {
44 pub fn new(system: Storage) -> Self {
45 Self {
46 system,
47 user: Vec::new(),
48 }
49 }
50
51 pub fn add_user(&mut self, user: Storage) {
52 self.user.push(user)
53 }
54
55 pub fn system(&self) -> &[u8] {
56 self.system.as_ref()
57 }
58
59 /// # Safety
60 /// Call this function only after system dictionary data is ready.
61 pub unsafe fn system_static_slice(&self) -> &'static [u8] {
62 std::mem::transmute(self.system())
63 }
64
65 pub(crate) fn user_static_slice(&self) -> Vec<&'static [u8]> {
66 let mut result = Vec::with_capacity(self.user.len());
67 for u in self.user.iter() {
68 let slice: &'static [u8] = unsafe { std::mem::transmute(u.as_ref()) };
69 result.push(slice);
70 }
71 result
72 }
73}