From 3661173bb012296b1b6048f5b857801611c9e58a Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Thu, 20 Aug 2026 00:42:06 +0300 Subject: [PATCH] Final code cleanup --- src/lang.rs | 92 +++++++++++++++++++++++------------------------ src/main.rs | 1 + src/navigation.rs | 2 +- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/lang.rs b/src/lang.rs index 5d38442..921b9ca 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -1,9 +1,10 @@ -use crate::AppState; use crate::data_provider::card_stats::{delete_stat, load_stats_of_deck, update_stat_score}; -use crate::data_provider::history::{HistoryItem, push_note}; +use crate::data_provider::history::{push_note, HistoryItem}; +use crate::AppState; use chrono::{DateTime, Utc}; -use rand::distr::Distribution; +use parking_lot::Mutex; use rand::distr::weighted::WeightedIndex; +use rand::distr::Distribution; use rand::prelude::SliceRandom; use rand::rng; use rand::rngs::ThreadRng; @@ -11,15 +12,14 @@ use rayon::iter::IndexedParallelIterator; use rayon::iter::IntoParallelRefIterator; use rayon::iter::ParallelIterator; use rhai::{Engine, Scope}; -use rusqlite::ToSql; use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef}; +use rusqlite::ToSql; use std::cmp::PartialEq; use std::collections::HashMap; use std::fmt::{Display, Formatter}; use std::num::ParseIntError; use std::str::FromStr; use std::sync::Arc; -use parking_lot::Mutex; use std::time::Instant; const MAX_HISTORY_LEN: usize = 20; @@ -668,49 +668,47 @@ impl DeckSettings { let engine = Engine::new(); let ast = engine.compile(&self.filter); - if ast.is_err() { - return list; + if let Ok(ast) = ast { + let groups = &state.word_groups; + + list = state + .dictionary + .par_iter() + .enumerate() + .filter(|(_, word)| { + let mut more = rhai::Map::new(); + for iced in &word.additional { + more.insert(iced.0.clone().into(), iced.1.clone().into()); + } + let mut scope = Scope::new(); + scope + .push_constant("id", word.id.0) + .push_constant("key", word.key.clone()) + .push_constant("value", word.value.clone()) + .push_constant("tags", word.tags.clone()) + .push_constant("more", more) + .push_constant( + "group", + groups + .iter() + .find(|g| g.id == word.group_id) + .cloned() + .unwrap() + .name, + ); + + let result = engine.eval_ast_with_scope::(&mut scope, &ast); + result.is_ok() && result.unwrap() + }) + .map(|(index, _)| index) + .collect(); + + println!("Collecting available words is {:?}", time.elapsed()); + + list + } else { + list } - - let ast = ast.unwrap(); - - let groups = &state.word_groups; - - list = state - .dictionary - .par_iter() - .enumerate() - .filter(|(_, word)| { - let mut more = rhai::Map::new(); - for iced in &word.additional { - more.insert(iced.0.clone().into(), iced.1.clone().into()); - } - let mut scope = Scope::new(); - scope - .push_constant("id", word.id.0) - .push_constant("key", word.key.clone()) - .push_constant("value", word.value.clone()) - .push_constant("tags", word.tags.clone()) - .push_constant("more", more) - .push_constant( - "group", - groups - .iter() - .find(|g| g.id == word.group_id) - .cloned() - .unwrap() - .name, - ); - - let result = engine.eval_ast_with_scope::(&mut scope, &ast); - result.is_ok() && result.unwrap() - }) - .map(|(index, _)| index) - .collect(); - - println!("Collecting available words is {:?}", time.elapsed()); - - list } pub fn require_speech(&self) -> bool { self.forward == "speech" || self.backward == "speech" diff --git a/src/main.rs b/src/main.rs index e9706c1..d04e06a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,7 @@ fn main() -> iced::Result { .run() } +#[allow(unused_mut)] fn window_settings() -> window::Settings { let mut settings = window::Settings { position: Position::Centered, diff --git a/src/navigation.rs b/src/navigation.rs index 4709400..8f8ed1d 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -132,7 +132,7 @@ impl ScreenState { Ok(v) => v, }; - let id: Id = history_file_name[4..history_file_name.len() - 12] + let id = history_file_name[4..history_file_name.len() - 12] .parse::() .unwrap();