Introduce MaybeError type
This commit is contained in:
1 parent
d836b57d2a
commit
a60ed37cd4
16 files changed
+127
-93
No files matched your search
@@ -1,10 +1,10 @@
|
||||
use crate::data::entities::{WordData, WordGroup};
|
||||
use crate::notification::NotificationData;
|
||||
use crate::notification::{MaybeError, NotificationData};
|
||||
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
||||
use rusqlite::{Connection, params};
|
||||
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
|
||||
.query_row(
|
||||
"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(
|
||||
words: &mut [WordData],
|
||||
connection: &mut Connection,
|
||||
) -> Result<(), NotificationData> {
|
||||
) -> MaybeError {
|
||||
let tx = connection.transaction().map_err(|_| DbLoadError)?;
|
||||
let count = words.len();
|
||||
|
||||
@@ -60,7 +60,7 @@ pub fn add_words(
|
||||
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() {
|
||||
Ok(add_word(word, connection)?)
|
||||
} else {
|
||||
@@ -71,7 +71,7 @@ pub fn update_word(word: &mut WordData, connection: &Connection) -> Result<(), N
|
||||
&word.key,
|
||||
&word.value,
|
||||
&word.tags,
|
||||
serde_json::to_string(&word.additional).unwrap(),
|
||||
serde_json::to_string(&word.additional).map_err(|_| DbSaveError)?,
|
||||
&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() {
|
||||
return Err(DbSaveError);
|
||||
}
|
||||
@@ -133,7 +133,7 @@ pub fn load_word_groups(connection: &Connection) -> Result<Vec<WordGroup>, Notif
|
||||
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
|
||||
.query_row(
|
||||
"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(
|
||||
group: &mut WordGroup,
|
||||
connection: &Connection,
|
||||
) -> Result<(), NotificationData> {
|
||||
) -> MaybeError {
|
||||
if !group.id.is_valid() {
|
||||
Ok(add_group(group, connection)?)
|
||||
} 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() {
|
||||
return Err(DbSaveError);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user