Skip to content

Commit 95f6eeb

Browse files
committed
feat(dict)!: break system dict loader to load base and extra separately
1 parent 33c4144 commit 95f6eeb

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

capi/data/mini.dat

7 Bytes
Binary file not shown.

capi/src/io.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ pub unsafe extern "C" fn chewing_new2(
128128
vec![Box::new(builtin.unwrap()) as Box<dyn Dictionary>]
129129
}
130130
};
131+
let extra_dicts = sys_loader.load_extra().unwrap_or_default();
131132
let abbrev = sys_loader.load_abbrev();
132133
let abbrev = match abbrev {
133134
Ok(abbr) => abbr,
@@ -166,7 +167,8 @@ pub unsafe extern "C" fn chewing_new2(
166167

167168
let estimate = LaxUserFreqEstimate::max_from(user_dictionary.as_ref());
168169

169-
let dict = Layered::new(dictionaries, user_dictionary);
170+
let system_dicts = Vec::from_iter(dictionaries.into_iter().chain(extra_dicts.into_iter()));
171+
let dict = Layered::new(system_dicts, user_dictionary);
170172
let conversion_engine = Box::new(ChewingEngine::new());
171173
let kb_compat = KeyboardLayoutCompat::Default;
172174
let keyboard = AnyKeyboardLayout::Qwerty(Qwerty);

src/dictionary/loader.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,17 @@ impl SystemDictionaryLoader {
8888
let tsi_dict = Trie::open(tsi_dict_path).map_err(io_err)?;
8989
results.push(Box::new(tsi_dict));
9090

91+
Ok(results)
92+
}
93+
/// Searches and loads the extra dictionaries.
94+
pub fn load_extra(&self) -> Result<Vec<Box<dyn Dictionary>>, LoadDictionaryError> {
95+
let search_path = if let Some(sys_path) = &self.sys_path {
96+
sys_path.to_owned()
97+
} else {
98+
sys_path_from_env_var()
99+
};
91100
let extra_files = find_extra_dat_by_path(&search_path);
101+
let mut results: Vec<Box<dyn Dictionary>> = vec![];
92102
for path in extra_files {
93103
info!("Loading {}", path.display());
94104
match Trie::open(&path) {

src/editor/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,16 @@ pub(crate) struct SharedState {
191191

192192
impl Editor {
193193
pub fn chewing() -> Result<Editor, Box<dyn Error>> {
194-
let system_dict = SystemDictionaryLoader::new().load()?;
194+
let sys_loader = SystemDictionaryLoader::new();
195+
let base_dict = sys_loader.load()?;
196+
let extra_dict = sys_loader.load_extra()?;
197+
let system_dict = Vec::from_iter(base_dict.into_iter().chain(extra_dict.into_iter()));
195198
let user_dict = UserDictionaryLoader::new().load()?;
196199
let estimate = LaxUserFreqEstimate::max_from(user_dict.as_ref());
197200
let dict = Layered::new(system_dict, user_dict);
198201
let conversion_engine = Box::new(ChewingEngine::new());
199-
let abbrev = SystemDictionaryLoader::new().load_abbrev()?;
200-
let sym_sel = SystemDictionaryLoader::new().load_symbol_selector()?;
202+
let abbrev = sys_loader.load_abbrev()?;
203+
let sym_sel = sys_loader.load_symbol_selector()?;
201204
let editor = Editor::new(conversion_engine, dict, estimate, abbrev, sym_sel);
202205
Ok(editor)
203206
}

tools/src/info.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@ use chewing::dictionary::{Dictionary, SystemDictionaryLoader, UserDictionaryLoad
44
use crate::flags;
55

66
pub(crate) fn run(args: flags::Info) -> Result<()> {
7-
let mut dictionaries = vec![];
8-
if args.user {
9-
dictionaries.push(UserDictionaryLoader::new().load()?);
10-
}
117
if args.system {
12-
dictionaries.extend(SystemDictionaryLoader::new().load()?);
8+
let dictionaries = SystemDictionaryLoader::new().load()?;
9+
if args.json {
10+
print_json_info(&dictionaries, "base");
11+
} else {
12+
print_info(&dictionaries, "base");
13+
}
14+
let extra = SystemDictionaryLoader::new().load_extra()?;
15+
if args.json {
16+
print_json_info(&extra, "extra");
17+
} else {
18+
print_info(&extra, "extra");
19+
}
1320
}
14-
if let Some(path) = args.path {
15-
dictionaries.push(UserDictionaryLoader::new().userphrase_path(path).load()?);
21+
if args.user {
22+
let dict = UserDictionaryLoader::new().load()?;
23+
if args.json {
24+
print_json_info(&[dict], "user");
25+
} else {
26+
print_info(&[dict], "user");
27+
}
1628
}
17-
if args.json {
18-
print_json_info(&dictionaries);
19-
} else {
20-
print_info(&dictionaries);
29+
if let Some(path) = args.path {
30+
let dict = UserDictionaryLoader::new().userphrase_path(path).load()?;
31+
if args.json {
32+
print_json_info(&[dict], "input");
33+
} else {
34+
print_info(&[dict], "input");
35+
}
2136
}
2237
Ok(())
2338
}
@@ -39,7 +54,7 @@ fn escape_json(str: String) -> String {
3954
out
4055
}
4156

42-
fn print_json_info(dictionaries: &[Box<dyn Dictionary>]) {
57+
fn print_json_info(dictionaries: &[Box<dyn Dictionary>], from: &str) {
4358
let mut iter = dictionaries.iter().peekable();
4459
println!("[");
4560
while let Some(dict) = iter.next() {
@@ -49,6 +64,7 @@ fn print_json_info(dictionaries: &[Box<dyn Dictionary>]) {
4964
.unwrap_or(String::new());
5065
let info = dict.about();
5166
println!(" {{");
67+
println!(r#" "from": "{from}","#);
5268
println!(r#" "path": "{}","#, escape_json(path));
5369
println!(r#" "name": "{}","#, escape_json(info.name));
5470
println!(r#" "version": "{}","#, escape_json(info.version));
@@ -60,14 +76,15 @@ fn print_json_info(dictionaries: &[Box<dyn Dictionary>]) {
6076
println!("]");
6177
}
6278

63-
fn print_info(dictionaries: &[Box<dyn Dictionary>]) {
79+
fn print_info(dictionaries: &[Box<dyn Dictionary>], from: &str) {
6480
for dict in dictionaries {
6581
let path = dict
6682
.path()
6783
.map(|p| p.display().to_string())
6884
.unwrap_or(String::new());
6985
let info = dict.about();
7086
println!("---");
87+
println!("From : {from}");
7188
println!("Path : {}", path);
7289
println!("Name : {}", info.name);
7390
println!("Version : {}", info.version);

0 commit comments

Comments
 (0)