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) {
let index = connection
let index : u32 = connection
.query_row(
"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)
)
.unwrap_or_else(|e| {println!("{}", e); 0});
.unwrap();
set.id = index.into();
}
@@ -61,7 +61,7 @@ pub fn update_deck(set: &mut DeckSettings, connection: &Connection) {
&set.id
),
)
.unwrap_or_else(|e| {println!("{}", e); 0});
.unwrap();
}
}
@@ -71,8 +71,5 @@ pub fn delete_set(set: &DeckSettings, connection: &Connection) {
}
connection
.execute("DELETE FROM card_set WHERE id = ?1", (&set.id,))
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
}
+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",
(&stat.score, &stat.last_open.timestamp(), &stat.id),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
println!("Updated stat: {}", time.elapsed().as_millis());
}
@@ -81,9 +78,5 @@ pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
return;
}
connection
.execute("DELETE FROM card_stats WHERE id = ?1", (&stat.id,))
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.execute("DELETE FROM card_stats WHERE id = ?1", (&stat.id,)).unwrap();
}
+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());
for string in strings {
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 {
timestamp: DateTime::from_timestamp(time.parse().unwrap(), 0).unwrap(),
word_id: word.parse::<Id>().unwrap(),
mode: match mode.parse::<u8>().unwrap() {
timestamp: DateTime::from_timestamp(time.parse().unwrap_or_default(), 0).unwrap(),
word_id: id,
mode: match mode.parse::<u8>().unwrap_or_default() {
2 => WordOpenMode::Hard,
3 => WordOpenMode::Ok,
4 => WordOpenMode::Easy,
_ => WordOpenMode::None,
},
before: before.parse().unwrap(),
after: after.parse().unwrap(),
before: before.parse().unwrap_or_default(),
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) {
connection
.execute("DELETE FROM settings WHERE id = ?1", (&key,))
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
}
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)",
(key, value),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
}
fn update_settings(key: String, value: String, connection: &Connection) {
@@ -53,10 +47,7 @@ set value = ?2
where id = ?1",
(key, value),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
}
fn get_settings_list(connection: &Connection) -> Vec<String> {
+8 -27
View File
@@ -3,7 +3,7 @@ use rusqlite::{params, Connection};
use std::collections::HashMap;
pub fn add_word(word: &mut WordData, connection: &Connection) {
let index = connection
let index : u32 = connection
.query_row(
"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,
),
|row| row.get(0),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
).unwrap();
word.id = index.into();
}
@@ -72,10 +68,7 @@ pub fn update_word(word: &mut WordData, connection: &Connection) {
&word.id,
),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
}
}
@@ -85,10 +78,7 @@ pub fn delete_word(word: &WordData, connection: &Connection) {
}
connection
.execute("DELETE FROM words WHERE id = ?1", (&word.id,))
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
}
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) {
let index = connection
let index : u32 = connection
.query_row(
"INSERT INTO word_group (name) VALUES (?1) RETURNING id",
(&group.name,),
|row| row.get(0),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
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",
(&group.name, &group.id),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
}
}
@@ -175,8 +159,5 @@ pub fn delete_group(group: &WordGroup, connection: &Connection) {
}
connection
.execute("DELETE FROM word_group WHERE id = ?1", (&group.id,))
.unwrap_or_else(|e| {
println!("{}", e);
0
});
.unwrap();
}
+17 -4
View File
@@ -210,9 +210,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
SaveGroup => {
let state = &mut self.state.lock().unwrap();
let connection = &state.connection;
let mut group = state
.word_groups[self.selected_group_index]
.clone();
let mut group = state.word_groups[self.selected_group_index].clone();
update_group(&mut group, connection);
state.word_groups[self.selected_group_index] = group;
@@ -274,7 +272,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
self.groups_panel(),
row![horizontal().width(8), self.words_list(),],
row![
button("Добавить слово").style(jl_button).on_press(NewWord),
self.add_word_button(),
horizontal().width(Fill),
button("Импорт").style(text).on_press(ToImport),
]
@@ -598,6 +596,21 @@ impl DictionaryState {
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> {