Formatting

This commit is contained in:
micialware committed 2026-08-25 12:04:07 +03:00
1 parent dddac01bdf
commit 10cf7f0d2e
29 files changed
+138 -138

No files matched your search

+2 -2
View File
@@ -1,9 +1,9 @@
use crate::core::srs::OrderMode;
use crate::data::entities::DeckSettings;
use rusqlite::Connection;
use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use crate::notification::{MaybeError, NotificationData};
use rusqlite::Connection;
pub fn load_sets(connection: &Connection) -> Result<Vec<DeckSettings>, NotificationData> {
let mut stmt = connection
+4 -2
View File
@@ -1,6 +1,6 @@
use crate::data::entities::{CardStatistics, DeckSettings};
use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use crate::notification::{MaybeError, NotificationData};
use log::info;
use rusqlite::{Connection, params};
use std::time::Instant;
@@ -32,7 +32,9 @@ pub fn load_stats_of_deck(
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> MaybeError {
let time = Instant::now();
let tx = connection.transaction().expect("Failed to create transaction");
let tx = connection
.transaction()
.expect("Failed to create transaction");
let count = stats.len();
{
+1 -2
View File
@@ -2,8 +2,8 @@ use crate::core::srs::WordOpenMode;
use crate::data::Id;
use crate::data::entities::HistoryItem;
use crate::fs::*;
use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::PermissionDenied;
use crate::notification::{MaybeError, NotificationData};
use chrono::DateTime;
use std::fs::{File, OpenOptions};
use std::io::Write;
@@ -29,7 +29,6 @@ fn append_lines_if_exists(path: PathBuf, lines: &mut Vec<String>) -> MaybeError
.for_each(|i| lines.push(i));
}
Ok(())
}
fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
+10 -9
View File
@@ -1,10 +1,10 @@
use crate::helpers::into_result;
use crate::notification::NotificationData;
use crate::notification::NotificationData::IncorrectAnkiFile;
use hashbrown::HashMap;
use log::info;
use rusqlite::Connection;
use serde_json::Value;
use crate::helpers::into_result;
use crate::notification::NotificationData::IncorrectAnkiFile;
#[derive(Clone)]
pub struct ImportData(pub(crate) Vec<ImportGroup>);
@@ -29,8 +29,7 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
let json_models: String = connection
.query_one("select models from col", (), |row| row.get(0))
.map_err(|_| IncorrectAnkiFile)?;
let raw: Value = serde_json::from_str(json_models.as_str())
.map_err(|_| IncorrectAnkiFile)?;
let raw: Value = serde_json::from_str(json_models.as_str()).map_err(|_| IncorrectAnkiFile)?;
let sets = raw.as_object();
if let Some(sets) = sets {
let mut total_data = ImportData(vec![]);
@@ -48,14 +47,13 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
for (_, collection) in sets {
let fields_property = collection.get("fields");
if let Some(fields_property) = fields_property {
let fields = into_result(fields_property
.as_array(), IncorrectAnkiFile)?
let fields = into_result(fields_property.as_array(), IncorrectAnkiFile)?
.iter()
.filter_map(|x| x.get("name"))
.map(|x| x.as_str().unwrap().to_string())
.collect();
let id = collection.get("id");
if id.is_none() || id.unwrap().as_u64().is_none(){
if id.is_none() || id.unwrap().as_u64().is_none() {
return Err(IncorrectAnkiFile);
}
let id = id.unwrap().as_u64().unwrap();
@@ -80,12 +78,15 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
total_data.0.push(group);
}
}
return Ok(total_data)
return Ok(total_data);
}
Err(IncorrectAnkiFile)
}
pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Result<Vec<ImportNote>, NotificationData> {
pub fn get_words_of_group(
connection: &Connection,
group_id: u64,
) -> Result<Vec<ImportNote>, NotificationData> {
let mut count_stmt = connection
.prepare("select tags, flds from notes where mid == ?1;")
.map_err(|_| IncorrectAnkiFile)?;
+3 -15
View File
@@ -17,11 +17,7 @@ pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
None
}
pub fn set_setting(
key: String,
value: String,
connection: &Connection,
) -> MaybeError {
pub fn set_setting(key: String, value: String, connection: &Connection) -> MaybeError {
let current = get_settings_list(connection)?;
if current.contains(&key) {
update_settings(key, value, connection)?;
@@ -39,11 +35,7 @@ pub fn delete_settings(key: String, connection: &Connection) -> MaybeError {
Ok(())
}
fn create_settings(
key: String,
value: String,
connection: &Connection,
) -> MaybeError {
fn create_settings(key: String, value: String, connection: &Connection) -> MaybeError {
connection
.execute(
"INSERT into settings (id, value) VALUES (?1, ?2)",
@@ -53,11 +45,7 @@ fn create_settings(
Ok(())
}
fn update_settings(
key: String,
value: String,
connection: &Connection,
) -> MaybeError {
fn update_settings(key: String, value: String, connection: &Connection) -> MaybeError {
connection
.execute(
"update settings
+7 -3
View File
@@ -1,12 +1,14 @@
use rusqlite::Connection;
use crate::fs::*;
use rusqlite::Connection;
pub fn create_db() {
let db_file = db_path();
if !db_file.exists() {
std::fs::File::create(&db_file).expect("Database file creation failed");
let connection = Connection::open(&db_file).expect("Connection open failed");
connection.execute("PRAGMA foreign_keys = ON;", []).expect("Failed to set PRAGMA");
connection
.execute("PRAGMA foreign_keys = ON;", [])
.expect("Failed to set PRAGMA");
create_tables(&connection);
}
@@ -52,6 +54,8 @@ fn make_card_stats(conn: &Connection) -> Result<(), rusqlite::Error> {
pub fn default_connection() -> Connection {
let db_file = db_path();
let connection = Connection::open(db_file).expect("Failed to open database");
connection.execute("PRAGMA foreign_keys = ON;", []).expect("Failed to set PRAGMA");
connection
.execute("PRAGMA foreign_keys = ON;", [])
.expect("Failed to set PRAGMA");
connection
}
+14 -6
View File
@@ -1,10 +1,10 @@
use crate::fs::*;
use crate::notification::NotificationData;
use crate::notification::NotificationData::*;
use reqwest::Client;
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::BufReader;
use crate::fs::*;
use crate::notification::NotificationData;
use crate::notification::NotificationData::*;
pub async fn get_voice(text: &str) -> Result<BufReader<File>, NotificationData> {
let mut path = voice_dir()?;
@@ -19,7 +19,11 @@ pub async fn get_voice(text: &str) -> Result<BufReader<File>, NotificationData>
.send()
.await;
let query = query.map_err(|_| VoiceVoxUnavailable)?.text().await.map_err(|_| VoiceVoxUnavailable)?;
let query = query
.map_err(|_| VoiceVoxUnavailable)?
.text()
.await
.map_err(|_| VoiceVoxUnavailable)?;
// Synthesis
let audio = client
.post(format!("{engine_url}/synthesis?speaker=11"))
@@ -32,8 +36,12 @@ pub async fn get_voice(text: &str) -> Result<BufReader<File>, NotificationData>
.await
.map_err(|_| VoiceVoxUnavailable)?;
tokio::fs::write(&path, &audio).await.map_err(|_| PermissionDenied)?;
tokio::fs::write(&path, &audio)
.await
.map_err(|_| PermissionDenied)?;
}
Ok(BufReader::new(File::open(path).map_err(|_| PermissionDenied)?))
Ok(BufReader::new(
File::open(path).map_err(|_| PermissionDenied)?,
))
}
+1 -1
View File
@@ -5,7 +5,7 @@ use log::info;
use std::io;
use tokio::fs::{File, OpenOptions};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL};
use zstd::{DEFAULT_COMPRESSION_LEVEL, Decoder, Encoder};
// const API_URL: &str = "http://127.0.0.1:8089/";
const API_URL: &str = "https://learning.micialware.ru/";
+12 -13
View File
@@ -1,6 +1,6 @@
use crate::data::entities::{WordData, WordGroup};
use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use crate::notification::{MaybeError, NotificationData};
use rusqlite::{Connection, params};
use std::collections::HashMap;
@@ -22,10 +22,7 @@ pub fn add_word(word: &mut WordData, connection: &Connection) -> MaybeError {
Ok(())
}
pub fn add_words(
words: &mut [WordData],
connection: &mut Connection,
) -> MaybeError {
pub fn add_words(words: &mut [WordData], connection: &mut Connection) -> MaybeError {
let tx = connection.transaction().map_err(|_| DbLoadError)?;
let count = words.len();
@@ -44,7 +41,7 @@ pub fn add_words(
serde_json::to_string(&word.additional).unwrap_or_default(),
word.group_id
])
.map_err(|_| DbSaveError)?;
.map_err(|_| DbSaveError)?;
}
}
@@ -102,14 +99,17 @@ pub fn load_words(connection: &Connection) -> Result<Vec<WordData>, Notification
key: row.get(1)?,
value: row.get(2)?,
tags: row.get(3)?,
additional: serde_json::from_str::<HashMap<String, String>>(&additional).unwrap_or_default(),
additional: serde_json::from_str::<HashMap<String, String>>(&additional)
.unwrap_or_default(),
group_id: row.get(5)?,
})
})
.map_err(|_| DbLoadError)?;
let mut buffer = Vec::with_capacity(1024);
word_iter.filter_map(|res| res.ok()).for_each(|i| buffer.push(i));
word_iter
.filter_map(|res| res.ok())
.for_each(|i| buffer.push(i));
Ok(buffer)
}
@@ -128,7 +128,9 @@ pub fn load_word_groups(connection: &Connection) -> Result<Vec<WordGroup>, Notif
.map_err(|_| DbLoadError)?;
let mut buffer = Vec::with_capacity(8);
group_iter.filter_map(|res| res.ok()).for_each(|i| buffer.push(i));
group_iter
.filter_map(|res| res.ok())
.for_each(|i| buffer.push(i));
Ok(buffer)
}
@@ -146,10 +148,7 @@ pub fn add_group(group: &mut WordGroup, connection: &Connection) -> MaybeError {
Ok(())
}
pub fn update_group(
group: &mut WordGroup,
connection: &Connection,
) -> MaybeError {
pub fn update_group(group: &mut WordGroup, connection: &Connection) -> MaybeError {
if !group.id.is_valid() {
Ok(add_group(group, connection)?)
} else {