Skip to content

Commit 4d209a5

Browse files
committed
refactor!: the DictionaryMut trait is merged back to the Dictionary trait
1 parent 9bce35c commit 4d209a5

File tree

10 files changed

+56
-105
lines changed

10 files changed

+56
-105
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ What's New in libchewing (unreleased)
3939
and without the break word.
4040
- rust: simplified dictionary API.
4141
- rust: logging now depends on tracing and tracing-subscriber.
42+
- rust: the DictionaryMut trait is merged back to the Dictionary trait.
4243

4344
* Dictionary
4445
- Default symbols.dat now includes commonly used emojis.

src/conversion/chewing.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,7 @@ mod tests {
681681
#[test]
682682
fn convert_zero_length_entry() {
683683
let mut dict = test_dictionary();
684-
let dict_mut = dict.as_dict_mut().unwrap();
685-
dict_mut.add_phrase(&[], ("", 0).into()).unwrap();
684+
dict.add_phrase(&[], ("", 0).into()).unwrap();
686685
let engine = ChewingEngine::new();
687686
let mut composition = Composition::new();
688687
for sym in [

src/conversion/simple.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ mod tests {
136136
#[test]
137137
fn convert_zero_length_entry() {
138138
let mut dict = test_dictionary();
139-
let dict_mut = dict.as_dict_mut().unwrap();
140-
dict_mut.add_phrase(&[], ("", 0).into()).unwrap();
139+
dict.add_phrase(&[], ("", 0).into()).unwrap();
141140
let engine = SimpleEngine::new();
142141
let mut composition = Composition::new();
143142
for sym in [

src/dictionary/layered.rs

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

66
use tracing::error;
77

8-
use super::{
9-
Dictionary, DictionaryInfo, DictionaryMut, Entries, LookupStrategy, Phrase,
10-
UpdateDictionaryError,
11-
};
8+
use super::{Dictionary, DictionaryInfo, Entries, LookupStrategy, Phrase, UpdateDictionaryError};
129
use crate::zhuyin::Syllable;
1310

1411
/// A collection of dictionaries that returns the union of the lookup results.
@@ -143,26 +140,12 @@ impl Dictionary for Layered {
143140
None
144141
}
145142

146-
fn as_dict_mut(&mut self) -> Option<&mut dyn DictionaryMut> {
147-
self.user_dict.as_dict_mut()
148-
}
149-
}
150-
151-
impl DictionaryMut for Layered {
152143
fn reopen(&mut self) -> Result<(), UpdateDictionaryError> {
153-
if let Some(writer) = self.user_dict.as_dict_mut() {
154-
writer.reopen()
155-
} else {
156-
Ok(())
157-
}
144+
self.user_dict.reopen()
158145
}
159146

160147
fn flush(&mut self) -> Result<(), UpdateDictionaryError> {
161-
if let Some(writer) = self.user_dict.as_dict_mut() {
162-
writer.flush()
163-
} else {
164-
Ok(())
165-
}
148+
self.user_dict.flush()
166149
}
167150

168151
fn add_phrase(
@@ -174,11 +157,7 @@ impl DictionaryMut for Layered {
174157
error!("BUG! added phrase is empty");
175158
return Ok(());
176159
}
177-
if let Some(writer) = self.user_dict.as_dict_mut() {
178-
writer.add_phrase(syllables, phrase)
179-
} else {
180-
Ok(())
181-
}
160+
self.user_dict.add_phrase(syllables, phrase)
182161
}
183162

184163
fn update_phrase(
@@ -192,23 +171,16 @@ impl DictionaryMut for Layered {
192171
error!("BUG! added phrase is empty");
193172
return Ok(());
194173
}
195-
if let Some(writer) = self.user_dict.as_dict_mut() {
196-
writer.update_phrase(syllables, phrase, user_freq, time)
197-
} else {
198-
Ok(())
199-
}
174+
self.user_dict
175+
.update_phrase(syllables, phrase, user_freq, time)
200176
}
201177

202178
fn remove_phrase(
203179
&mut self,
204180
syllables: &[Syllable],
205181
phrase_str: &str,
206182
) -> Result<(), UpdateDictionaryError> {
207-
if let Some(writer) = self.user_dict.as_dict_mut() {
208-
writer.remove_phrase(syllables, phrase_str)
209-
} else {
210-
Ok(())
211-
}
183+
self.user_dict.remove_phrase(syllables, phrase_str)
212184
}
213185
}
214186

@@ -222,8 +194,7 @@ mod tests {
222194
use super::Layered;
223195
use crate::{
224196
dictionary::{
225-
Dictionary, DictionaryBuilder, DictionaryMut, LookupStrategy, Phrase, Trie, TrieBuf,
226-
TrieBuilder,
197+
Dictionary, DictionaryBuilder, LookupStrategy, Phrase, Trie, TrieBuf, TrieBuilder,
227198
},
228199
syl,
229200
zhuyin::Bopomofo,
@@ -354,15 +325,14 @@ mod tests {
354325
),
355326
);
356327
let _ = dict.about();
357-
assert!(dict.as_dict_mut().is_none());
358-
assert!(dict.reopen().is_ok());
359-
assert!(dict.flush().is_ok());
328+
assert!(dict.reopen().is_err());
329+
assert!(dict.flush().is_err());
360330
assert!(
361331
dict.add_phrase(
362332
&[syl![Bopomofo::C, Bopomofo::E, Bopomofo::TONE4]],
363333
("冊", 100).into()
364334
)
365-
.is_ok()
335+
.is_err()
366336
);
367337
assert!(
368338
dict.update_phrase(
@@ -371,11 +341,11 @@ mod tests {
371341
0,
372342
0,
373343
)
374-
.is_ok()
344+
.is_err()
375345
);
376346
assert!(
377347
dict.remove_phrase(&[syl![Bopomofo::C, Bopomofo::E, Bopomofo::TONE4]], "冊")
378-
.is_ok()
348+
.is_err()
379349
);
380350
Ok(())
381351
}

src/dictionary/loader.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,10 @@ impl UserDictionaryLoader {
174174
let freq = phrase.freq();
175175
let last_used = phrase.last_used().unwrap_or_default();
176176
fresh_dict
177-
.as_dict_mut()
178-
.unwrap()
179177
.update_phrase(&syllables, phrase, freq, last_used)
180178
.map_err(|e| io::Error::new(io::ErrorKind::Other, Box::new(e)))?;
181179
}
182180
fresh_dict
183-
.as_dict_mut()
184-
.unwrap()
185181
.flush()
186182
.map_err(|e| io::Error::new(io::ErrorKind::Other, Box::new(e)))?;
187183
}
@@ -197,14 +193,10 @@ impl UserDictionaryLoader {
197193
let freq = phrase.freq();
198194
let last_used = phrase.last_used().unwrap_or_default();
199195
fresh_dict
200-
.as_dict_mut()
201-
.unwrap()
202196
.update_phrase(&syllables, phrase, freq, last_used)
203197
.map_err(|e| io::Error::other(Box::new(e)))?;
204198
}
205199
fresh_dict
206-
.as_dict_mut()
207-
.unwrap()
208200
.flush()
209201
.map_err(|e| io::Error::other(Box::new(e)))?;
210202
}

src/dictionary/mod.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ pub enum LookupStrategy {
323323
/// ```
324324
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
325325
///
326-
/// use chewing::{dictionary::{Dictionary, DictionaryMut, LookupStrategy, TrieBuf}, syl, zhuyin::Bopomofo};
326+
/// use chewing::{dictionary::{Dictionary, LookupStrategy, TrieBuf}, syl, zhuyin::Bopomofo};
327327
///
328328
/// let mut dict = TrieBuf::new_in_memory();
329329
/// dict.add_phrase(&[syl![Bopomofo::C, Bopomofo::E, Bopomofo::TONE4]], ("測", 100).into())?;
@@ -348,21 +348,20 @@ pub trait Dictionary: Debug {
348348
fn about(&self) -> DictionaryInfo;
349349
/// Returns the dictionary file path if it's backed by a file.
350350
fn path(&self) -> Option<&Path>;
351-
fn as_dict_mut(&mut self) -> Option<&mut dyn DictionaryMut>;
352-
}
353-
354-
/// An interface for updating dictionaries.
355-
pub trait DictionaryMut: Debug {
356351
/// Reopens the dictionary if it was changed by a different process
357352
///
358353
/// It should not fail if the dictionary is read-only or able to sync across
359354
/// processes automatically.
360-
fn reopen(&mut self) -> Result<(), UpdateDictionaryError>;
355+
fn reopen(&mut self) -> Result<(), UpdateDictionaryError> {
356+
Err(UpdateDictionaryError { source: None })
357+
}
361358
/// Flushes all the changes back to the filesystem
362359
///
363360
/// The change made to the dictionary might not be persisted without
364361
/// calling this method.
365-
fn flush(&mut self) -> Result<(), UpdateDictionaryError>;
362+
fn flush(&mut self) -> Result<(), UpdateDictionaryError> {
363+
Err(UpdateDictionaryError { source: None })
364+
}
366365
/// An method for updating dictionaries.
367366
///
368367
/// For more about the concept of dictionaries generally, please see the
@@ -373,7 +372,7 @@ pub trait DictionaryMut: Debug {
373372
/// ```
374373
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
375374
///
376-
/// use chewing::{dictionary::{DictionaryMut, TrieBuf}, syl, zhuyin::Bopomofo};
375+
/// use chewing::{dictionary::{Dictionary, TrieBuf}, syl, zhuyin::Bopomofo};
377376
///
378377
/// let mut dict = TrieBuf::new_in_memory();
379378
/// dict.add_phrase(&[syl![Bopomofo::C, Bopomofo::E, Bopomofo::TONE4]], ("測", 100).into())?;
@@ -383,25 +382,29 @@ pub trait DictionaryMut: Debug {
383382
/// TODO: doc
384383
fn add_phrase(
385384
&mut self,
386-
syllables: &[Syllable],
387-
phrase: Phrase,
388-
) -> Result<(), UpdateDictionaryError>;
389-
385+
_syllables: &[Syllable],
386+
_phrase: Phrase,
387+
) -> Result<(), UpdateDictionaryError> {
388+
Err(UpdateDictionaryError { source: None })
389+
}
390390
/// TODO: doc
391391
fn update_phrase(
392392
&mut self,
393-
syllables: &[Syllable],
394-
phrase: Phrase,
395-
user_freq: u32,
396-
time: u64,
397-
) -> Result<(), UpdateDictionaryError>;
398-
393+
_syllables: &[Syllable],
394+
_phrase: Phrase,
395+
_user_freq: u32,
396+
_time: u64,
397+
) -> Result<(), UpdateDictionaryError> {
398+
Err(UpdateDictionaryError { source: None })
399+
}
399400
/// TODO: doc
400401
fn remove_phrase(
401402
&mut self,
402-
syllables: &[Syllable],
403-
phrase_str: &str,
404-
) -> Result<(), UpdateDictionaryError>;
403+
_syllables: &[Syllable],
404+
_phrase_str: &str,
405+
) -> Result<(), UpdateDictionaryError> {
406+
Err(UpdateDictionaryError { source: None })
407+
}
405408
}
406409

407410
/// Errors during dictionary construction.
@@ -447,12 +450,11 @@ pub trait DictionaryBuilder {
447450

448451
#[cfg(test)]
449452
mod tests {
450-
use crate::dictionary::{Dictionary, DictionaryBuilder, DictionaryMut};
453+
use crate::dictionary::{Dictionary, DictionaryBuilder};
451454

452455
#[test]
453456
fn ensure_object_safe() {
454457
const _: Option<&dyn Dictionary> = None;
455-
const _: Option<&dyn DictionaryMut> = None;
456458
const _: Option<&dyn DictionaryBuilder> = None;
457459
}
458460
}

src/dictionary/sqlite.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::{
99
use rusqlite::{Connection, Error as RusqliteError, OpenFlags, OptionalExtension, params};
1010

1111
use super::{
12-
BuildDictionaryError, Dictionary, DictionaryBuilder, DictionaryInfo, DictionaryMut, Entries,
13-
LookupStrategy, Phrase, UpdateDictionaryError,
12+
BuildDictionaryError, Dictionary, DictionaryBuilder, DictionaryInfo, Entries, LookupStrategy,
13+
Phrase, UpdateDictionaryError,
1414
};
1515
use crate::zhuyin::Syllable;
1616

@@ -383,17 +383,16 @@ impl Dictionary for SqliteDictionary {
383383
self.path.as_ref().map(|p| p as &Path)
384384
}
385385

386-
fn as_dict_mut(&mut self) -> Option<&mut dyn DictionaryMut> {
387-
if !self.readonly { Some(self) } else { None }
388-
}
389-
}
390-
391-
impl DictionaryMut for SqliteDictionary {
392386
fn reopen(&mut self) -> Result<(), UpdateDictionaryError> {
393387
Ok(())
394388
}
395389

396390
fn flush(&mut self) -> Result<(), UpdateDictionaryError> {
391+
if self.readonly {
392+
return Err(UpdateDictionaryError {
393+
source: Some(Box::new(SqliteDictionaryError::ReadOnly)),
394+
});
395+
}
397396
self.conn.pragma_update(None, "wal_checkpoint", "PASSIVE")?;
398397
Ok(())
399398
}
@@ -594,8 +593,7 @@ mod tests {
594593
use super::SqliteDictionary;
595594
use crate::{
596595
dictionary::{
597-
Dictionary, DictionaryBuilder, DictionaryMut, LookupStrategy, Phrase,
598-
SqliteDictionaryBuilder,
596+
Dictionary, DictionaryBuilder, LookupStrategy, Phrase, SqliteDictionaryBuilder,
599597
},
600598
syl,
601599
zhuyin::Bopomofo,
@@ -666,7 +664,7 @@ mod tests {
666664
let mut dict =
667665
SqliteDictionary::open_readonly(&temp_path).expect("Unable to open database");
668666
assert_eq!(temp_path.to_path_buf(), dict.path().unwrap());
669-
assert!(dict.as_dict_mut().is_none());
667+
assert!(dict.flush().is_err());
670668
}
671669

672670
#[test]

src/dictionary/trie.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,6 @@ impl Dictionary for Trie {
419419
fn path(&self) -> Option<&Path> {
420420
self.path.as_ref().map(|p| p as &Path)
421421
}
422-
423-
fn as_dict_mut(&mut self) -> Option<&mut dyn super::DictionaryMut> {
424-
None
425-
}
426422
}
427423

428424
fn context_specific<T: EncodeValue + Tagged>(

src/dictionary/trie_buf.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::{
1010
use tracing::{error, info};
1111

1212
use super::{
13-
BuildDictionaryError, Dictionary, DictionaryBuilder, DictionaryInfo, DictionaryMut, Entries,
14-
LookupStrategy, Phrase, Trie, TrieBuilder, UpdateDictionaryError,
13+
BuildDictionaryError, Dictionary, DictionaryBuilder, DictionaryInfo, Entries, LookupStrategy,
14+
Phrase, Trie, TrieBuilder, UpdateDictionaryError,
1515
};
1616
use crate::zhuyin::Syllable;
1717

@@ -304,12 +304,6 @@ impl Dictionary for TrieBuf {
304304
self.trie.as_ref()?.path()
305305
}
306306

307-
fn as_dict_mut(&mut self) -> Option<&mut dyn DictionaryMut> {
308-
Some(self)
309-
}
310-
}
311-
312-
impl DictionaryMut for TrieBuf {
313307
fn reopen(&mut self) -> Result<(), UpdateDictionaryError> {
314308
self.sync()?;
315309
Ok(())
@@ -375,7 +369,7 @@ mod tests {
375369

376370
use super::{Dictionary, TrieBuf};
377371
use crate::{
378-
dictionary::{DictionaryMut, LookupStrategy, Phrase},
372+
dictionary::{LookupStrategy, Phrase},
379373
syl,
380374
zhuyin::Bopomofo::*,
381375
};

src/editor/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use crate::{
2929
special_symbol_input,
3030
},
3131
dictionary::{
32-
DEFAULT_DICT_NAMES, Dictionary, DictionaryMut, Layered, LookupStrategy,
33-
SystemDictionaryLoader, UpdateDictionaryError, UserDictionaryLoader,
32+
DEFAULT_DICT_NAMES, Dictionary, Layered, LookupStrategy, SystemDictionaryLoader,
33+
UpdateDictionaryError, UserDictionaryLoader,
3434
},
3535
input::{KeyState, KeyboardEvent, keysym::*},
3636
zhuyin::Syllable,

0 commit comments

Comments
 (0)