diff --git a/src/data_provider/card_sets.rs b/src/data_provider/card_sets.rs index 86f4c22..00f75fe 100644 --- a/src/data_provider/card_sets.rs +++ b/src/data_provider/card_sets.rs @@ -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 diff --git a/src/data_provider/card_stats.rs b/src/data_provider/card_stats.rs index e913bca..888f030 100644 --- a/src/data_provider/card_stats.rs +++ b/src/data_provider/card_stats.rs @@ -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; @@ -64,7 +64,7 @@ pub fn add_stat_list(stat: &mut [CardStatistics], connection: &Connection) { for (index, id) in (start_index..=last_index).enumerate() { 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 == INVALID_ID { + if !stat.id.is_valid() { return; } connection diff --git a/src/data_provider/import.rs b/src/data_provider/import.rs index 54e360f..0e22cb0 100644 --- a/src/data_provider/import.rs +++ b/src/data_provider/import.rs @@ -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); diff --git a/src/data_provider/words.rs b/src/data_provider/words.rs index 5947a96..0d3e649 100644 --- a/src/data_provider/words.rs +++ b/src/data_provider/words.rs @@ -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 diff --git a/src/dictionary.rs b/src/dictionary.rs index 30f2c3b..73182dc 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -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 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, diff --git a/src/lang.rs b/src/lang.rs index e0c5efa..182650b 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -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, @@ -703,4 +708,4 @@ impl DeckSettings { pub fn require_speech(&self) -> bool { self.forward == "speech" || self.backward == "speech" } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 866ecfa..479f092 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/navigation.rs b/src/navigation.rs index bf2c459..1a6c4d1 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -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::() .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(); diff --git a/src/repetition.rs b/src/repetition.rs index efb38b4..7d24a08 100644 --- a/src/repetition.rs +++ b/src/repetition.rs @@ -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 { diff --git a/src/repetitions.rs b/src/repetitions.rs index 2d5e052..d86126d 100644 --- a/src/repetitions.rs +++ b/src/repetitions.rs @@ -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 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,