pub struct MonolingualDictionaryService {
client: MonolingualClient,
db: Db,
dict_dir: PathBuf,
pending_installs: Arc<Mutex<HashSet<String>>>,
}Expand description
Provides monolingual dictionary management: querying available dictionaries, listing installed ones, and downloading + extracting new ones.
All network metadata is cached in the application SQLite database.
Downloaded dictionaries are extracted to
<dict_dir>/reader-dict/<lang>/.
The service is cheaply cloneable (Arc-backed). All clones share the same
pending_installs set, so concurrent-download guards work correctly across
the UI thread (which holds the original) and background threads (which hold
clones).
Fields§
§client: MonolingualClient§db: Db§dict_dir: PathBuf§pending_installs: Arc<Mutex<HashSet<String>>>Implementations§
Source§impl MonolingualDictionaryService
impl MonolingualDictionaryService
Sourcepub fn get_available_dictionaries(
&self,
) -> Result<Vec<(String, DictionaryEntry)>, MonolingualError>
pub fn get_available_dictionaries( &self, ) -> Result<Vec<(String, DictionaryEntry)>, MonolingualError>
Returns all dictionaries available for download from the remote API.
Metadata is served from the SQLite cache when available; otherwise a network request is made and the result is cached.
§Errors
Returns an error if the metadata cannot be loaded from cache or network.
Sourcepub fn get_entry_for_lang(
&self,
lang: &str,
) -> Result<Option<DictionaryEntry>, MonolingualError>
pub fn get_entry_for_lang( &self, lang: &str, ) -> Result<Option<DictionaryEntry>, MonolingualError>
Returns the cached metadata entry for a single language.
This does not make any network requests. Returns None if no entry for
lang has been cached yet.
§Errors
Returns an error if the database read fails.
Sourcepub fn get_installed_dictionaries(
&self,
) -> Result<Vec<String>, MonolingualError>
pub fn get_installed_dictionaries( &self, ) -> Result<Vec<String>, MonolingualError>
Returns the language codes of all locally installed dictionaries.
A dictionary is considered installed when its language directory exists
inside <dict_dir>/reader-dict/ and contains at least one .index
file paired with a .dict or .dict.dz file.
§Errors
Returns an error if the directory cannot be read.
Sourcepub fn is_installing(&self, lang: &str) -> bool
pub fn is_installing(&self, lang: &str) -> bool
Returns true if a download is already in progress for lang.
This can be used by callers to suppress duplicate install requests before spawning a background thread.
Sourcepub fn install_dictionary<F>(
&self,
lang: &str,
entry: &DictionaryEntry,
include_etymologies: bool,
progress_callback: &mut F,
) -> Result<(), MonolingualError>
pub fn install_dictionary<F>( &self, lang: &str, entry: &DictionaryEntry, include_etymologies: bool, progress_callback: &mut F, ) -> Result<(), MonolingualError>
Downloads and installs a dictionary for the given language.
The archive is downloaded to a temporary file, then extracted into
<dict_dir>/reader-dict/<lang>/ and the files are renamed to
Reader-Dict-<lang>.index and Reader-Dict-<lang>.dict[.dz]. Any
existing files in that directory are overwritten.
Returns MonolingualError::InstallationInProgress immediately if a
download for the same language is already running. The caller should
check Self::is_installing on the UI thread before spawning a thread
to get a user-visible early exit; this check inside install_dictionary
provides a safety net against races.
§Arguments
entry- Metadata entry for the dictionary to install. The language code and version are derived from this entry.include_etymologies- Whentrue, the full archive (with etymologies) is downloaded; whenfalse, the smaller no-etymology variant is used.progress_callback- Called after each downloaded chunk with(bytes_downloaded_so_far, total_bytes).
§Errors
Returns an error if a download for the language is already in progress, if the download fails, if the archive cannot be parsed, or if files cannot be written to disk.
fn do_install<F>( &self, lang: &str, entry: &DictionaryEntry, include_etymologies: bool, progress_callback: &mut F, ) -> Result<(), MonolingualError>
Sourcepub fn remove_installed(&self, lang: &str)
pub fn remove_installed(&self, lang: &str)
Removes the installed dictionary record for lang from the database.
Logs a warning on failure rather than propagating the error, as this is a best-effort cleanup step called from event handlers.
Sourcepub fn is_update_available(&self, lang: &str) -> bool
pub fn is_update_available(&self, lang: &str) -> bool
Returns true if a newer version of the dictionary for lang is
available on the server than the currently installed version.
Returns false on any error to avoid surfacing spurious update badges.
fn load_metadata( &self, ) -> Result<HashMap<String, HashMap<String, DictionaryEntry>>, MonolingualError>
fn fetch_and_cache_metadata( &self, ) -> Result<HashMap<String, HashMap<String, DictionaryEntry>>, MonolingualError>
fn get_cached_metadata( &self, ) -> Result<Option<HashMap<String, HashMap<String, DictionaryEntry>>>, MonolingualError>
fn reader_dict_dir(&self) -> PathBuf
fn lang_dir(&self, lang: &str) -> PathBuf
Trait Implementations§
Source§impl Clone for MonolingualDictionaryService
impl Clone for MonolingualDictionaryService
Source§fn clone(&self) -> MonolingualDictionaryService
fn clone(&self) -> MonolingualDictionaryService
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for MonolingualDictionaryService
impl !RefUnwindSafe for MonolingualDictionaryService
impl Send for MonolingualDictionaryService
impl Sync for MonolingualDictionaryService
impl Unpin for MonolingualDictionaryService
impl UnsafeUnpin for MonolingualDictionaryService
impl !UnwindSafe for MonolingualDictionaryService
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more