Skip to content

Commit 2e14d35

Browse files
committed
refactor!: remove SyllableSlice trait
1 parent bfafa21 commit 2e14d35

File tree

12 files changed

+140
-167
lines changed

12 files changed

+140
-167
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ What's New in libchewing (unreleased)
1515
used to initialize a chewing context with specific dictionaries enabled.
1616
- New boolean config option `chewing.sort_candidates_by_frequency` can be
1717
set to enable sorting candidates by frequency.
18+
- Rust type SyllableSlice is removed from public interfaces.
1819

1920
* Bug Fixes
2021
- Auto shift cursor after candidate selection now moves the cursor to the

src/conversion/chewing.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use std::{
66

77
use log::trace;
88

9-
use crate::dictionary::{Dictionary, LookupStrategy, Phrase};
9+
use crate::{
10+
dictionary::{Dictionary, LookupStrategy, Phrase},
11+
zhuyin::Syllable,
12+
};
1013

1114
use super::{Composition, ConversionEngine, Gap, Interval, Symbol};
1215

@@ -149,9 +152,14 @@ impl ChewingEngine {
149152
return None;
150153
}
151154

155+
let syllables: Vec<Syllable> = symbols
156+
.iter()
157+
.map(|s| s.to_syllable().unwrap_or_default())
158+
.collect();
159+
152160
let mut max_freq = 0;
153161
let mut best_phrase = None;
154-
'next_phrase: for phrase in dict.lookup_all_phrases(&symbols, self.lookup_strategy) {
162+
'next_phrase: for phrase in dict.lookup_all_phrases(&syllables, self.lookup_strategy) {
155163
// If there exists a user selected interval which is a
156164
// sub-interval of this phrase but the substring is
157165
// different then we can skip this phrase.

src/conversion/mod.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ mod simple;
66
mod symbol;
77

88
use std::{
9-
borrow::Cow,
109
cmp::{max, min},
1110
fmt::Debug,
1211
};
1312

14-
use crate::{
15-
dictionary::Dictionary,
16-
zhuyin::{Syllable, SyllableSlice},
17-
};
13+
use crate::{dictionary::Dictionary, zhuyin::Syllable};
1814

1915
pub use self::chewing::ChewingEngine;
2016
pub use self::fuzzy::FuzzyChewingEngine;
@@ -150,30 +146,6 @@ impl From<char> for Symbol {
150146
}
151147
}
152148

