remove more unwrap

This commit is contained in:
micialware committed 2026-08-24 23:10:23 +03:00
1 parent 5a6747030b
commit b5b1eb768c
6 files changed
+38 -26

No files matched your search

+12 -10
View File
@@ -1,6 +1,6 @@
use crate::core::srs::WordOpenMode;
use crate::data::entities::HistoryItem;
use crate::data::Id;
use crate::data::entities::HistoryItem;
use crate::fs::*;
use crate::notification::NotificationData;
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))
}
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() {
let reader = BufReader::new(File::open(path).unwrap());
for line in reader.lines() {
lines.push(line.unwrap());
}
let reader = BufReader::new(File::open(path).map_err(|_| PermissionDenied)?);
reader
.lines()
.map_while(Result::ok)
.for_each(|i| lines.push(i));
}
Ok(())
}
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() {
continue;
}
let id = id.unwrap();
let id = id.unwrap_or_default();
if !id.is_valid() {
continue;
}
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,
mode: match mode.parse::<u8>().unwrap_or_default() {
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)?;
Ok(())
}
+5 -5
View File
@@ -39,8 +39,8 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
let counts = count_stmt
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
.unwrap()
.map(|x| x.unwrap())
.map_err(|_| NotificationData::IncorrectAnkiFile)?
.filter_map(|x| x.ok())
.collect::<Vec<(i64, i64)>>();
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())
.collect();
let id = collection.get("id");
if id.is_none() {
if id.is_none() || id.unwrap().as_u64().is_none(){
return Err(NotificationData::IncorrectAnkiFile);
}
let id = id.unwrap().as_u64().unwrap();
let name = collection.get("name");
if name.is_none() {
if name.is_none() || name.unwrap().as_str().is_none() {
return Err(NotificationData::IncorrectAnkiFile);
}
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(|x| x.unwrap())
.filter_map(|x| x.ok())
.collect::<Vec<ImportNote>>())
}
+18 -8
View File
@@ -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())
}
+1 -1
View File
@@ -4,7 +4,7 @@ use crate::fs::*;
pub fn create_db() {
let db_file = db_path();
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();
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();