mecab_ko_dict/trie/
backend.rs1use std::path::Path;
4
5use smallvec::SmallVec;
6
7use crate::error::Result;
8
9use super::{mmap::MmapTrie, Trie};
10
11pub enum TrieBackend {
15 Owned(Trie<'static>),
17 Mmap(MmapTrie),
19}
20
21pub type PrefixSearchResult = SmallVec<[(u32, usize); 16]>;
25
26impl TrieBackend {
27 pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
33 Ok(Self::Owned(Trie::from_file(path)?))
34 }
35
36 pub fn from_mmap_file<P: AsRef<Path>>(path: P) -> Result<Self> {
42 Ok(Self::Mmap(MmapTrie::from_file(path)?))
43 }
44
45 pub fn from_compressed_file<P: AsRef<Path>>(path: P) -> Result<Self> {
51 Ok(Self::Owned(Trie::from_compressed_file(path)?))
52 }
53
54 #[must_use]
56 pub fn exact_match(&self, key: &str) -> Option<u32> {
57 match self {
58 Self::Owned(t) => t.exact_match(key),
59 Self::Mmap(t) => t.exact_match(key),
60 }
61 }
62
63 #[must_use]
65 pub fn exact_match_bytes(&self, key: &[u8]) -> Option<u32> {
66 match self {
67 Self::Owned(t) => t.exact_match_bytes(key),
68 Self::Mmap(t) => t.exact_match_bytes(key),
69 }
70 }
71
72 #[must_use]
74 pub fn common_prefix_search(&self, text: &str) -> PrefixSearchResult {
75 match self {
76 Self::Owned(t) => t.common_prefix_search(text).collect(),
77 Self::Mmap(t) => t.common_prefix_search(text).collect(),
78 }
79 }
80
81 #[must_use]
83 pub fn common_prefix_search_bytes(&self, key: &[u8]) -> PrefixSearchResult {
84 match self {
85 Self::Owned(t) => t.common_prefix_search_bytes(key).collect(),
86 Self::Mmap(t) => t.common_prefix_search_bytes(key).collect(),
87 }
88 }
89
90 #[must_use]
92 pub fn common_prefix_search_at(&self, text: &str, start_byte: usize) -> PrefixSearchResult {
93 match self {
94 Self::Owned(t) => t.common_prefix_search_at(text, start_byte),
95 Self::Mmap(t) => t.common_prefix_search_at(text, start_byte),
96 }
97 }
98}