Use is_valid for Id

This commit is contained in:
2026-08-18 17:31:19 +03:00
parent 3cd7c12baa
commit d3c28179b6
10 changed files with 36 additions and 32 deletions
+3 -3
View File
@@ -1,4 +1,4 @@
use crate::lang::{DeckSettings, OrderMode, INVALID_ID};
use crate::lang::{DeckSettings, INVALID_ID, OrderMode};
use rusqlite::Connection;
@@ -47,7 +47,7 @@ pub fn add_set(set: &mut DeckSettings, connection: &Connection) {
}
pub fn update_deck(set: &mut DeckSettings, connection: &Connection) {
if set.id == INVALID_ID {
if !set.id.is_valid() {
add_set(set, connection);
} else {
connection
@@ -66,7 +66,7 @@ pub fn update_deck(set: &mut DeckSettings, connection: &Connection) {
}
pub fn delete_set(set: &DeckSettings, connection: &Connection) {
if set.id == INVALID_ID {
if !set.id.is_valid() {
return;
}
connection
+2 -2
View File
@@ -1,4 +1,4 @@
use crate::lang::{DeckSettings, CardStatistics, INVALID_ID};
use crate::lang::{CardStatistics, DeckSettings, INVALID_ID};
use rusqlite::Connection;
use std::time::Instant;
@@ -103,7 +103,7 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
}
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
if stat.id == INVALID_ID {
if !stat.id.is_valid() {
return;
}
connection
+1 -1
View File
@@ -1,6 +1,6 @@
use hashbrown::HashMap;
use rusqlite::Connection;
use serde_json::Value;
use hashbrown::HashMap;
#[derive(Clone)]
pub struct ImportData(pub(crate) Vec<ImportGroup>);
+5 -5
View File
@@ -1,4 +1,4 @@
use crate::lang::{WordData, WordGroup, INVALID_ID};
use crate::lang::{INVALID_ID, WordData, WordGroup};
use rusqlite::{Connection, params};
use std::collections::HashMap;
@@ -64,7 +64,7 @@ pub fn add_words(words: &mut [WordData], connection: &mut Connection) {
}
pub fn update_word(word: &mut WordData, connection: &Connection) {
if word.id == INVALID_ID {
if !word.id.is_valid() {
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 == INVALID_ID {
if !word.id.is_valid() {
return;
}
connection
@@ -160,7 +160,7 @@ pub fn add_group(group: &mut WordGroup, connection: &Connection) {
}
pub fn update_group(group: &mut WordGroup, connection: &Connection) {
if group.id == INVALID_ID {
if !group.id.is_valid() {
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 == INVALID_ID {
if !group.id.is_valid() {
return;
}
connection
+4 -4
View File
@@ -2,13 +2,14 @@ use crate::data_provider::words::{delete_group, delete_word, update_group, updat
use crate::dictionary::DictionaryMessage::*;
use crate::dictionary_test::DictionaryQuizState;
use crate::import::ImportState;
use crate::lang::{WordData, WordGroup, INVALID_ID};
use crate::lang::{INVALID_ID, WordData, WordGroup};
use crate::navigation::Page::{Import, Word};
use crate::navigation::{NavigatedPage, Page};
use crate::styling::*;
use crate::word::WordState;
use crate::{AppState, RootMessage};
use chrono::{DateTime, TimeDelta, Utc};
use hashbrown::{HashMap, HashSet};
use iced::alignment::Vertical::Center;
use iced::widget::button::Style;
use iced::widget::button::{danger, text};
@@ -18,7 +19,6 @@ use iced::widget::*;
use iced::{Border, Color, Shadow, Task};
use iced_core::Length::Fill;
use rand::random_range;
use hashbrown::{HashMap, HashSet};
use std::fs;
use std::ops::Add;
use std::path::PathBuf;
@@ -96,7 +96,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
let dict = &state.dictionary;
word = dict[*index].clone();
}
if word.id != INVALID_ID {
if word.id.is_valid() {
return Some(Word(WordState::new(word, *index, self.state.clone())));
}
}
@@ -423,7 +423,7 @@ impl DictionaryState {
let line_button = || {
let action = WordAction(index);
if data.id == INVALID_ID {
if !data.id.is_valid() {
return button("-").on_press(action).style(|_x, _status| Style {
background: None,
text_color: Color::BLACK,
+6 -1
View File
@@ -28,7 +28,6 @@ const FADE_PER_DAY: f32 = 0.95;
#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy)]
pub(crate) struct Id(u32);
pub const INVALID_ID: Id = Id(0);
impl FromStr for Id {
type Err = ParseIntError;
@@ -69,6 +68,12 @@ impl ToSql for Id {
}
}
impl Id {
pub(crate) fn is_valid(self) -> bool {
self.0 != 0
}
}
#[derive(Clone, Debug)]
pub struct KanaSet {
name: String,
+1 -1
View File
@@ -27,6 +27,7 @@ use crate::lang::{DeckSettings, Id, WordData, WordGroup};
use crate::navigation::{AppSettings, RootMessage, ScreenState};
use crate::quiz::*;
use chrono::NaiveDate;
use hashbrown::HashMap;
use iced::{Font, window};
use iced::{Subscription, Theme, keyboard};
use iced_core::Size;
@@ -34,7 +35,6 @@ use iced_core::window::Position;
use iced_core::window::settings::PlatformSpecific;
use mimalloc::MiMalloc;
use rusqlite::Connection;
use hashbrown::HashMap;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
+5 -5
View File
@@ -7,6 +7,7 @@ use crate::dictionary::{DictionaryMessage, DictionaryState, app_data_dir};
use crate::dictionary_test::{DictionaryQuizMessage, DictionaryQuizState};
use crate::history::{HistoryMessage, HistoryState};
use crate::import::{ImportMessage, ImportState};
use crate::lang::Id;
use crate::message_navigation;
use crate::navigation::Page::*;
use crate::navigation::RootMessage::{DataLoaded, Keyboard, UpdateData};
@@ -24,14 +25,13 @@ use crate::word::{WordMessage, WordState};
use crate::writing::{WritingMessage, WritingState};
use crate::{AppState, fill_state};
use chrono::NaiveDate;
use hashbrown::HashMap;
use iced::keyboard::Event;
use iced::{Element, Task};
use reqwest::Error;
use hashbrown::HashMap;
use rusqlite::Connection;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use rusqlite::Connection;
use crate::lang::Id;
impl Default for ScreenState {
fn default() -> Self {
@@ -127,7 +127,7 @@ impl ScreenState {
for file in directory.read_dir().unwrap().flatten() {
let mut vec = vec![];
let history_file_name = file.file_name().into_string().unwrap();
let id : Id = history_file_name[4..history_file_name.len() - 12]
let id: Id = history_file_name[4..history_file_name.len() - 12]
.parse::<Id>()
.unwrap();
@@ -172,7 +172,7 @@ impl ScreenState {
if let UpdateData = message {
println!("Loading data");
let mut state = self.app_state.lock().unwrap();
if cfg!(windows){
if cfg!(windows) {
state.connection = Connection::open_in_memory().unwrap();
}
let path = app_data_dir();
+2 -2
View File
@@ -1,5 +1,6 @@
use crate::data_provider::voice::get_voice;
use crate::lang::{DeckData, DeckSettings, CardStatistics, WordData, WordOpenMode};
use crate::lang::Id;
use crate::lang::{CardStatistics, DeckData, DeckSettings, WordData, WordOpenMode};
use crate::navigation::Page::PreviousPage;
use crate::navigation::{KeyPressedPage, NavigatedPage, Page};
use crate::styling::*;
@@ -13,7 +14,6 @@ use iced::{Element, Fill, Task, Theme, alignment, keyboard};
use rodio::MixerDeviceSink;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use crate::lang::Id;
use tokio::task::spawn_blocking;
pub struct RepetitionState {
+5 -6
View File
@@ -1,7 +1,7 @@
use crate::data_provider::card_sets::update_deck;
use crate::data_provider::card_stats::{add_stat_list, load_stats_of_deck};
use crate::history::HistoryState;
use crate::lang::{AppendMode, CardStatistics, DeckSettings, INVALID_ID, Id, OrderMode};
use crate::lang::{AppendMode, CardStatistics, DeckSettings, Id, OrderMode};
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
use crate::navigation::{NavigatedPage, Page};
use crate::repetition::RepetitionState;
@@ -115,7 +115,7 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
let index = self.selected_deck_index.unwrap();
self.clear_selection();
let deck = self.decks.remove(index);
if deck.id != INVALID_ID {
if deck.id.is_valid() {
let mut state = self.state.lock().unwrap();
state.decks.remove(index);
self.decks = self
@@ -292,7 +292,7 @@ impl RepetitionsState {
impl RepetitionsState {
fn launch_delete_button(&self, deck: &DeckViewData) -> Element<'_, RepetitionsMessage> {
if deck.id != INVALID_ID {
if deck.id.is_valid() {
if deck.append_mode == AppendMode::Manual
&& deck.existing_words_indices.as_ref().unwrap().is_empty()
{
@@ -325,7 +325,7 @@ impl RepetitionsState {
]
.spacing(QUARTER_SPACING),
{
if deck.id == INVALID_ID {
if !deck.id.is_valid() {
column![
column![
text!("Передняя сторона"),
@@ -548,8 +548,7 @@ impl RepetitionsState {
} else {
None
},
text_color:
_x.palette().primary,
text_color: _x.palette().primary,
border: Border {
color: Default::default(),
width: 0.0,