Remove silent unwrap in data providers

This commit is contained in:
2026-08-19 23:37:59 +03:00
parent 01f0999772
commit de2dbaf6c1
6 changed files with 47 additions and 64 deletions
+4 -7
View File
@@ -30,7 +30,7 @@ pub fn load_sets(connection: &Connection) -> Vec<DeckSettings> {
} }
pub fn add_set(set: &mut DeckSettings, connection: &Connection) { pub fn add_set(set: &mut DeckSettings, connection: &Connection) {
let index = connection let index : u32 = connection
.query_row( .query_row(
"INSERT INTO card_set (name, forward, backward, filter) VALUES (?1, ?2, ?3, ?4) RETURNING id", "INSERT INTO card_set (name, forward, backward, filter) VALUES (?1, ?2, ?3, ?4) RETURNING id",
( (
@@ -41,7 +41,7 @@ pub fn add_set(set: &mut DeckSettings, connection: &Connection) {
), ),
|row| row.get(0) |row| row.get(0)
) )
.unwrap_or_else(|e| {println!("{}", e); 0}); .unwrap();
set.id = index.into(); set.id = index.into();
} }
@@ -61,7 +61,7 @@ pub fn update_deck(set: &mut DeckSettings, connection: &Connection) {
&set.id &set.id
), ),
) )
.unwrap_or_else(|e| {println!("{}", e); 0}); .unwrap();
} }
} }
@@ -71,8 +71,5 @@ pub fn delete_set(set: &DeckSettings, connection: &Connection) {
} }
connection connection
.execute("DELETE FROM card_set WHERE id = ?1", (&set.id,)) .execute("DELETE FROM card_set WHERE id = ?1", (&set.id,))
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
} }
+2 -9
View File
@@ -69,10 +69,7 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3", "UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
(&stat.score, &stat.last_open.timestamp(), &stat.id), (&stat.score, &stat.last_open.timestamp(), &stat.id),
) )
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
println!("Updated stat: {}", time.elapsed().as_millis()); println!("Updated stat: {}", time.elapsed().as_millis());
} }
@@ -81,9 +78,5 @@ pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
return; return;
} }
connection connection
.execute("DELETE FROM card_stats WHERE id = ?1", (&stat.id,)) .execute("DELETE FROM card_stats WHERE id = ?1", (&stat.id,)).unwrap();
.unwrap_or_else(|e| {
println!("{}", e);
0
});
} }
+13 -5
View File
@@ -30,17 +30,25 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
let mut items = Vec::with_capacity(strings.len()); let mut items = Vec::with_capacity(strings.len());
for string in strings { for string in strings {
if let [time, word, mode, before, after] = string.split(';').collect::<Vec<&str>>()[..] { if let [time, word, mode, before, after] = string.split(';').collect::<Vec<&str>>()[..] {
let id = word.parse::<Id>();
if id.is_err(){
continue;
}
let id = id.unwrap();
if !id.is_valid() {
continue;
}
items.push(HistoryItem { items.push(HistoryItem {
timestamp: DateTime::from_timestamp(time.parse().unwrap(), 0).unwrap(), timestamp: DateTime::from_timestamp(time.parse().unwrap_or_default(), 0).unwrap(),
word_id: word.parse::<Id>().unwrap(), word_id: id,
mode: match mode.parse::<u8>().unwrap() { mode: match mode.parse::<u8>().unwrap_or_default() {
2 => WordOpenMode::Hard, 2 => WordOpenMode::Hard,
3 => WordOpenMode::Ok, 3 => WordOpenMode::Ok,
4 => WordOpenMode::Easy, 4 => WordOpenMode::Easy,
_ => WordOpenMode::None, _ => WordOpenMode::None,
}, },
before: before.parse().unwrap(), before: before.parse().unwrap_or_default(),
after: after.parse().unwrap(), after: after.parse().unwrap_or_default(),
}); });
} }
} }
+3 -12
View File
@@ -27,10 +27,7 @@ pub fn set_setting(key: String, value: String, connection: &Connection) {
pub fn delete_settings(key: String, connection: &Connection) { pub fn delete_settings(key: String, connection: &Connection) {
connection connection
.execute("DELETE FROM settings WHERE id = ?1", (&key,)) .execute("DELETE FROM settings WHERE id = ?1", (&key,))
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
} }
fn create_settings(key: String, value: String, connection: &Connection) { fn create_settings(key: String, value: String, connection: &Connection) {
@@ -39,10 +36,7 @@ fn create_settings(key: String, value: String, connection: &Connection) {
"INSERT into settings (id, value) VALUES (?1, ?2)", "INSERT into settings (id, value) VALUES (?1, ?2)",
(key, value), (key, value),
) )
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
} }
fn update_settings(key: String, value: String, connection: &Connection) { fn update_settings(key: String, value: String, connection: &Connection) {
@@ -53,10 +47,7 @@ set value = ?2
where id = ?1", where id = ?1",
(key, value), (key, value),
) )
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
} }
fn get_settings_list(connection: &Connection) -> Vec<String> { fn get_settings_list(connection: &Connection) -> Vec<String> {
+8 -27
View File
@@ -3,7 +3,7 @@ use rusqlite::{params, Connection};
use std::collections::HashMap; use std::collections::HashMap;
pub fn add_word(word: &mut WordData, connection: &Connection) { pub fn add_word(word: &mut WordData, connection: &Connection) {
let index = connection let index : u32 = connection
.query_row( .query_row(
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5) RETURNING id", "INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5) RETURNING id",
( (
@@ -14,11 +14,7 @@ pub fn add_word(word: &mut WordData, connection: &Connection) {
&word.group_id, &word.group_id,
), ),
|row| row.get(0), |row| row.get(0),
) ).unwrap();
.unwrap_or_else(|e| {
println!("{}", e);
0
});
word.id = index.into(); word.id = index.into();
} }
@@ -72,10 +68,7 @@ pub fn update_word(word: &mut WordData, connection: &Connection) {
&word.id, &word.id,
), ),
) )
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
} }
} }
@@ -85,10 +78,7 @@ pub fn delete_word(word: &WordData, connection: &Connection) {
} }
connection connection
.execute("DELETE FROM words WHERE id = ?1", (&word.id,)) .execute("DELETE FROM words WHERE id = ?1", (&word.id,))
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
} }
pub fn load_words(connection: &Connection) -> Vec<WordData> { pub fn load_words(connection: &Connection) -> Vec<WordData> {
@@ -139,16 +129,13 @@ pub fn load_word_groups(connection: &Connection) -> Vec<WordGroup> {
} }
pub fn add_group(group: &mut WordGroup, connection: &Connection) { pub fn add_group(group: &mut WordGroup, connection: &Connection) {
let index = connection let index : u32 = connection
.query_row( .query_row(
"INSERT INTO word_group (name) VALUES (?1) RETURNING id", "INSERT INTO word_group (name) VALUES (?1) RETURNING id",
(&group.name,), (&group.name,),
|row| row.get(0), |row| row.get(0),
) )
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
group.id = index.into(); group.id = index.into();
} }
@@ -162,10 +149,7 @@ pub fn update_group(group: &mut WordGroup, connection: &Connection) {
"UPDATE word_group SET name = ?1 WHERE id = ?2", "UPDATE word_group SET name = ?1 WHERE id = ?2",
(&group.name, &group.id), (&group.name, &group.id),
) )
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
} }
} }
@@ -175,8 +159,5 @@ pub fn delete_group(group: &WordGroup, connection: &Connection) {
} }
connection connection
.execute("DELETE FROM word_group WHERE id = ?1", (&group.id,)) .execute("DELETE FROM word_group WHERE id = ?1", (&group.id,))
.unwrap_or_else(|e| { .unwrap();
println!("{}", e);
0
});
} }
+17 -4
View File
@@ -210,9 +210,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
SaveGroup => { SaveGroup => {
let state = &mut self.state.lock().unwrap(); let state = &mut self.state.lock().unwrap();
let connection = &state.connection; let connection = &state.connection;
let mut group = state let mut group = state.word_groups[self.selected_group_index].clone();
.word_groups[self.selected_group_index]
.clone();
update_group(&mut group, connection); update_group(&mut group, connection);
state.word_groups[self.selected_group_index] = group; state.word_groups[self.selected_group_index] = group;
@@ -274,7 +272,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
self.groups_panel(), self.groups_panel(),
row![horizontal().width(8), self.words_list(),], row![horizontal().width(8), self.words_list(),],
row![ row![
button("Добавить слово").style(jl_button).on_press(NewWord), self.add_word_button(),
horizontal().width(Fill), horizontal().width(Fill),
button("Импорт").style(text).on_press(ToImport), button("Импорт").style(text).on_press(ToImport),
] ]
@@ -598,6 +596,21 @@ impl DictionaryState {
space().into() space().into()
} }
} }
fn add_word_button(&self) -> iced::Element<'_, DictionaryMessage> {
let state = self.state.lock().unwrap();
let group_id = state.word_groups[self.selected_group_index].id;
let button = button("Добавить слово")
.style(jl_button);
if group_id.is_valid() {
return
button.on_press(NewWord)
.into();
}
button.into()
}
} }
pub fn split_with_coma(ts: &str) -> Vec<String> { pub fn split_with_coma(ts: &str) -> Vec<String> {