Migrate to Id type

This commit is contained in:
2026-08-18 12:47:06 +03:00
parent 99b3818181
commit 97c76350e1
12 changed files with 106 additions and 62 deletions
+4 -4
View File
@@ -1,4 +1,4 @@
use crate::lang::{DeckSettings, OrderMode};
use crate::lang::{DeckSettings, OrderMode, INVALID_ID};
use rusqlite::Connection;
@@ -43,11 +43,11 @@ pub fn add_set(set: &mut DeckSettings, connection: &Connection) {
)
.unwrap_or_else(|e| {println!("{}", e); 0});
set.id = index;
set.id = index.into();
}
pub fn update_card_set(set: &mut DeckSettings, connection: &Connection) {
if set.id == 0 {
if set.id == INVALID_ID {
add_set(set, connection);
} else {
connection
@@ -66,7 +66,7 @@ pub fn update_card_set(set: &mut DeckSettings, connection: &Connection) {
}
pub fn delete_set(set: &DeckSettings, connection: &Connection) {
if set.id == 0 {
if set.id == INVALID_ID {
return;
}
connection
+3 -3
View File
@@ -1,4 +1,4 @@
use crate::lang::{DeckSettings, CardStatistics};
use crate::lang::{DeckSettings, CardStatistics, INVALID_ID};
use rusqlite::Connection;
use std::time::Instant;
@@ -62,7 +62,7 @@ pub fn add_stat_list(stat: &mut [CardStatistics], connection: &Connection) {
let start_index = last_index - (stat.len() as u32) + 1;
for (index, id) in (start_index..=last_index).enumerate() {
stat[index].id = id;
stat[index].id = id.into();
}
println!("Added {} cards for {:?}", stat.len(), time.elapsed());
@@ -103,7 +103,7 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
}
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
if stat.id == 0 {
if stat.id == INVALID_ID {
return;
}
connection
+5 -5
View File
@@ -1,5 +1,5 @@
use crate::dictionary::app_data_dir;
use crate::lang::WordOpenMode;
use crate::lang::{Id, WordOpenMode};
use chrono::{DateTime, Utc};
use std::fs;
use std::fs::{File, OpenOptions};
@@ -7,7 +7,7 @@ use std::io::Write;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
pub fn get_history_of_set(id: u32) -> Vec<HistoryItem> {
pub fn get_history_of_set(id: Id) -> Vec<HistoryItem> {
let app_dir = history_dir();
let head = app_dir.clone().join(format!("set_{}_history.csv", id));
let mut lines = Vec::new();
@@ -32,7 +32,7 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
if let [time, word, mode, before, after] = string.split(';').collect::<Vec<&str>>()[..] {
items.push(HistoryItem {
timestamp: DateTime::from_timestamp(time.parse().unwrap(), 0).unwrap(),
word_id: word.parse::<u32>().unwrap(),
word_id: word.parse::<u32>().unwrap().into(),
mode: match mode.parse::<u8>().unwrap() {
2 => WordOpenMode::Hard,
3 => WordOpenMode::Ok,
@@ -48,7 +48,7 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
items
}
pub fn push_note(set_id: u32, item: HistoryItem) {
pub fn push_note(set_id: Id, item: HistoryItem) {
let app_dir = history_dir();
let path = app_dir.clone().join(format!("set_{}_history.csv", set_id));
@@ -84,7 +84,7 @@ pub fn history_dir() -> PathBuf {
#[derive(Clone)]
pub struct HistoryItem {
pub timestamp: DateTime<Utc>,
pub word_id: u32,
pub word_id: Id,
pub mode: WordOpenMode,
pub before: u8,
pub after: u8,
+8 -8
View File
@@ -1,4 +1,4 @@
use crate::lang::{WordData, WordGroup};
use crate::lang::{WordData, WordGroup, INVALID_ID};
use rusqlite::{Connection, params};
use std::collections::HashMap;
@@ -20,7 +20,7 @@ pub fn add_word(word: &mut WordData, connection: &Connection) {
0
});
word.id = index;
word.id = index.into();
}
pub fn add_words(words: &mut [WordData], connection: &mut Connection) {
@@ -59,12 +59,12 @@ pub fn add_words(words: &mut [WordData], connection: &mut Connection) {
let start_index = last_index - (count as u32) + 1;
for (index, id) in (start_index..=last_index).enumerate() {
words[index].id = id;
words[index].id = id.into();
}
}
pub fn update_word(word: &mut WordData, connection: &Connection) {
if word.id == 0 {
if word.id == INVALID_ID {
add_word(word, connection);
} else {
connection
@@ -86,7 +86,7 @@ pub fn update_word(word: &mut WordData, connection: &Connection) {
}
pub fn delete_word(word: &WordData, connection: &Connection) {
if word.id == 0 {
if word.id == INVALID_ID {
return;
}
connection
@@ -156,11 +156,11 @@ pub fn add_group(group: &mut WordGroup, connection: &Connection) {
0
});
group.id = index;
group.id = index.into();
}
pub fn update_group(group: &mut WordGroup, connection: &Connection) {
if group.id == 0 {
if group.id == INVALID_ID {
add_group(group, connection);
} else {
connection
@@ -176,7 +176,7 @@ pub fn update_group(group: &mut WordGroup, connection: &Connection) {
}
pub fn delete_group(group: &WordGroup, connection: &Connection) {
if group.id == 0 {
if group.id == INVALID_ID {
return;
}
connection