code cleanup

This commit is contained in:
2026-08-01 19:43:07 +03:00
parent 42d0488f23
commit 20cb50574c
21 changed files with 421 additions and 373 deletions
+20 -18
View File
@@ -1,21 +1,25 @@
use crate::lang::SetOrderMode;
use crate::repetitions::CardSetSettings;
use rusqlite::Connection;
use crate::lang::SetOrderMode;
pub fn load_sets(connection: &Connection) -> Vec<CardSetSettings> {
let mut stmt = connection.prepare("SELECT id, name, forward, backward, filter FROM card_set").unwrap();
let iter = stmt.query_map([], |row| {
Ok(CardSetSettings {
id: row.get(0)?,
name: row.get(1)?,
forward: row.get(2)?,
backward: row.get(3)?,
filter: row.get(4)?,
count: None,
worst_words_list: None,
open_mode: SetOrderMode::Default,
let mut stmt = connection
.prepare("SELECT id, name, forward, backward, filter FROM card_set")
.unwrap();
let iter = stmt
.query_map([], |row| {
Ok(CardSetSettings {
id: row.get(0)?,
name: row.get(1)?,
forward: row.get(2)?,
backward: row.get(3)?,
filter: row.get(4)?,
count: None,
worst_words_list: None,
open_mode: SetOrderMode::Default,
})
})
}).unwrap();
.unwrap();
let mut buffer = vec![];
for word in iter {
@@ -42,12 +46,10 @@ pub fn add_set(set: &mut CardSetSettings, connection: &Connection) {
set.id = index;
}
pub fn update_card_set(set: &mut CardSetSettings, connection: &Connection){
pub fn update_card_set(set: &mut CardSetSettings, connection: &Connection) {
if set.id == 0 {
add_set(set, &connection);
}
else {
} else {
connection
.execute(
"UPDATE card_set SET name = ?1, forward = ?2, backward = ?3, filter = ?4 WHERE id = ?5",
@@ -73,4 +75,4 @@ pub fn delete_set(set: &CardSetSettings, connection: &Connection) {
println!("{}", e);
0
});
}
}
+48 -33
View File
@@ -4,16 +4,20 @@ use rusqlite::Connection;
use std::time::Instant;
pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<CardStatistics> {
let mut stmt = connection.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1").unwrap();
let iter = stmt.query_map((set.id,), |row| {
Ok(CardStatistics {
id: row.get(0)?,
word_id: row.get(1)?,
set_id: set.id,
score: row.get(2)?,
last_open: row.get(3)?,
let mut stmt = connection
.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1")
.unwrap();
let iter = stmt
.query_map((set.id,), |row| {
Ok(CardStatistics {
id: row.get(0)?,
word_id: row.get(1)?,
set_id: set.id,
score: row.get(2)?,
last_open: row.get(3)?,
})
})
}).unwrap();
.unwrap();
let mut buffer = vec![];
for word in iter {
@@ -23,23 +27,38 @@ pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<
buffer
}
pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
let inserting = stat.iter().map(|stat| format!("({}, {}, {}, {})", stat.word_id.to_string(), stat.set_id.to_string(), stat.score.to_string(), stat.last_open.timestamp().to_string())).collect::<Vec<_>>().join(", ");
let query = format!("INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES {}", inserting);
let count = connection
.execute(
query.as_str(),
(
),
);
let inserting = stat
.iter()
.map(|stat| {
format!(
"({}, {}, {}, {})",
stat.word_id.to_string(),
stat.set_id.to_string(),
stat.score.to_string(),
stat.last_open.timestamp().to_string()
)
})
.collect::<Vec<_>>()
.join(", ");
let query = format!(
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES {}",
inserting
);
let count = connection.execute(query.as_str(), ());
if count.is_err() {
println!("{}", count.unwrap_err());
return;
}
let last_index : u32 = connection.query_one("SELECT seq from sqlite_sequence WHERE name == ?1", ("card_stats".to_string(),), |row| row.get(0)).unwrap();
let last_index: u32 = connection
.query_one(
"SELECT seq from sqlite_sequence WHERE name == ?1",
("card_stats".to_string(),),
|row| row.get(0),
)
.unwrap();
let start_index = last_index - (count.unwrap() as u32) + 1;
@@ -48,10 +67,8 @@ pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
stat[index].id = id as u32;
index += 1;
}
}
pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
let time = Instant::now();
let index = connection
@@ -71,21 +88,19 @@ pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
println!("Added stat: {}", time.elapsed().as_millis());
}
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection){
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
let time = Instant::now();
connection
.execute(
"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});
connection
.execute(
"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
});
println!("Updated stat: {}", time.elapsed().as_millis());
}
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
+19 -11
View File
@@ -33,7 +33,6 @@ 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>>()[..] {
items.push(HistoryItem {
timestamp: DateTime::from_timestamp(time.parse().unwrap(), 0).unwrap(),
word_id: word.parse::<u32>().unwrap(),
@@ -55,16 +54,25 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
pub fn push_note(set_id: u32, item: HistoryItem) {
let app_dir = app_data_dir();
let path = app_dir.clone().join(format!("set_{}_history.csv", set_id));
let mut file = OpenOptions::new().create(true).append(true).open(path).unwrap();
let line_str = format!("{};{};{};{};{}", item.timestamp.timestamp(), item.word_id, match item.mode {
WordOpenMode::Easy => 4,
WordOpenMode::Ok => 3,
WordOpenMode::Hard => 2,
WordOpenMode::None => 1
},
item.before,
item.after);
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(path)
.unwrap();
let line_str = format!(
"{};{};{};{};{}",
item.timestamp.timestamp(),
item.word_id,
match item.mode {
WordOpenMode::Easy => 4,
WordOpenMode::Ok => 3,
WordOpenMode::Hard => 2,
WordOpenMode::None => 1,
},
item.before,
item.after
);
writeln!(&mut file, "{}", line_str.to_string()).unwrap();
}
+3 -4
View File
@@ -1,8 +1,7 @@
pub(crate) mod words;
pub(crate) mod card_sets;
pub(crate) mod card_stats;
pub(crate) mod voice;
pub(crate) mod history;
pub(crate) mod settings;
pub(crate) mod sqlite;
pub(crate) mod history;
pub(crate) mod voice;
pub(crate) mod words;
+9 -11
View File
@@ -1,10 +1,10 @@
use rusqlite::Connection;
pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
let mut stmt = connection.prepare("SELECT value FROM settings WHERE id = ?1").unwrap();
let iter = stmt.query_map((key,), |row| {
row.get(0)
}).unwrap();
let mut stmt = connection
.prepare("SELECT value FROM settings WHERE id = ?1")
.unwrap();
let iter = stmt.query_map((key,), |row| row.get(0)).unwrap();
for row in iter {
if let Ok(value) = row {
@@ -19,7 +19,7 @@ pub fn set_setting(key: String, value: String, connection: &Connection) {
let current = get_settings_list(connection);
if current.contains(&key) {
update_settings(key, value, connection);
}else {
} else {
create_settings(key, value, connection);
}
}
@@ -56,13 +56,11 @@ where id = ?1",
.unwrap_or_else(|e| {
println!("{}", e);
0
});}
});
}
fn get_settings_list(connection: &Connection) -> Vec<String> {
let mut stmt = connection.prepare("SELECT id FROM settings").unwrap();
let iter = stmt.query_map((), |row| {
row.get(0)
}).unwrap();
iter.map(|row| { row.unwrap() }).collect()
let iter = stmt.query_map((), |row| row.get(0)).unwrap();
iter.map(|row| row.unwrap()).collect()
}
+2 -4
View File
@@ -10,16 +10,14 @@ pub fn create_db() {
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
create_tables(&connection);
}else {
} else {
let connection = Connection::open(&db_file).unwrap();
ensure_db_schema(&connection);
}
}
fn ensure_db_schema(conn: &Connection) {
}
fn ensure_db_schema(conn: &Connection) {}
fn create_tables(conn: &Connection) {
make_card_set(conn).unwrap();
+4 -2
View File
@@ -27,9 +27,11 @@ pub async fn get_voice(text: &str) -> BufReader<File> {
.header("Content-Type", "application/json")
.body(query)
.send()
.await.unwrap()
.await
.unwrap()
.bytes()
.await.unwrap();
.await
.unwrap();
tokio::fs::write(&path, &audio).await.unwrap();
}
-1
View File
@@ -2,7 +2,6 @@ use crate::lang::{WordData, WordGroup};
use rusqlite::Connection;
use std::collections::HashMap;
pub fn add_word(word: &mut WordData, connection: &Connection) {
let index = connection
.query_row(