Files
jap_learn/src/data/entities.rs
T

287 lines
6.6 KiB
Rust
Raw Normal View History

2026-09-04 17:26:59 +03:00
use crate::core::srs::{FADE_PER_DAY, MAX_SCORE, WordOpenMode};
2026-08-24 11:40:31 +03:00
use crate::data::{INVALID_ID, Id};
2026-09-07 10:53:09 +03:00
use chrono::{DateTime, Local, NaiveDate, Utc};
2026-08-29 22:02:01 +03:00
use fsrs::MemoryState;
2026-08-31 11:26:05 +03:00
use litemap::LiteMap;
2026-08-24 11:40:31 +03:00
use log::info;
2026-09-02 00:19:58 +03:00
use rhai::Engine;
2026-08-30 19:22:28 +03:00
use std::cmp::Ordering;
2026-09-02 00:19:58 +03:00
use std::hash::{Hash, Hasher};
2026-08-25 15:46:39 +03:00
use std::marker::PhantomData;
2026-09-07 10:53:09 +03:00
use std::sync::Arc;
2026-08-24 11:40:31 +03:00
use std::time::Instant;
2026-09-07 10:53:09 +03:00
use crate::data_provider::history::{HistoryRepository, HistoryRepositoryInner};
2026-08-24 11:35:37 +03:00
#[derive(Clone, Debug)]
pub struct WordData {
pub id: Id,
pub group_id: Id,
2026-08-24 11:35:37 +03:00
pub key: String,
pub value: String,
pub tags: String,
pub additional: LiteMap<String, String>,
2026-08-24 11:35:37 +03:00
}
2026-08-26 17:53:09 +03:00
impl EntityData for WordData {
fn id(&self) -> Id {
self.id
}
fn set_id(&mut self, id: Id) {
self.id = id;
}
}
2026-08-24 11:35:37 +03:00
#[derive(Clone)]
pub struct WordGroup {
pub id: Id,
pub name: String,
}
impl EntityData for WordGroup {
fn id(&self) -> Id {
self.id
}
fn set_id(&mut self, id: Id) {
self.id = id;
}
}
2026-08-24 11:35:37 +03:00
impl WordData {
pub fn new() -> Self {
Self {
id: INVALID_ID,
key: String::new(),
value: String::new(),
tags: String::new(),
additional: Default::default(),
group_id: 1.into(),
}
}
}
#[derive(Clone)]
2026-09-04 11:18:23 +03:00
pub struct HistoryNote {
pub id: Id,
2026-08-24 11:35:37 +03:00
pub timestamp: DateTime<Utc>,
2026-09-07 10:53:09 +03:00
pub deck_id: Id,
2026-08-24 11:35:37 +03:00
pub word_id: Id,
2026-09-04 11:18:23 +03:00
pub answer: WordOpenMode,
2026-08-24 11:35:37 +03:00
pub before: u8,
pub after: u8,
2026-09-04 11:18:23 +03:00
pub fsrs: bool
}
impl EntityData for HistoryNote {
fn id(&self) -> Id {
self.id
}
fn set_id(&mut self, id: Id) {
self.id = id;
}
2026-08-24 11:40:31 +03:00
}
2026-09-07 10:53:09 +03:00
impl HistoryRepositoryInner {
pub fn activity_of_deck(&self, id: Id) -> Vec<(NaiveDate, u32)> {
let mut query = self.query(Arc::new(move |note| note.deck_id == id));
let values = query.iter(&self).collect::<Vec<_>>();
let mut counts = vec![];
if !values.is_empty() {
let mut date = values[0].timestamp.with_timezone(&Local).date_naive();
let mut count = 0;
for note in values {
let current_date = note.timestamp.with_timezone(&Local).date_naive();
if date == current_date{
count += 1;
}
else {
counts.push((date, count));
date = current_date;
count = 0;
}
}
}
counts
}
}
2026-08-24 11:40:31 +03:00
#[derive(Clone)]
pub struct DeckSettings {
pub id: Id,
pub name: String,
pub forward: String,
pub backward: String,
pub filter: String,
}
2026-08-31 12:12:14 +03:00
impl EntityData for DeckSettings {
fn id(&self) -> Id {
self.id
}
fn set_id(&mut self, id: Id) {
self.id = id;
}
}
2026-08-24 11:40:31 +03:00
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(),
}
}
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 require_speech(&self) -> bool {
self.forward == "speech" || self.backward == "speech"
}
}
#[derive(Clone, PartialEq)]
2026-08-30 20:43:31 +03:00
pub struct CardState {
2026-08-24 11:40:31 +03:00
pub id: Id,
pub word_id: Id,
2026-09-06 23:55:50 +03:00
pub deck_id: Id,
2026-08-24 11:40:31 +03:00
pub last_open: DateTime<Utc>,
pub score: u8,
2026-08-28 18:52:38 +03:00
pub fsrs_opened: NaiveDate,
pub fsrs_memory: Option<MemoryState>,
pub fsrs_open_day: NaiveDate,
2026-08-24 11:40:31 +03:00
}
impl EntityData for CardState {
fn id(&self) -> Id {
self.id
}
fn set_id(&mut self, id: Id) {
self.id = id;
}
}
2026-08-30 20:43:31 +03:00
impl CardState {
2026-08-24 11:40:31 +03:00
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)
}
}
2026-08-25 15:46:39 +03:00
2026-08-31 14:35:11 +03:00
pub struct CustomField {
2026-08-31 11:52:54 +03:00
pub id: Id,
pub name: String,
}
2026-08-26 17:53:09 +03:00
pub struct EntityHandle<E> {
2026-08-25 15:46:39 +03:00
pub(crate) id: Id,
2026-08-30 20:32:16 +03:00
index: u32,
2026-08-27 09:21:16 +03:00
pub(crate) _phantom: PhantomData<E>,
2026-08-25 15:46:39 +03:00
}
2026-08-28 16:52:48 +03:00
impl<T> Clone for EntityHandle<T> {
fn clone(&self) -> Self {
2026-08-30 19:22:28 +03:00
EntityHandle {
2026-08-28 16:52:48 +03:00
id: self.id,
index: self.index,
_phantom: Default::default(),
}
}
}
2026-09-02 00:19:58 +03:00
impl<T> Copy for EntityHandle<T> {}
2026-08-30 19:22:28 +03:00
impl<T> EntityHandle<T> {
2026-08-29 22:41:11 +03:00
pub fn new(id: Id, index: usize, real: bool) -> Self {
Self {
id,
2026-08-30 20:32:16 +03:00
index: ((index as u32) << 1 >> 1) | (!real as u32) << 31,
2026-08-30 19:22:28 +03:00
_phantom: Default::default(),
2026-08-29 22:41:11 +03:00
}
}
2026-09-06 23:55:50 +03:00
pub fn from_id(id: Id) -> Self {
2026-09-07 15:48:10 +03:00
Self::new(id, id.0 as usize, true)
2026-09-06 23:55:50 +03:00
}
2026-08-29 22:41:11 +03:00
pub fn index(&self) -> usize {
(self.index << 1 >> 1) as usize
}
pub fn set_index(&mut self, index: usize) {
2026-08-30 20:32:16 +03:00
self.index = (self.index >> 31 << 31) | ((index as u32) << 1 >> 1);
2026-08-29 22:41:11 +03:00
}
pub fn real(&self) -> bool {
(self.index >> 31) == 0
}
pub fn set_real(&mut self, value: bool) {
2026-08-30 20:32:16 +03:00
self.index = self.index << 1 >> 1 | (!value as u32) << 31;
2026-08-29 22:41:11 +03:00
}
}
2026-08-28 16:52:48 +03:00
impl<T> Eq for EntityHandle<T> {}
impl<T> PartialEq<Self> for EntityHandle<T> {
fn eq(&self, other: &Self) -> bool {
2026-08-29 22:41:11 +03:00
self.real() == other.real() && self.id == other.id
2026-08-28 16:52:48 +03:00
}
}
impl<T> PartialOrd<Self> for EntityHandle<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T> Ord for EntityHandle<T> {
fn cmp(&self, other: &Self) -> Ordering {
2026-08-29 22:41:11 +03:00
if self.real() && !other.real() {
2026-08-30 19:22:28 +03:00
return Ordering::Less;
2026-08-28 16:52:48 +03:00
}
2026-08-30 19:22:28 +03:00
if !self.real() && other.real() {
return Ordering::Greater;
2026-08-28 16:52:48 +03:00
}
2026-08-30 19:22:28 +03:00
2026-08-28 16:52:48 +03:00
self.id.cmp(&other.id)
}
}
2026-09-02 00:19:58 +03:00
impl<T> Hash for EntityHandle<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
self.real().hash(state);
}
}
2026-08-30 19:22:28 +03:00
pub trait EntityData {
2026-08-26 17:53:09 +03:00
fn id(&self) -> Id;
fn set_id(&mut self, id: Id);
2026-08-25 15:46:39 +03:00
}