Introduce MaybeError type

This commit is contained in:
micialware committed 2026-08-24 23:42:58 +03:00
1 parent d836b57d2a
commit a60ed37cd4
16 files changed
+127 -93

No files matched your search

+2 -2
View File
@@ -6,7 +6,7 @@ use crate::core::srs::{
use crate::data::entities::*; use crate::data::entities::*;
use crate::data_provider::card_stats::{load_stats_of_deck, update_stat_score}; use crate::data_provider::card_stats::{load_stats_of_deck, update_stat_score};
use crate::data_provider::history::push_note; use crate::data_provider::history::push_note;
use crate::notification::NotificationData; use crate::notification::{MaybeError, NotificationData};
use chrono::Utc; use chrono::Utc;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::sync::Arc; use std::sync::Arc;
@@ -88,7 +88,7 @@ impl DeckData {
self.current_word_index = Some(index); self.current_word_index = Some(index);
(self.words[index].clone(), self.set[index].clone()) (self.words[index].clone(), self.set[index].clone())
} }
pub fn open(&mut self, status: WordOpenMode) -> Result<(), NotificationData> { pub fn open(&mut self, status: WordOpenMode) -> MaybeError {
if let Some(index) = self.current_word_index { if let Some(index) = self.current_word_index {
let word: &mut CardStatistics = &mut self.set[index]; let word: &mut CardStatistics = &mut self.set[index];
let old_score = word.score; let old_score = word.score;
+4 -4
View File
@@ -2,7 +2,7 @@ use crate::core::srs::OrderMode;
use crate::data::entities::DeckSettings; use crate::data::entities::DeckSettings;
use rusqlite::Connection; use rusqlite::Connection;
use crate::notification::NotificationData; use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use crate::notification::NotificationData::{DbLoadError, DbSaveError};
pub fn load_sets(connection: &Connection) -> Result<Vec<DeckSettings>, NotificationData> { pub fn load_sets(connection: &Connection) -> Result<Vec<DeckSettings>, NotificationData> {
@@ -28,7 +28,7 @@ pub fn load_sets(connection: &Connection) -> Result<Vec<DeckSettings>, Notificat
Ok(buffer) Ok(buffer)
} }
pub fn add_set(set: &mut DeckSettings, connection: &Connection) -> Result<(), NotificationData> { pub fn add_set(set: &mut DeckSettings, connection: &Connection) -> MaybeError {
let index : u32 = 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",
@@ -46,7 +46,7 @@ pub fn add_set(set: &mut DeckSettings, connection: &Connection) -> Result<(), No
Ok(()) Ok(())
} }
pub fn update_deck(set: &mut DeckSettings, connection: &Connection) -> Result<(), NotificationData> { pub fn update_deck(set: &mut DeckSettings, connection: &Connection) -> MaybeError {
if !set.id.is_valid() { if !set.id.is_valid() {
add_set(set, connection)?; add_set(set, connection)?;
} else { } else {
@@ -66,7 +66,7 @@ pub fn update_deck(set: &mut DeckSettings, connection: &Connection) -> Result<()
Ok(()) Ok(())
} }
pub fn delete_set(set: &DeckSettings, connection: &Connection) -> Result<(), NotificationData> { pub fn delete_set(set: &DeckSettings, connection: &Connection) -> MaybeError {
if !set.id.is_valid() { if !set.id.is_valid() {
return Err(DbSaveError); return Err(DbSaveError);
} }
+4 -4
View File
@@ -1,5 +1,5 @@
use crate::data::entities::{CardStatistics, DeckSettings}; use crate::data::entities::{CardStatistics, DeckSettings};
use crate::notification::NotificationData; use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use log::info; use log::info;
use rusqlite::{Connection, params}; use rusqlite::{Connection, params};
@@ -30,7 +30,7 @@ pub fn load_stats_of_deck(
Ok(buffer) Ok(buffer)
} }
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> Result<(), NotificationData> { pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> MaybeError {
let time = Instant::now(); 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(); let count = stats.len();
@@ -66,7 +66,7 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection)
Ok(()) Ok(())
} }
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> Result<(), NotificationData> { pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> MaybeError {
let time = Instant::now(); let time = Instant::now();
connection connection
@@ -79,7 +79,7 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> Resu
Ok(()) Ok(())
} }
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> Result<(), NotificationData> { pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> MaybeError {
if stat.is_empty() { if stat.is_empty() {
return Ok(()); return Ok(());
} }
+3 -3
View File
@@ -2,7 +2,7 @@ use crate::core::srs::WordOpenMode;
use crate::data::Id; use crate::data::Id;
use crate::data::entities::HistoryItem; use crate::data::entities::HistoryItem;
use crate::fs::*; use crate::fs::*;
use crate::notification::NotificationData; use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::PermissionDenied; use crate::notification::NotificationData::PermissionDenied;
use chrono::DateTime; use chrono::DateTime;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
@@ -20,7 +20,7 @@ pub fn get_history_of_set(id: Id) -> Result<Vec<HistoryItem>, NotificationData>
Ok(parse_history_items(lines)) Ok(parse_history_items(lines))
} }
fn append_lines_if_exists(path: PathBuf, lines: &mut Vec<String>) -> Result<(), NotificationData> { fn append_lines_if_exists(path: PathBuf, lines: &mut Vec<String>) -> MaybeError {
if path.exists() { if path.exists() {
let reader = BufReader::new(File::open(path).map_err(|_| PermissionDenied)?); let reader = BufReader::new(File::open(path).map_err(|_| PermissionDenied)?);
reader reader
@@ -63,7 +63,7 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
items items
} }
pub fn push_note(set_id: Id, item: HistoryItem) -> Result<(), NotificationData> { pub fn push_note(set_id: Id, item: HistoryItem) -> MaybeError {
let app_dir = history_dir()?; let app_dir = history_dir()?;
let path = app_dir.clone().join(format!("set_{}_history.csv", set_id)); let path = app_dir.clone().join(format!("set_{}_history.csv", set_id));
+13 -12
View File
@@ -3,6 +3,8 @@ use hashbrown::HashMap;
use log::info; use log::info;
use rusqlite::Connection; use rusqlite::Connection;
use serde_json::Value; use serde_json::Value;
use crate::helpers::into_result;
use crate::notification::NotificationData::IncorrectAnkiFile;
#[derive(Clone)] #[derive(Clone)]
pub struct ImportData(pub(crate) Vec<ImportGroup>); pub struct ImportData(pub(crate) Vec<ImportGroup>);
@@ -26,41 +28,40 @@ pub struct ImportNote {
pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationData> { pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationData> {
let json_models: String = connection let json_models: String = connection
.query_one("select models from col", (), |row| row.get(0)) .query_one("select models from col", (), |row| row.get(0))
.map_err(|_| NotificationData::IncorrectAnkiFile)?; .map_err(|_| IncorrectAnkiFile)?;
let raw: Value = serde_json::from_str(json_models.as_str()) let raw: Value = serde_json::from_str(json_models.as_str())
.map_err(|_| NotificationData::IncorrectAnkiFile)?; .map_err(|_| IncorrectAnkiFile)?;
let sets = raw.as_object(); let sets = raw.as_object();
if let Some(sets) = sets { if let Some(sets) = sets {
let mut total_data = ImportData(vec![]); let mut total_data = ImportData(vec![]);
let mut count_stmt = connection let mut count_stmt = connection
.prepare("select mid, count(id) from notes group by mid") .prepare("select mid, count(id) from notes group by mid")
.map_err(|_| NotificationData::IncorrectAnkiFile)?; .map_err(|_| IncorrectAnkiFile)?;
let counts = count_stmt let counts = count_stmt
.query_map([], |row| Ok((row.get(0)?, row.get(1)?))) .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
.map_err(|_| NotificationData::IncorrectAnkiFile)? .map_err(|_| IncorrectAnkiFile)?
.filter_map(|x| x.ok()) .filter_map(|x| x.ok())
.collect::<Vec<(i64, i64)>>(); .collect::<Vec<(i64, i64)>>();
for (_, collection) in sets { for (_, collection) in sets {
let fields_property = collection.get("fields"); let fields_property = collection.get("fields");
if let Some(fields_property) = fields_property { if let Some(fields_property) = fields_property {
let fields = fields_property let fields = into_result(fields_property
.as_array() .as_array(), IncorrectAnkiFile)?
.unwrap()
.iter() .iter()
.filter_map(|x| x.get("name")) .filter_map(|x| x.get("name"))
.map(|x| x.as_str().unwrap().to_string()) .map(|x| x.as_str().unwrap().to_string())
.collect(); .collect();
let id = collection.get("id"); 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(NotificationData::IncorrectAnkiFile); return Err(IncorrectAnkiFile);
} }
let id = id.unwrap().as_u64().unwrap(); let id = id.unwrap().as_u64().unwrap();
let name = collection.get("name"); let name = collection.get("name");
if name.is_none() || name.unwrap().as_str().is_none() { if name.is_none() || name.unwrap().as_str().is_none() {
return Err(NotificationData::IncorrectAnkiFile); return Err(IncorrectAnkiFile);
} }
let name = name.unwrap().as_str().unwrap().to_string(); let name = name.unwrap().as_str().unwrap().to_string();
@@ -81,13 +82,13 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
} }
return Ok(total_data) return Ok(total_data)
} }
Err(NotificationData::IncorrectAnkiFile) 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 let mut count_stmt = connection
.prepare("select tags, flds from notes where mid == ?1;") .prepare("select tags, flds from notes where mid == ?1;")
.map_err(|_| NotificationData::IncorrectAnkiFile)?; .map_err(|_| IncorrectAnkiFile)?;
Ok(count_stmt Ok(count_stmt
.query_map((group_id as i64,), |row| { .query_map((group_id as i64,), |row| {
@@ -100,7 +101,7 @@ pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Result<Vec<
.collect(), .collect(),
}) })
}) })
.map_err(|_| NotificationData::IncorrectAnkiFile)? .map_err(|_| IncorrectAnkiFile)?
.filter_map(|x| x.ok()) .filter_map(|x| x.ok())
.collect::<Vec<ImportNote>>()) .collect::<Vec<ImportNote>>())
} }
+5 -5
View File
@@ -1,4 +1,4 @@
use crate::notification::NotificationData; use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use iced::futures::StreamExt; use iced::futures::StreamExt;
use rusqlite::Connection; use rusqlite::Connection;
@@ -22,7 +22,7 @@ pub fn set_setting(
key: String, key: String,
value: String, value: String,
connection: &Connection, connection: &Connection,
) -> Result<(), NotificationData> { ) -> MaybeError {
let current = get_settings_list(connection)?; let current = get_settings_list(connection)?;
if current.contains(&key) { if current.contains(&key) {
update_settings(key, value, connection)?; update_settings(key, value, connection)?;
@@ -33,7 +33,7 @@ pub fn set_setting(
Ok(()) Ok(())
} }
pub fn delete_settings(key: String, connection: &Connection) -> Result<(), NotificationData> { pub fn delete_settings(key: String, connection: &Connection) -> MaybeError {
connection connection
.execute("DELETE FROM settings WHERE id = ?1", (&key,)) .execute("DELETE FROM settings WHERE id = ?1", (&key,))
.map_err(|_| DbSaveError)?; .map_err(|_| DbSaveError)?;
@@ -44,7 +44,7 @@ fn create_settings(
key: String, key: String,
value: String, value: String,
connection: &Connection, connection: &Connection,
) -> Result<(), NotificationData> { ) -> MaybeError {
connection connection
.execute( .execute(
"INSERT into settings (id, value) VALUES (?1, ?2)", "INSERT into settings (id, value) VALUES (?1, ?2)",
@@ -58,7 +58,7 @@ fn update_settings(
key: String, key: String,
value: String, value: String,
connection: &Connection, connection: &Connection,
) -> Result<(), NotificationData> { ) -> MaybeError {
connection connection
.execute( .execute(
"update settings "update settings
+9 -9
View File
@@ -5,19 +5,19 @@ pub fn create_db() {
let db_file = db_path(); let db_file = db_path();
if !db_file.exists() { if !db_file.exists() {
std::fs::File::create(&db_file).expect("Database file creation failed"); std::fs::File::create(&db_file).expect("Database file creation failed");
let connection = Connection::open(&db_file).unwrap(); let connection = Connection::open(&db_file).expect("Connection open failed");
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap(); connection.execute("PRAGMA foreign_keys = ON;", []).expect("Failed to set PRAGMA");
create_tables(&connection); create_tables(&connection);
} }
} }
fn create_tables(conn: &Connection) { fn create_tables(conn: &Connection) {
make_card_set(conn).unwrap(); make_card_set(conn).expect("Failed to init table");
make_settings(conn).unwrap(); make_settings(conn).expect("Failed to init table");
make_word_group(conn).unwrap(); make_word_group(conn).expect("Failed to init table");
make_words(conn).unwrap(); make_words(conn).expect("Failed to init table");
make_card_stats(conn).unwrap(); make_card_stats(conn).expect("Failed to init table");
} }
fn make_settings(conn: &Connection) -> Result<(), rusqlite::Error> { fn make_settings(conn: &Connection) -> Result<(), rusqlite::Error> {
@@ -51,7 +51,7 @@ fn make_card_stats(conn: &Connection) -> Result<(), rusqlite::Error> {
pub fn default_connection() -> Connection { pub fn default_connection() -> Connection {
let db_file = db_path(); let db_file = db_path();
let connection = Connection::open(db_file).unwrap(); let connection = Connection::open(db_file).expect("Failed to open database");
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap(); connection.execute("PRAGMA foreign_keys = ON;", []).expect("Failed to set PRAGMA");
connection connection
} }
+48 -27
View File
@@ -1,18 +1,20 @@
use std::io; use crate::fs::*;
use crate::notification::NotificationData::{CompressError, PermissionDenied, WebApiUnavailable};
use crate::notification::{MaybeError, NotificationData};
use log::info; use log::info;
use std::io;
use tokio::fs::{File, OpenOptions}; use tokio::fs::{File, OpenOptions};
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use zstd::{DEFAULT_COMPRESSION_LEVEL, Decoder, Encoder}; use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL};
use crate::fs::*;
use crate::notification::NotificationData;
use crate::notification::NotificationData::{CompressError, PermissionDenied, WebApiUnavailable};
// const API_URL: &str = "http://127.0.0.1:8089/"; // const API_URL: &str = "http://127.0.0.1:8089/";
const API_URL: &str = "https://learning.micialware.ru/"; const API_URL: &str = "https://learning.micialware.ru/";
pub async fn send_data(id: String) -> Result<(), NotificationData> { pub async fn send_data(id: String) -> MaybeError {
let db_file = db_path(); let db_file = db_path();
let data = tokio::fs::read(db_file).await.map_err(|_| WebApiUnavailable)?; let data = tokio::fs::read(db_file)
.await
.map_err(|_| WebApiUnavailable)?;
let data = compress(data)?; let data = compress(data)?;
let api_url = API_URL.to_string(); let api_url = API_URL.to_string();
@@ -23,30 +25,35 @@ pub async fn send_data(id: String) -> Result<(), NotificationData> {
.post(&id_url) .post(&id_url)
.body(data) .body(data)
.send() .send()
.await.map_err(|_| WebApiUnavailable)? .await
.map_err(|_| WebApiUnavailable)?
.text() .text()
.await.map_err(|_| WebApiUnavailable)?; .await
.map_err(|_| WebApiUnavailable)?;
set_local_version(new_version.parse::<u32>().unwrap_or_default()).await; set_local_version(new_version.parse::<u32>().unwrap_or_default()).await;
Ok(()) Ok(())
} }
fn compress(data: Vec<u8>) -> Result<Vec<u8>, NotificationData> { fn compress(data: Vec<u8>) -> Result<Vec<u8>, NotificationData> {
let mut encoder = Encoder::new(Vec::new(), DEFAULT_COMPRESSION_LEVEL).map_err(|_| CompressError)?; let mut encoder =
Encoder::new(Vec::new(), DEFAULT_COMPRESSION_LEVEL).map_err(|_| CompressError)?;
io::copy(&mut &data[..], &mut encoder).map_err(|_| PermissionDenied)?; io::copy(&mut &data[..], &mut encoder).map_err(|_| PermissionDenied)?;
encoder.finish().map_err(|_| CompressError)?; encoder.finish().map_err(|_| CompressError)?;
Ok(data) Ok(data)
} }
pub async fn load_data(id: String, temp: bool) -> Result<(), NotificationData> { pub async fn load_data(id: String, temp: bool) -> MaybeError {
let api_url = API_URL.to_string(); let api_url = API_URL.to_string();
let id_url = format!("{api_url}download/{id}"); let id_url = format!("{api_url}download/{id}");
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let data = client let data = client
.get(&id_url) .get(&id_url)
.send() .send()
.await.map_err(|_| WebApiUnavailable)? .await
.map_err(|_| WebApiUnavailable)?
.bytes() .bytes()
.await.map_err(|_| WebApiUnavailable)? .await
.map_err(|_| WebApiUnavailable)?
.to_vec(); .to_vec();
let data = decompress(data)?; let data = decompress(data)?;
@@ -54,15 +61,19 @@ pub async fn load_data(id: String, temp: bool) -> Result<(), NotificationData> {
let file_name = if temp { "data.db.tmp" } else { "data.db" }; let file_name = if temp { "data.db.tmp" } else { "data.db" };
let db_file = path.join(file_name); let db_file = path.join(file_name);
tokio::fs::write(db_file, data).await.map_err(|_| PermissionDenied)?; tokio::fs::write(db_file, data)
.await
.map_err(|_| PermissionDenied)?;
let version_url = format!("{API_URL}{id}/version"); let version_url = format!("{API_URL}{id}/version");
let version = client let version = client
.get(&version_url) .get(&version_url)
.send() .send()
.await.map_err(|_| WebApiUnavailable)? .await
.map_err(|_| WebApiUnavailable)?
.text() .text()
.await.map_err(|_| WebApiUnavailable)?; .await
.map_err(|_| WebApiUnavailable)?;
set_local_version(version.parse::<u32>().unwrap_or_default()).await; set_local_version(version.parse::<u32>().unwrap_or_default()).await;
Ok(()) Ok(())
} }
@@ -93,22 +104,29 @@ pub async fn first_sync() -> Result<String, NotificationData> {
pub async fn get_web_version(key: &str) -> Result<u32, NotificationData> { pub async fn get_web_version(key: &str) -> Result<u32, NotificationData> {
let id_url = format!("{API_URL}{key}/version"); let id_url = format!("{API_URL}{key}/version");
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let version = client.get(&id_url).send().await.map_err(|_| WebApiUnavailable)?.text().await.map_err(|_| WebApiUnavailable)?; let version = client
.get(&id_url)
.send()
.await
.map_err(|_| WebApiUnavailable)?
.text()
.await
.map_err(|_| WebApiUnavailable)?;
info!("version: {}", version); info!("version: {}", version);
Ok(version.parse::<u32>().unwrap_or_default()) Ok(version.parse::<u32>().unwrap_or_default())
} }
pub async fn get_local_version() -> u32 { pub async fn get_local_version() -> Result<u32, NotificationData> {
let file = db_meta_path(); let file = db_meta_path();
if file.exists() { if file.exists() {
let mut data = String::new(); let mut data = String::new();
File::open(file.clone()) File::open(file.clone())
.await .await
.unwrap() .map_err(|_| PermissionDenied)?
.read_to_string(&mut data) .read_to_string(&mut data)
.await .await
.unwrap(); .map_err(|_| PermissionDenied)?;
return data.parse::<u32>().unwrap_or_default(); return Ok(data.parse::<u32>().unwrap_or_default());
} }
let mut file = OpenOptions::new() let mut file = OpenOptions::new()
@@ -117,12 +135,14 @@ pub async fn get_local_version() -> u32 {
.truncate(true) .truncate(true)
.open(file) .open(file)
.await .await
.unwrap(); .map_err(|_| PermissionDenied)?;
file.write_all("0".as_bytes()).await.unwrap(); file.write_all("0".as_bytes())
0 .await
.map_err(|_| PermissionDenied)?;
Ok(0)
} }
pub async fn set_local_version(version: u32) { pub async fn set_local_version(version: u32) -> Result<(), NotificationData> {
let file_path = db_meta_path(); let file_path = db_meta_path();
let mut file = OpenOptions::new() let mut file = OpenOptions::new()
.write(true) .write(true)
@@ -130,8 +150,9 @@ pub async fn set_local_version(version: u32) {
.truncate(true) .truncate(true)
.open(file_path) .open(file_path)
.await .await
.unwrap(); .map_err(|_| PermissionDenied)?;
file.write_all(version.to_string().as_bytes()) file.write_all(version.to_string().as_bytes())
.await .await
.unwrap(); .map_err(|_| PermissionDenied)?;
Ok(())
} }
+9 -9
View File
@@ -1,10 +1,10 @@
use crate::data::entities::{WordData, WordGroup}; use crate::data::entities::{WordData, WordGroup};
use crate::notification::NotificationData; use crate::notification::{MaybeError, NotificationData};
use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use rusqlite::{Connection, params}; use rusqlite::{Connection, params};
use std::collections::HashMap; use std::collections::HashMap;
pub fn add_word(word: &mut WordData, connection: &Connection) -> Result<(), NotificationData> { pub fn add_word(word: &mut WordData, connection: &Connection) -> MaybeError {
let index : u32 = 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",
@@ -25,7 +25,7 @@ pub fn add_word(word: &mut WordData, connection: &Connection) -> Result<(), Noti
pub fn add_words( pub fn add_words(
words: &mut [WordData], words: &mut [WordData],
connection: &mut Connection, connection: &mut Connection,
) -> Result<(), NotificationData> { ) -> MaybeError {
let tx = connection.transaction().map_err(|_| DbLoadError)?; let tx = connection.transaction().map_err(|_| DbLoadError)?;
let count = words.len(); let count = words.len();
@@ -60,7 +60,7 @@ pub fn add_words(
Ok(()) Ok(())
} }
pub fn update_word(word: &mut WordData, connection: &Connection) -> Result<(), NotificationData> { pub fn update_word(word: &mut WordData, connection: &Connection) -> MaybeError {
if !word.id.is_valid() { if !word.id.is_valid() {
Ok(add_word(word, connection)?) Ok(add_word(word, connection)?)
} else { } else {
@@ -71,7 +71,7 @@ pub fn update_word(word: &mut WordData, connection: &Connection) -> Result<(), N
&word.key, &word.key,
&word.value, &word.value,
&word.tags, &word.tags,
serde_json::to_string(&word.additional).unwrap(), serde_json::to_string(&word.additional).map_err(|_| DbSaveError)?,
&word.id, &word.id,
), ),
) )
@@ -80,7 +80,7 @@ pub fn update_word(word: &mut WordData, connection: &Connection) -> Result<(), N
} }
} }
pub fn delete_word(word: &WordData, connection: &Connection) -> Result<(), NotificationData> { pub fn delete_word(word: &WordData, connection: &Connection) -> MaybeError {
if !word.id.is_valid() { if !word.id.is_valid() {
return Err(DbSaveError); return Err(DbSaveError);
} }
@@ -133,7 +133,7 @@ pub fn load_word_groups(connection: &Connection) -> Result<Vec<WordGroup>, Notif
Ok(buffer) Ok(buffer)
} }
pub fn add_group(group: &mut WordGroup, connection: &Connection) -> Result<(), NotificationData> { pub fn add_group(group: &mut WordGroup, connection: &Connection) -> MaybeError {
let index: u32 = 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",
@@ -149,7 +149,7 @@ pub fn add_group(group: &mut WordGroup, connection: &Connection) -> Result<(), N
pub fn update_group( pub fn update_group(
group: &mut WordGroup, group: &mut WordGroup,
connection: &Connection, connection: &Connection,
) -> Result<(), NotificationData> { ) -> MaybeError {
if !group.id.is_valid() { if !group.id.is_valid() {
Ok(add_group(group, connection)?) Ok(add_group(group, connection)?)
} else { } else {
@@ -163,7 +163,7 @@ pub fn update_group(
} }
} }
pub fn delete_group(group: &WordGroup, connection: &Connection) -> Result<(), NotificationData> { pub fn delete_group(group: &WordGroup, connection: &Connection) -> MaybeError {
if !group.id.is_valid() { if !group.id.is_valid() {
return Err(DbSaveError); return Err(DbSaveError);
} }
+10
View File
@@ -10,4 +10,14 @@ macro_rules! push_error {
($e:expr) => { ($e:expr) => {
$crate::navigation::RootMessage::PushNotification($crate::notification::NotificationRequest::Error($e)) $crate::navigation::RootMessage::PushNotification($crate::notification::NotificationRequest::Error($e))
} }
}
#[inline(always)]
pub fn into_result<R, E>(option: Option<R>, error: E) -> Result<R, E>{
if let Some(r) = option {
Ok(r)
}
else {
Err(error)
}
} }
+1 -1
View File
@@ -172,7 +172,7 @@ impl ScreenState {
} }
async fn load_web_backup(string: String) -> Result<bool, NotificationData> { async fn load_web_backup(string: String) -> Result<bool, NotificationData> {
let local = get_local_version().await; let local = get_local_version().await?;
let web = get_web_version(string.as_str()).await?; let web = get_web_version(string.as_str()).await?;
info!("{} {}", local, web); info!("{} {}", local, web);
if web > local { if web > local {
+2
View File
@@ -192,3 +192,5 @@ pub enum NotificationData {
DbLoadError, DbLoadError,
CompressError, CompressError,
} }
pub type MaybeError = Result<(), NotificationData>;
+2 -2
View File
@@ -4,7 +4,7 @@ use crate::pages::import::ImportMessage::*;
use crate::data::entities::{WordData, WordGroup}; use crate::data::entities::{WordData, WordGroup};
use crate::navigation::{NavigatedPage, Page, RootMessage}; use crate::navigation::{NavigatedPage, Page, RootMessage};
use crate::notification::NotificationData::OpenFileError; use crate::notification::NotificationData::OpenFileError;
use crate::notification::NotificationData; use crate::notification::{MaybeError, NotificationData};
use crate::styling::*; use crate::styling::*;
use iced::widget::button::danger; use iced::widget::button::danger;
use iced::widget::container::success; use iced::widget::container::success;
@@ -373,7 +373,7 @@ impl ImportState {
) )
} }
fn extract_import_file(path: PathBuf) -> Result<(), NotificationData> { fn extract_import_file(path: PathBuf) -> MaybeError {
let file = File::open(path).map_err(|_| OpenFileError)?; let file = File::open(path).map_err(|_| OpenFileError)?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
let mut archive = ZipArchive::new(reader).map_err(|_| OpenFileError)?; let mut archive = ZipArchive::new(reader).map_err(|_| OpenFileError)?;
+3 -3
View File
@@ -6,7 +6,7 @@ use crate::data::entities::{CardStatistics, DeckSettings, WordData};
use crate::data_provider::voice::get_voice; use crate::data_provider::voice::get_voice;
use crate::navigation::Page::PreviousPage; use crate::navigation::Page::PreviousPage;
use crate::navigation::{KeyPressedPage, NavigatedPage, Page, RootMessage}; use crate::navigation::{KeyPressedPage, NavigatedPage, Page, RootMessage};
use crate::notification::NotificationData; use crate::notification::{MaybeError, NotificationData};
use crate::styling::*; use crate::styling::*;
use crate::{push_error, push_error_task}; use crate::{push_error, push_error_task};
use chrono::Local; use chrono::Local;
@@ -337,7 +337,7 @@ pub enum RepetitionMessage {
async fn play_sound( async fn play_sound(
sink: Arc<Option<MixerDeviceSink>>, sink: Arc<Option<MixerDeviceSink>>,
text: String, text: String,
) -> Result<(), NotificationData> { ) -> MaybeError {
let data = get_voice(text.as_str()).await?; let data = get_voice(text.as_str()).await?;
spawn_blocking(move || { spawn_blocking(move || {
if let Some(sink) = sink.as_ref() if let Some(sink) = sink.as_ref()
@@ -347,6 +347,6 @@ async fn play_sound(
} }
}) })
.await .await
.unwrap(); .expect("Playing sound failed");
Ok(()) Ok(())
} }
+3 -3
View File
@@ -4,7 +4,7 @@ use crate::data_provider::card_sets::{delete_set, update_deck};
use crate::data_provider::card_stats::{delete_stats, load_stats_of_deck}; use crate::data_provider::card_stats::{delete_stats, load_stats_of_deck};
use crate::navigation::Page::PreviousPage; use crate::navigation::Page::PreviousPage;
use crate::navigation::{NavigatedPage, Page, RootMessage}; use crate::navigation::{NavigatedPage, Page, RootMessage};
use crate::notification::NotificationData; use crate::notification::MaybeError;
use crate::pages::repetition_settings::RepetitionSettingsMessage::*; use crate::pages::repetition_settings::RepetitionSettingsMessage::*;
use crate::styling::*; use crate::styling::*;
use crate::{push_error, push_error_task}; use crate::{push_error, push_error_task};
@@ -13,7 +13,7 @@ use iced::widget::text_input::Catalog;
use iced::widget::{button, column, row, scrollable, space, text, text_input}; use iced::widget::{button, column, row, scrollable, space, text, text_input};
use iced::{Element, Task}; use iced::{Element, Task};
use iced_core::Length::Fill; use iced_core::Length::Fill;
use iced_core::{Padding, Theme, color}; use iced_core::{color, Padding, Theme};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@@ -179,7 +179,7 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
} }
} }
fn update_deck_stats(deck: &DeckSettings, state: &AppState) -> Result<(), NotificationData> { fn update_deck_stats(deck: &DeckSettings, state: &AppState) -> MaybeError {
let mut db_stats = load_stats_of_deck(deck, &state.connection).expect("Wrong db operation"); let mut db_stats = load_stats_of_deck(deck, &state.connection).expect("Wrong db operation");
let filtered_words = deck.get_word_list(state); let filtered_words = deck.get_word_list(state);
filtered_words.iter().for_each(|word| { filtered_words.iter().for_each(|word| {
+9 -9
View File
@@ -1,12 +1,12 @@
use crate::app::AppState; use crate::app::AppState;
use crate::core::srs::{AppendMode, OrderMode}; use crate::core::srs::{AppendMode, OrderMode};
use crate::data::Id;
use crate::data::entities::{CardStatistics, DeckSettings}; use crate::data::entities::{CardStatistics, DeckSettings};
use crate::data::Id;
use crate::data_provider::card_sets::update_deck; use crate::data_provider::card_sets::update_deck;
use crate::data_provider::card_stats::{add_stat_list, load_stats_of_deck}; use crate::data_provider::card_stats::{add_stat_list, load_stats_of_deck};
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings}; use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
use crate::navigation::{NavigatedPage, Page, RootMessage}; use crate::navigation::{NavigatedPage, Page, RootMessage};
use crate::notification::{NotificationData, NotificationRequest}; use crate::notification::{MaybeError, NotificationRequest};
use crate::pages::history::HistoryState; use crate::pages::history::HistoryState;
use crate::pages::repetition::RepetitionState; use crate::pages::repetition::RepetitionState;
use crate::pages::repetition_settings::RepetitionSettingsState; use crate::pages::repetition_settings::RepetitionSettingsState;
@@ -16,17 +16,17 @@ use crate::push_error_task;
use crate::styling::*; use crate::styling::*;
use chrono::{Days, Local, Utc}; use chrono::{Days, Local, Utc};
use hashbrown::{HashMap, HashSet}; use hashbrown::{HashMap, HashSet};
use iced::widget::button::{danger, Status};
pub use iced::widget::button::{Catalog, Style}; pub use iced::widget::button::{Catalog, Style};
use iced::widget::button::{Status, danger};
use iced::widget::tooltip::Position::Top; use iced::widget::tooltip::Position::Top;
use iced::widget::{ use iced::widget::{
Column, Row, button, column, container, lazy, radio, row, scrollable, space, svg, text, button, column, container, lazy, radio, row, scrollable, space, svg, text, text_input, tooltip,
text_input, tooltip, Column, Row,
}; };
use iced::{Background, Border, Center, Color, Element, Fill, Length, Shadow, Task, Theme}; use iced::{Background, Border, Center, Color, Element, Fill, Length, Shadow, Task, Theme};
use iced_core::Padding;
use iced_core::border::Radius; use iced_core::border::Radius;
use iced_core::svg::Handle; use iced_core::svg::Handle;
use iced_core::Padding;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
@@ -562,7 +562,7 @@ impl RepetitionsState {
} }
impl RepetitionsState { impl RepetitionsState {
fn append_all_words(state: &mut AppState, deck: &DeckViewData) -> Result<(), NotificationData> { fn append_all_words(state: &mut AppState, deck: &DeckViewData) -> MaybeError {
let existing = &deck.existing_words_indices; let existing = &deck.existing_words_indices;
let mut created_set = HashSet::with_capacity(existing.len()); let mut created_set = HashSet::with_capacity(existing.len());
existing.iter().for_each(|c| { existing.iter().for_each(|c| {
@@ -580,7 +580,7 @@ impl RepetitionsState {
state: &mut AppState, state: &mut AppState,
deck: &DeckSettings, deck: &DeckSettings,
indices: impl Iterator<Item = usize>, indices: impl Iterator<Item = usize>,
) -> Result<(), NotificationData> { ) -> MaybeError {
let words = &state.dictionary; let words = &state.dictionary;
let stats = &mut indices let stats = &mut indices
.map(|i| { .map(|i| {
@@ -618,7 +618,7 @@ impl RepetitionsState {
self.selected_deck_index = None; self.selected_deck_index = None;
} }
fn fill_deck_word_indices(&mut self) -> Result<(), NotificationData> { fn fill_deck_word_indices(&mut self) -> MaybeError {
let state = &self.state.lock(); let state = &self.state.lock();
let mut deck = self.selected_deck().unwrap().clone(); let mut deck = self.selected_deck().unwrap().clone();
let added: Vec<_> = load_stats_of_deck(&deck, &state.connection)? let added: Vec<_> = load_stats_of_deck(&deck, &state.connection)?