remove more unwrap
This commit is contained in:
1 parent
5a6747030b
commit
b5b1eb768c
6 files changed
+38
-26
No files matched your search
@@ -1,12 +1,13 @@
|
||||
use crate::notification::NotificationData;
|
||||
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
||||
use iced::futures::StreamExt;
|
||||
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 mut iter = stmt.query_map((key,), |row| row.get(0)).unwrap();
|
||||
.expect("Failed to prepare statement.");
|
||||
let mut iter = stmt.query_map((key,), |row| row.get(0)).ok()?;
|
||||
|
||||
if let Some(row) = iter.next()
|
||||
&& let Ok(value) = row
|
||||
@@ -32,13 +33,18 @@ pub fn set_setting(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_settings(key: String, connection: &Connection) {
|
||||
pub fn delete_settings(key: String, connection: &Connection) -> Result<(), NotificationData> {
|
||||
connection
|
||||
.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
|
||||
.execute(
|
||||
"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> {
|
||||
let mut stmt = connection.prepare("SELECT id FROM settings").expect("prepare stmt failed");
|
||||
let iter = stmt.query_map((), |row| row.get(0)).map_err(|_| DbLoadError)?;
|
||||
Ok(iter.map(|row| row.unwrap()).collect())
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT id FROM settings")
|
||||
.expect("prepare stmt failed");
|
||||
let iter = stmt
|
||||
.query_map((), |row| row.get(0))
|
||||
.map_err(|_| DbLoadError)?;
|
||||
Ok(iter.filter_map(|i| i.ok()).collect())
|
||||
}
|
||||
Reference in new issue
Block a user