Extract all entities to entities.rs

This commit is contained in:
micialware committed 2026-08-24 11:40:31 +03:00
1 parent c67663141f
commit 92ca42869c
9 files changed
+135 -128

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@ use iced_core::window::Position;
use iced_core::window::settings::PlatformSpecific;
use log::info;
use rusqlite::Connection;
use crate::core::deck::DeckSettings;
use crate::data::entities::DeckSettings;
use crate::data_provider::card_sets::load_sets;
use crate::data_provider::settings::get_setting;
use crate::data_provider::sqlite::default_connection;
-115
View File
@@ -14,87 +14,6 @@ use crate::data_provider::history::{push_note};
use crate::data::entities::*;
use crate::data::Id;
#[derive(Clone)]
pub struct DeckSettings {
pub id: Id,
pub name: String,
pub forward: String,
pub backward: String,
pub filter: String,
pub count: Option<usize>,
pub open_mode: OrderMode,
}
impl DeckSettings {
pub(crate) fn with_name(name: String) -> DeckSettings {
DeckSettings {
id: 0.into(),
name,
forward: "".to_string(),
backward: "".to_string(),
filter: "true".to_string(),
count: None,
open_mode: OrderMode::Default,
}
}
pub fn check_filter(&self) -> bool {
let time = Instant::now();
let engine = Engine::new();
let ast = engine.compile(&self.filter);
info!("Compile time is {:?}", time.elapsed());
ast.is_ok()
}
pub fn get_word_list(&self, state: &AppState) -> Vec<usize> {
let time = Instant::now();
let mut list = vec![];
let engine = Engine::new();
let ast = engine.compile(&self.filter);
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 as i64)
.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::<bool>(&mut scope, &ast);
result.is_ok() && result.unwrap()
})
.map(|(index, _)| index)
.collect();
info!("Collecting available words is {:?}", time.elapsed());
list
} else {
list
}
}
pub fn require_speech(&self) -> bool {
self.forward == "speech" || self.backward == "speech"
}
}
#[derive(Clone)]
pub struct DeckData {
@@ -209,37 +128,3 @@ impl DeckData {
}
}
#[derive(Clone, PartialEq)]
pub struct CardStatistics {
pub id: Id,
pub word_id: Id,
pub set_id: Id,
pub last_open: DateTime<Utc>,
pub score: u8,
}
impl CardStatistics {
pub fn update(&mut self, status: WordOpenMode) {
match status {
WordOpenMode::Easy => {
self.score = (self.calculated_score() + 5.0).round() as u8;
}
WordOpenMode::Ok => self.score = (self.calculated_score() + 2.0).round() as u8,
WordOpenMode::Hard => {
self.score = (self.calculated_score() * 0.75).round() as u8;
}
WordOpenMode::None => {
self.score = (self.calculated_score() * 0.4) as u8;
}
}
self.score = self.score.clamp(1, MAX_SCORE);
self.last_open = Utc::now();
}
pub fn calculated_score(&self) -> f32 {
let time = Utc::now() - self.last_open;
let days = time.num_days();
let multiplier = FADE_PER_DAY.powi(days as i32);
(self.score as f32 * multiplier).max(1.0)
}
}
+2 -1
View File
@@ -1,8 +1,9 @@
use crate::core::deck::{CardStatistics, DeckData};
use crate::core::deck::DeckData;
use rand::distr::weighted::WeightedIndex;
use rand::prelude::ThreadRng;
use rand::prelude::{Distribution, SliceRandom};
use rand::rng;
use crate::data::entities::CardStatistics;
const MAX_HISTORY_LEN: usize = 20;
const MAX_HISTORY_LEN_PART: f32 = 0.33;
+125 -4
View File
@@ -1,7 +1,12 @@
use std::collections::HashMap;
use crate::app::AppState;
use crate::core::srs::{FADE_PER_DAY, MAX_SCORE, OrderMode, WordOpenMode};
use crate::data::{INVALID_ID, Id};
use chrono::{DateTime, Utc};
use crate::core::srs::WordOpenMode;
use crate::data::{Id, INVALID_ID};
use log::info;
use rayon::prelude::*;
use rhai::{Engine, Scope};
use std::collections::HashMap;
use std::time::Instant;
#[derive(Clone, Debug)]
pub struct WordData {
@@ -18,7 +23,6 @@ pub struct WordGroup {
pub name: String,
}
impl WordData {
pub fn new() -> Self {
Self {
@@ -40,3 +44,120 @@ pub struct HistoryItem {
pub before: u8,
pub after: u8,
}
#[derive(Clone)]
pub struct DeckSettings {
pub id: Id,
pub name: String,
pub forward: String,
pub backward: String,
pub filter: String,
pub count: Option<usize>,
pub open_mode: OrderMode,
}
impl DeckSettings {
pub(crate) fn with_name(name: String) -> DeckSettings {
DeckSettings {
id: 0.into(),
name,
forward: "".to_string(),
backward: "".to_string(),
filter: "true".to_string(),
count: None,
open_mode: OrderMode::Default,
}
}
pub fn check_filter(&self) -> bool {
let time = Instant::now();
let engine = Engine::new();
let ast = engine.compile(&self.filter);
info!("Compile time is {:?}", time.elapsed());
ast.is_ok()
}
pub fn get_word_list(&self, state: &AppState) -> Vec<usize> {
let time = Instant::now();
let mut list = vec![];
let engine = Engine::new();
let ast = engine.compile(&self.filter);
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 as i64)
.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::<bool>(&mut scope, &ast);
result.is_ok() && result.unwrap()
})
.map(|(index, _)| index)
.collect();
info!("Collecting available words is {:?}", time.elapsed());
list
} else {
list
}
}
pub fn require_speech(&self) -> bool {
self.forward == "speech" || self.backward == "speech"
}
}
#[derive(Clone, PartialEq)]
pub struct CardStatistics {
pub id: Id,
pub word_id: Id,
pub set_id: Id,
pub last_open: DateTime<Utc>,
pub score: u8,
}
impl CardStatistics {
pub fn update(&mut self, status: WordOpenMode) {
match status {
WordOpenMode::Easy => {
self.score = (self.calculated_score() + 5.0).round() as u8;
}
WordOpenMode::Ok => self.score = (self.calculated_score() + 2.0).round() as u8,
WordOpenMode::Hard => {
self.score = (self.calculated_score() * 0.75).round() as u8;
}
WordOpenMode::None => {
self.score = (self.calculated_score() * 0.4) as u8;
}
}
self.score = self.score.clamp(1, MAX_SCORE);
self.last_open = Utc::now();
}
pub fn calculated_score(&self) -> f32 {
let time = Utc::now() - self.last_open;
let days = time.num_days();
let multiplier = FADE_PER_DAY.powi(days as i32);
(self.score as f32 * multiplier).max(1.0)
}
}
+2 -2
View File
@@ -1,7 +1,7 @@
use crate::core::srs::{ OrderMode};
use crate::core::srs::OrderMode;
use crate::data::entities::DeckSettings;
use rusqlite::Connection;
use crate::core::deck::DeckSettings;
pub fn load_sets(connection: &Connection) -> Vec<DeckSettings> {
let mut stmt = connection
+1 -1
View File
@@ -1,7 +1,7 @@
use rusqlite::{Connection, params};
use std::time::Instant;
use log::info;
use crate::core::deck::{CardStatistics, DeckSettings};
use crate::data::entities::{CardStatistics, DeckSettings};
pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<CardStatistics> {
let mut stmt = connection
+2 -2
View File
@@ -1,7 +1,7 @@
use crate::app::AppState;
use crate::core::deck::{CardStatistics, DeckData, DeckSettings};
use crate::core::deck::DeckData;
use crate::core::srs::WordOpenMode;
use crate::data::entities::WordData;
use crate::data::entities::{CardStatistics, DeckSettings, WordData};
use crate::data::Id;
use crate::data_provider::voice::get_voice;
use crate::navigation::Page::PreviousPage;
+1 -1
View File
@@ -15,7 +15,7 @@ use parking_lot::Mutex;
use std::sync::Arc;
use std::time::Duration;
use crate::app::AppState;
use crate::core::deck::DeckSettings;
use crate::data::entities::DeckSettings;
pub struct RepetitionSettingsState {
set: DeckSettings,
+1 -1
View File
@@ -24,7 +24,7 @@ use parking_lot::Mutex;
use std::ops::Deref;
use std::sync::Arc;
use crate::app::AppState;
use crate::core::deck::{CardStatistics, DeckSettings};
use crate::data::entities::{CardStatistics, DeckSettings};
use crate::core::srs::{AppendMode, OrderMode};
use crate::data::Id;