Data provider refactor #11
No files matched your search
+1
-1
@@ -6,7 +6,7 @@ use std::str::FromStr;
|
|||||||
use rusqlite::ToSql;
|
use rusqlite::ToSql;
|
||||||
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
|
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy, Ord, PartialOrd)]
|
#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy, Ord, PartialOrd, Default)]
|
||||||
pub struct Id(pub(crate) u32);
|
pub struct Id(pub(crate) u32);
|
||||||
pub const INVALID_ID: Id = Id(0);
|
pub const INVALID_ID: Id = Id(0);
|
||||||
impl FromStr for Id {
|
impl FromStr for Id {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::core::srs::WordOpenMode;
|
use crate::core::srs::WordOpenMode;
|
||||||
use crate::data::entities::HistoryItem;
|
|
||||||
use crate::data::Id;
|
use crate::data::Id;
|
||||||
|
use crate::data::entities::HistoryItem;
|
||||||
use crate::fs::*;
|
use crate::fs::*;
|
||||||
use crate::notification::NotificationData;
|
use crate::notification::NotificationData;
|
||||||
use crate::notification::NotificationData::PermissionDenied;
|
use crate::notification::NotificationData::PermissionDenied;
|
||||||
@@ -20,13 +20,16 @@ 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>) {
|
fn append_lines_if_exists(path: PathBuf, lines: &mut Vec<String>) -> Result<(), NotificationData> {
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
let reader = BufReader::new(File::open(path).unwrap());
|
let reader = BufReader::new(File::open(path).map_err(|_| PermissionDenied)?);
|
||||||
for line in reader.lines() {
|
reader
|
||||||
lines.push(line.unwrap());
|
.lines()
|
||||||
}
|
.map_while(Result::ok)
|
||||||
|
.for_each(|i| lines.push(i));
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
|
fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
|
||||||
@@ -37,12 +40,13 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
|
|||||||
if id.is_err() {
|
if id.is_err() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let id = id.unwrap();
|
let id = id.unwrap_or_default();
|
||||||
if !id.is_valid() {
|
if !id.is_valid() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
items.push(HistoryItem {
|
items.push(HistoryItem {
|
||||||
timestamp: DateTime::from_timestamp(time.parse().unwrap_or_default(), 0).unwrap_or_default(),
|
timestamp: DateTime::from_timestamp(time.parse().unwrap_or_default(), 0)
|
||||||
|
.unwrap_or_default(),
|
||||||
word_id: id,
|
word_id: id,
|
||||||
mode: match mode.parse::<u8>().unwrap_or_default() {
|
mode: match mode.parse::<u8>().unwrap_or_default() {
|
||||||
2 => WordOpenMode::Hard,
|
2 => WordOpenMode::Hard,
|
||||||
@@ -84,5 +88,3 @@ pub fn push_note(set_id: Id, item: HistoryItem) -> Result<(), NotificationData>
|
|||||||
writeln!(&mut file, "{}", line_str).map_err(|_| PermissionDenied)?;
|
writeln!(&mut file, "{}", line_str).map_err(|_| PermissionDenied)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -39,8 +39,8 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
|
|||||||
|
|
||||||
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)?)))
|
||||||
.unwrap()
|
.map_err(|_| NotificationData::IncorrectAnkiFile)?
|
||||||
.map(|x| x.unwrap())
|
.filter_map(|x| x.ok())
|
||||||
.collect::<Vec<(i64, i64)>>();
|
.collect::<Vec<(i64, i64)>>();
|
||||||
|
|
||||||
for (_, collection) in sets {
|
for (_, collection) in sets {
|
||||||
@@ -54,12 +54,12 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
|
|||||||
.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() {
|
if id.is_none() || id.unwrap().as_u64().is_none(){
|
||||||
return Err(NotificationData::IncorrectAnkiFile);
|
return Err(NotificationData::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() {
|
if name.is_none() || name.unwrap().as_str().is_none() {
|
||||||
return Err(NotificationData::IncorrectAnkiFile);
|
return Err(NotificationData::IncorrectAnkiFile);
|
||||||
}
|
}
|
||||||
let name = name.unwrap().as_str().unwrap().to_string();
|
let name = name.unwrap().as_str().unwrap().to_string();
|
||||||
@@ -101,6 +101,6 @@ pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Result<Vec<
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
.map_err(|_| NotificationData::IncorrectAnkiFile)?
|
.map_err(|_| NotificationData::IncorrectAnkiFile)?
|
||||||
.map(|x| x.unwrap())
|
.filter_map(|x| x.ok())
|
||||||
.collect::<Vec<ImportNote>>())
|
.collect::<Vec<ImportNote>>())
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
use crate::notification::NotificationData;
|
use crate::notification::NotificationData;
|
||||||
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
||||||
|
use iced::futures::StreamExt;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
|
|
||||||
pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
|
pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
|
||||||
let mut stmt = connection
|
let mut stmt = connection
|
||||||
.prepare("SELECT value FROM settings WHERE id = ?1")
|
.prepare("SELECT value FROM settings WHERE id = ?1")
|
||||||
.unwrap();
|
.expect("Failed to prepare statement.");
|
||||||
let mut iter = stmt.query_map((key,), |row| row.get(0)).unwrap();
|
let mut iter = stmt.query_map((key,), |row| row.get(0)).ok()?;
|
||||||
|
|
||||||
if let Some(row) = iter.next()
|
if let Some(row) = iter.next()
|
||||||
&& let Ok(value) = row
|
&& let Ok(value) = row
|
||||||
@@ -32,13 +33,18 @@ pub fn set_setting(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_settings(key: String, connection: &Connection) {
|
pub fn delete_settings(key: String, connection: &Connection) -> Result<(), NotificationData> {
|
||||||
connection
|
connection
|
||||||
.execute("DELETE FROM settings WHERE id = ?1", (&key,))
|
.execute("DELETE FROM settings WHERE id = ?1", (&key,))
|
||||||
.unwrap();
|
.map_err(|_| DbSaveError)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_settings(key: String, value: String, connection: &Connection) -> Result<(), NotificationData>{
|
fn create_settings(
|
||||||
|
key: String,
|
||||||
|
value: String,
|
||||||
|
connection: &Connection,
|
||||||
|
) -> Result<(), NotificationData> {
|
||||||
connection
|
connection
|
||||||
.execute(
|
.execute(
|
||||||
"INSERT into settings (id, value) VALUES (?1, ?2)",
|
"INSERT into settings (id, value) VALUES (?1, ?2)",
|
||||||
@@ -65,7 +71,11 @@ where id = ?1",
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_settings_list(connection: &Connection) -> Result<Vec<String>, NotificationData> {
|
fn get_settings_list(connection: &Connection) -> Result<Vec<String>, NotificationData> {
|
||||||
let mut stmt = connection.prepare("SELECT id FROM settings").expect("prepare stmt failed");
|
let mut stmt = connection
|
||||||
let iter = stmt.query_map((), |row| row.get(0)).map_err(|_| DbLoadError)?;
|
.prepare("SELECT id FROM settings")
|
||||||
Ok(iter.map(|row| row.unwrap()).collect())
|
.expect("prepare stmt failed");
|
||||||
|
let iter = stmt
|
||||||
|
.query_map((), |row| row.get(0))
|
||||||
|
.map_err(|_| DbLoadError)?;
|
||||||
|
Ok(iter.filter_map(|i| i.ok()).collect())
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@ use crate::fs::*;
|
|||||||
pub fn create_db() {
|
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).unwrap();
|
std::fs::File::create(&db_file).expect("Database file creation failed");
|
||||||
let connection = Connection::open(&db_file).unwrap();
|
let connection = Connection::open(&db_file).unwrap();
|
||||||
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
|
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
|
||||||
|
|
||||||
|
|||||||
@@ -450,7 +450,7 @@ impl RepetitionsState {
|
|||||||
|
|
||||||
let date = (*now)
|
let date = (*now)
|
||||||
.checked_sub_days(Days::new(30 * 7 - i * 7 - j - 1))
|
.checked_sub_days(Days::new(30 * 7 - i * 7 - j - 1))
|
||||||
.unwrap();
|
.unwrap_or_default();
|
||||||
|
|
||||||
column = column.push(tooltip(
|
column = column.push(tooltip(
|
||||||
iced::widget::container(space().height(15).width(15)).style(
|
iced::widget::container(space().height(15).width(15)).style(
|
||||||
|
|||||||
Reference in new issue
Block a user