Remove many unwrap

This commit is contained in:
2026-08-19 19:21:21 +03:00
parent 39f8845119
commit 01f0999772
5 changed files with 40 additions and 21 deletions
+1 -1
View File
@@ -611,7 +611,7 @@ pub fn app_data_dir() -> PathBuf {
let mut dir = dirs::data_dir().unwrap();
dir.push("jap_learn");
if !dir.exists() {
fs::create_dir(dir.clone()).unwrap();
fs::create_dir_all(dir.clone()).unwrap();
}
dir
+10 -4
View File
@@ -171,11 +171,17 @@ impl DictionaryQuizState {
self.is_help = false;
self.answer = String::new();
if self.current_set.is_empty() {
self.update_set();
}
let next : WordData =
match self.current_set.pop() {
Some(word) => {
word
}
None => {
self.update_set();
self.current_set.pop().expect("Empty set")
}
};
let next = self.current_set.pop().unwrap();
if self.reverse {
self.view = next.value.clone();
self.correct = next.key.clone();
+1 -1
View File
@@ -512,7 +512,7 @@ impl ImportState {
.map(|name| (name.clone(), Vec::<usize>::with_capacity(1)))
.collect::<Vec<_>>();
for (key, endpoint) in &group.mapping {
let property_index = group.fields.iter().position(|f| f == key).unwrap();
let property_index = group.fields.iter().position(|f| f == key).expect("Unknown property");
let group_index = result
.iter()
.position(|(name, _)| name == endpoint)
+17 -11
View File
@@ -500,12 +500,14 @@ impl RandomSRSModule {
}
impl SRSModule for RandomSRSModule {
fn next(&mut self, set: &mut DeckData) -> usize {
if self.basket.is_empty() {
self.basket = (0..set.words.len()).collect::<Vec<usize>>();
self.basket.shuffle(&mut rand::rng())
match self.basket.pop() {
Some(index) => index,
None => {
self.basket = (0..set.words.len()).collect::<Vec<usize>>();
self.basket.shuffle(&mut rand::rng());
self.basket.pop().expect("empty basket")
}
}
self.basket.pop().unwrap()
}
fn open(&mut self, _: WordOpenMode, _: usize, _: CardStatistics) {}
@@ -552,7 +554,7 @@ impl SRSModule for SemiRandomSRSModule {
let new_weight = (100.0 / word.calculated_score()).powf(2.0);
self.last_weights
.update_weights(&[(index, &new_weight)])
.unwrap();
.expect("update weights error");
}
fn init(&mut self, set: &mut DeckData) {
self.initialized = true;
@@ -585,12 +587,16 @@ impl SRSModule for WorstWordsSRSModule {
self.rounds_remaining = self.rounds_count;
}
if self.queue.is_empty() {
self.queue.append(&mut self.pool.clone());
self.rounds_remaining -= 1;
match self.queue.pop() {
Some(index) => {
index
}
None => {
self.queue.append(&mut self.pool.clone());
self.rounds_remaining -= 1;
self.queue.pop().expect("empty queue")
}
}
self.queue.pop().unwrap()
}
fn open(&mut self, _: WordOpenMode, _: usize, _: CardStatistics) {}
+11 -4
View File
@@ -3,7 +3,7 @@ use crate::data_provider::sqlite::{create_db, default_connection};
use crate::data_provider::web_api::{
get_local_version, get_web_version, load_data, set_local_version,
};
use crate::dictionary::{DictionaryMessage, DictionaryState, app_data_dir};
use crate::dictionary::{app_data_dir, DictionaryMessage, DictionaryState};
use crate::dictionary_test::{DictionaryQuizMessage, DictionaryQuizState};
use crate::history::{HistoryMessage, HistoryState};
use crate::import::{ImportMessage, ImportState};
@@ -23,7 +23,7 @@ use crate::sync::{SyncMessage, SyncState};
use crate::view_navigation;
use crate::word::{WordMessage, WordState};
use crate::writing::{WritingMessage, WritingState};
use crate::{AppState, fill_state};
use crate::{fill_state, AppState};
use chrono::NaiveDate;
use hashbrown::HashMap;
use iced::keyboard::Event;
@@ -126,7 +126,11 @@ impl ScreenState {
let mut map = HashMap::new();
for file in directory.read_dir().unwrap().flatten() {
let mut vec = vec![];
let history_file_name = file.file_name().into_string().unwrap();
let history_file_name = match file.file_name().into_string() {
Err(_) => continue,
Ok(v) => v,
};
let id: Id = history_file_name[4..history_file_name.len() - 12]
.parse::<Id>()
.unwrap();
@@ -303,7 +307,10 @@ macro_rules! message_navigation {
($msg:expr, $stack:expr, $state:expr) => {
if let Some(new_page) = $state.navigate(&$msg) {
if let Page::PreviousPage = new_page {
$stack.pop();
if $stack.len() > 1 {
$stack.pop();
}
return (Task::<RootMessage>::none(), true);
} else {
$stack.push(new_page);