153-
impl SyllableSlice for &[Symbol] {
154-
fn to_slice(&self) -> Cow<'static, [Syllable]> {
155-
self.iter()
156-
.map_while(|&sym| match sym {
157-
Symbol::Syllable(syl) => Some(syl),
158-
Symbol::Char(_) => None,
159-
})
160-
.collect::<Vec<_>>()
161-
.into()
162-
}
163-
}
164-
165-
impl SyllableSlice for Vec<Symbol> {
166-
fn to_slice(&self) -> Cow<'static, [Syllable]> {
167-
self.iter()
168-
.map_while(|&sym| match sym {
169-
Symbol::Syllable(syl) => Some(syl),
170-
Symbol::Char(_) => None,
171-
})
172-
.collect::<Vec<_>>()
173-
.into()
174-
}
175-
}
176-
177149
/// Input data collected by the Editor.
178150
#[derive(Debug, Default, Clone)]
179151
pub struct Composition {

src/dictionary/layered.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55

66
use log::error;
77

8-
use crate::zhuyin::SyllableSlice;
8+
use crate::zhuyin::Syllable;
99

1010
use super::{
1111
Dictionary, DictionaryInfo, DictionaryMut, Entries, LookupStrategy, Phrase,
@@ -83,7 +83,7 @@ impl Dictionary for Layered {
8383
/// ```
8484
fn lookup_first_n_phrases(
8585
&self,
86-
syllables: &dyn SyllableSlice,
86+
syllables: &[Syllable],
8787
first: usize,
8888
strategy: LookupStrategy,
8989
) -> Vec<Phrase> {
@@ -159,7 +159,7 @@ impl DictionaryMut for Layered {
159159

160160
fn add_phrase(
161161
&mut self,
162-
syllables: &dyn SyllableSlice,
162+
syllables: &[Syllable],
163163
phrase: Phrase,
164164
) -> Result<(), UpdateDictionaryError> {
165165
if phrase.as_str().is_empty() {
@@ -175,7 +175,7 @@ impl DictionaryMut for Layered {
175175

176176
fn update_phrase(
177177
&mut self,
178-
syllables: &dyn SyllableSlice,
178+
syllables: &[Syllable],
179179
phrase: Phrase,
180180
user_freq: u32,
181181
time: u64,
@@ -193,7 +193,7 @@ impl DictionaryMut for Layered {
193193

194194
fn remove_phrase(
195195
&mut self,
196-
syllables: &dyn SyllableSlice,
196+
syllables: &[Syllable],
197197
phrase_str: &str,
198198
) -> Result<(), UpdateDictionaryError> {
199199
if let Some(writer) = self.user_dict.as_dict_mut() {

src/dictionary/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
path::Path,
1111
};
1212

13-
use crate::zhuyin::{Syllable, SyllableSlice};
13+
use crate::zhuyin::Syllable;
1414

1515
pub use layered::Layered;
1616
pub use loader::{
@@ -343,7 +343,7 @@ pub trait Dictionary: Debug {
343343
/// The result should use a stable order each time for the same input.
344344
fn lookup_first_n_phrases(
345345
&self,
346-
syllables: &dyn SyllableSlice,
346+
syllables: &[Syllable],
347347
first: usize,
348348
strategy: LookupStrategy,
349349
) -> Vec<Phrase>;
@@ -352,7 +352,7 @@ pub trait Dictionary: Debug {
352352
/// The result should use a stable order each time for the same input.
353353
fn lookup_first_phrase(
354354
&self,
355-
syllables: &dyn SyllableSlice,
355+
syllables: &[Syllable],
356356
strategy: LookupStrategy,
357357
) -> Option<Phrase> {
358358
self.lookup_first_n_phrases(syllables, 1, strategy)
@@ -362,11 +362,7 @@ pub trait Dictionary: Debug {
362362
/// Returns all phrases matched by the syllables.
363363
///
364364
/// The result should use a stable order each time for the same input.
365-
fn lookup_all_phrases(
366-
&self,
367-
syllables: &dyn SyllableSlice,
368-
strategy: LookupStrategy,
369-
) -> Vec<Phrase> {
365+
fn lookup_all_phrases(&self, syllables: &[Syllable], strategy: LookupStrategy) -> Vec<Phrase> {
370366
self.lookup_first_n_phrases(syllables, usize::MAX, strategy)
371367
}
372368
/// Returns an iterator to all phrases in the dictionary.
@@ -410,14 +406,14 @@ pub trait DictionaryMut: Debug {
410406
/// TODO: doc
411407
fn add_phrase(
412408
&mut self,
413-
syllables: &dyn SyllableSlice,
409+
syllables: &[Syllable],
414410
phrase: Phrase,
415411
) -> Result<(), UpdateDictionaryError>;
416412

417413
/// TODO: doc
418414
fn update_phrase(
419415
&mut self,
420-
syllables: &dyn SyllableSlice,
416+
syllables: &[Syllable],
421417
phrase: Phrase,
422418
user_freq: u32,
423419
time: u64,
@@ -426,7 +422,7 @@ pub trait DictionaryMut: Debug {
426422
/// TODO: doc
427423
fn remove_phrase(
428424
&mut self,
429-
syllables: &dyn SyllableSlice,
425+
syllables: &[Syllable],
430426
phrase_str: &str,
431427
) -> Result<(), UpdateDictionaryError>;
432428
}

src/dictionary/sqlite.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use rusqlite::{Connection, Error as RusqliteError, OpenFlags, OptionalExtension, params};
1010

11-
use crate::zhuyin::{Syllable, SyllableSlice};
11+
use crate::zhuyin::Syllable;
1212

1313
use super::{
1414
BuildDictionaryError, Dictionary, DictionaryBuilder, DictionaryInfo, DictionaryMut, Entries,
@@ -18,6 +18,21 @@ use super::{
1818
const APPLICATION_ID: u32 = 0x43484557; // 'CHEW' in big-endian
1919
const USER_VERSION: u32 = 0;
2020

21+
/// A slice that can be converted to a slice of syllables.
22+
trait SyllableSlice {
23+
fn to_bytes(&self) -> Vec<u8>;
24+
}
25+
26+
impl SyllableSlice for &[Syllable] {
27+
fn to_bytes(&self) -> Vec<u8> {
28+
let mut syllables_bytes = vec![];
29+
self.iter().for_each(|syl| {
30+
syllables_bytes.extend_from_slice(&syl.as_ref().to_u16().to_le_bytes())
31+
});
32+
syllables_bytes
33+
}
34+
}
35+
2136
/// TODO: doc
2237
#[derive(Debug)]
2338
#[non_exhaustive]
@@ -300,7 +315,7 @@ impl From<RusqliteError> for UpdateDictionaryError {
300315
impl Dictionary for SqliteDictionary {
301316
fn lookup_first_n_phrases(
302317
&self,
303-
syllables: &dyn SyllableSlice,
318+
syllables: &[Syllable],
304319
first: usize,
305320
strategy: LookupStrategy,
306321
) -> Vec<Phrase> {
@@ -392,7 +407,7 @@ impl DictionaryMut for SqliteDictionary {
392407

393408
fn add_phrase(
394409
&mut self,
395-
syllables: &dyn SyllableSlice,
410+
syllables: &[Syllable],
396411
phrase: Phrase,
397412
) -> Result<(), UpdateDictionaryError> {
398413
if self.readonly {
@@ -414,7 +429,7 @@ impl DictionaryMut for SqliteDictionary {
414429

415430
fn update_phrase(
416431
&mut self,
417-
syllables: &dyn SyllableSlice,
432+
syllables: &[Syllable],
418433
phrase: Phrase,
419434
user_freq: u32,
420435
time: u64,
@@ -468,7 +483,7 @@ impl DictionaryMut for SqliteDictionary {
468483

469484
fn remove_phrase(
470485
&mut self,
471-
syllables: &dyn SyllableSlice,
486+
syllables: &[Syllable],
472487
phrase_str: &str,
473488
) -> Result<(), UpdateDictionaryError> {
474489
let syllables_bytes = syllables.to_bytes();

src/dictionary/trie.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use der::{
1919
};
2020
use log::{error, warn};
2121

22-
use crate::zhuyin::{Syllable, SyllableSlice};
22+
use crate::zhuyin::Syllable;
2323

2424
use super::{
2525
BuildDictionaryError, Dictionary, DictionaryBuilder, DictionaryInfo, Entries, LookupStrategy,
@@ -260,7 +260,7 @@ macro_rules! iter_bail_if_oob {
260260
impl Dictionary for Trie {
261261
fn lookup_first_n_phrases(
262262
&self,
263-
syllables: &dyn SyllableSlice,
263+
syllables: &[Syllable],
264264
first: usize,
265265
strategy: LookupStrategy,
266266
) -> Vec<Phrase> {
@@ -293,7 +293,7 @@ impl Dictionary for Trie {
293293
// Perform a BFS search to find all leaf nodes
294294
let mut threads: VecDeque<TrieNodeView<'_>> = VecDeque::new();
295295
threads.push_back(root);
296-
for syl in syllables.to_slice().iter() {
296+
for syl in syllables {
297297
debug_assert!(syl.to_u16() != 0);
298298
for _ in 0..threads.len() {
299299
let node = threads.pop_front().unwrap();

0 commit comments

Comments
 (0)