Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/dictionary/layered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ impl Dictionary for Layered {
None
}

fn is_readonly(&self) -> bool {
self.sys_dict
.iter()
.chain(iter::once(&self.user_dict))
.any(|d| !d.is_readonly())
}

fn reopen(&mut self) -> Result<(), UpdateDictionaryError> {
self.user_dict.reopen()
}
Expand Down Expand Up @@ -347,6 +354,7 @@ mod tests {
dict.remove_phrase(&[syl![Bopomofo::C, Bopomofo::E, Bopomofo::TONE4]], "冊")
.is_err()
);
assert!(dict.is_readonly());
Ok(())
}
}
2 changes: 2 additions & 0 deletions src/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ pub trait Dictionary: Debug {
fn about(&self) -> DictionaryInfo;
/// Returns the dictionary file path if it's backed by a file.
fn path(&self) -> Option<&Path>;
/// Returns whether the dictionary was opened readonly.
fn is_readonly(&self) -> bool;
/// Reopens the dictionary if it was changed by a different process
///
/// It should not fail if the dictionary is read-only or able to sync across
Expand Down
4 changes: 4 additions & 0 deletions src/dictionary/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ impl Dictionary for SqliteDictionary {
self.path.as_ref().map(|p| p as &Path)
}

fn is_readonly(&self) -> bool {
self.readonly
}

fn reopen(&mut self) -> Result<(), UpdateDictionaryError> {
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions src/dictionary/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ impl Dictionary for Trie {
fn path(&self) -> Option<&Path> {
self.path.as_ref().map(|p| p as &Path)
}

fn is_readonly(&self) -> bool {
true
}
}

fn context_specific<T: EncodeValue + Tagged>(
Expand Down
8 changes: 8 additions & 0 deletions src/dictionary/trie_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct TrieBuf {
graveyard: BTreeSet<PhraseKey>,
join_handle: Option<JoinHandle<Result<(), UpdateDictionaryError>>>,
dirty: bool,
readonly: bool,
}

type PhraseKey = (Cow<'static, [Syllable]>, Cow<'static, str>);
Expand Down Expand Up @@ -61,6 +62,7 @@ impl TrieBuf {
graveyard: BTreeSet::new(),
join_handle: None,
dirty: false,
readonly: false,
})
}

Expand All @@ -72,6 +74,7 @@ impl TrieBuf {
graveyard: BTreeSet::new(),
join_handle: None,
dirty: false,
readonly: false,
}
}

Expand Down Expand Up @@ -257,6 +260,7 @@ impl TrieBuf {
graveyard: self.graveyard.clone(),
join_handle: None,
dirty: false,
readonly: false,
};
self.join_handle = Some(thread::spawn(move || {
let mut builder = TrieBuilder::new();
Expand Down Expand Up @@ -304,6 +308,10 @@ impl Dictionary for TrieBuf {
self.trie.as_ref()?.path()
}

fn is_readonly(&self) -> bool {
self.readonly
}

fn reopen(&mut self) -> Result<(), UpdateDictionaryError> {
self.sync()?;
Ok(())
Expand Down
Loading