Remove more unwrap
This commit is contained in:
1 parent
b5b1eb768c
commit
d836b57d2a
5 files changed
+74
-62
No files matched your search
+36
-33
@@ -1,12 +1,15 @@
|
||||
use crate::app::AppState;
|
||||
use crate::core::srs::{OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule, WordOpenMode, WorstWordsSRSModule};
|
||||
use crate::core::srs::{
|
||||
OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule, WordOpenMode,
|
||||
WorstWordsSRSModule,
|
||||
};
|
||||
use crate::data::entities::*;
|
||||
use crate::data_provider::card_stats::{load_stats_of_deck, update_stat_score};
|
||||
use crate::data_provider::history::push_note;
|
||||
use crate::notification::NotificationData;
|
||||
use chrono::Utc;
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
use crate::notification::NotificationData;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DeckData {
|
||||
@@ -18,7 +21,10 @@ pub struct DeckData {
|
||||
settings: DeckSettings,
|
||||
}
|
||||
impl DeckData {
|
||||
pub fn new(settings: &DeckSettings, state: Arc<Mutex<AppState>>) -> Result<Self, NotificationData> {
|
||||
pub fn new(
|
||||
settings: &DeckSettings,
|
||||
state: Arc<Mutex<AppState>>,
|
||||
) -> Result<Self, NotificationData> {
|
||||
let state_for = state.clone();
|
||||
let state_locked = state.lock();
|
||||
|
||||
@@ -83,42 +89,39 @@ impl DeckData {
|
||||
(self.words[index].clone(), self.set[index].clone())
|
||||
}
|
||||
pub fn open(&mut self, status: WordOpenMode) -> Result<(), NotificationData> {
|
||||
if self.current_word_index.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
let index = self.current_word_index.unwrap();
|
||||
if let Some(index) = self.current_word_index {
|
||||
let word: &mut CardStatistics = &mut self.set[index];
|
||||
let old_score = word.score;
|
||||
word.update(status);
|
||||
let new_score = word.score;
|
||||
|
||||
let word : &mut CardStatistics = &mut self.set[index];
|
||||
let old_score = word.score;
|
||||
word.update(status);
|
||||
let new_score = word.score;
|
||||
|
||||
match self.order_module {
|
||||
OrderModule::SemiRandomSRS(ref mut module) => {
|
||||
module.open(status, index, word);
|
||||
}
|
||||
OrderModule::RandomSRS(ref mut module) => {
|
||||
module.open(status, index, word);
|
||||
}
|
||||
OrderModule::WorstWordsSRS(ref mut module) => {
|
||||
module.open(status, index, word);
|
||||
match self.order_module {
|
||||
OrderModule::SemiRandomSRS(ref mut module) => {
|
||||
module.open(status, index, word);
|
||||
}
|
||||
OrderModule::RandomSRS(ref mut module) => {
|
||||
module.open(status, index, word);
|
||||
}
|
||||
OrderModule::WorstWordsSRS(ref mut module) => {
|
||||
module.open(status, index, word);
|
||||
}
|
||||
}
|
||||
update_stat_score(word, &self.state.lock().connection)?;
|
||||
let _ = push_note(
|
||||
self.settings.id,
|
||||
HistoryItem {
|
||||
timestamp: Utc::now(),
|
||||
word_id: word.word_id,
|
||||
mode: status,
|
||||
before: old_score,
|
||||
after: new_score,
|
||||
},
|
||||
);
|
||||
}
|
||||
update_stat_score(word, &self.state.lock().connection)?;
|
||||
let _ = push_note(
|
||||
self.settings.id,
|
||||
HistoryItem {
|
||||
timestamp: Utc::now(),
|
||||
word_id: word.word_id,
|
||||
mode: status,
|
||||
before: old_score,
|
||||
after: new_score,
|
||||
},
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.set.len()
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -111,7 +111,7 @@ impl SRSModule for SemiRandomSRSModule {
|
||||
.iter()
|
||||
.map(|s| (100.0 / s.calculated_score()).powf(2.0) * 2.0)
|
||||
.collect::<Vec<f32>>();
|
||||
self.last_weights = WeightedIndex::new(weights).unwrap();
|
||||
self.last_weights = WeightedIndex::new(weights).expect("Incorrect weights update");
|
||||
}
|
||||
}
|
||||
impl SemiRandomSRSModule {
|
||||
|
||||
@@ -105,12 +105,15 @@ impl DeckSettings {
|
||||
.iter()
|
||||
.find(|g| g.id == word.group_id)
|
||||
.cloned()
|
||||
.unwrap()
|
||||
.expect("No group id")
|
||||
.name,
|
||||
);
|
||||
|
||||
let result = engine.eval_ast_with_scope::<bool>(&mut scope, &ast);
|
||||
result.is_ok() && result.unwrap()
|
||||
if let Ok(result) = result {
|
||||
return result
|
||||
}
|
||||
false
|
||||
})
|
||||
.map(|(index, _)| index)
|
||||
.collect();
|
||||
|
||||
@@ -4,10 +4,10 @@ use crate::notification::NotificationData;
|
||||
use crate::notification::NotificationData::PermissionDenied;
|
||||
|
||||
pub fn app_data_dir() -> PathBuf {
|
||||
let mut dir = dirs::data_dir().unwrap();
|
||||
let mut dir = dirs::data_dir().expect("Unsupported OS");
|
||||
dir.push("jap_learn");
|
||||
if !dir.exists() {
|
||||
fs::create_dir_all(dir.clone()).unwrap();
|
||||
fs::create_dir_all(dir.clone()).expect("Unable to create directory");
|
||||
}
|
||||
|
||||
dir
|
||||
|
||||
+30
-24
@@ -1,11 +1,12 @@
|
||||
use crate::app::{fill_state, AppState};
|
||||
use crate::data::Id;
|
||||
use crate::data_provider::history::get_history_of_set;
|
||||
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::fs::*;
|
||||
use crate::message_navigation;
|
||||
use crate::{message_navigation, push_error, push_error_task};
|
||||
use crate::navigation::Page::*;
|
||||
use crate::navigation::RootMessage::{
|
||||
DataLoaded, Keyboard, Notification, PushNotification, UpdateData,
|
||||
@@ -40,7 +41,7 @@ use parking_lot::Mutex;
|
||||
use rusqlite::Connection;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use crate::data::Id;
|
||||
use crate::notification::NotificationData::PermissionDenied;
|
||||
|
||||
impl Default for ScreenState {
|
||||
fn default() -> Self {
|
||||
@@ -110,12 +111,13 @@ impl ScreenState {
|
||||
let state = ScreenState::default();
|
||||
let reading_additional_task = Task::perform(Self::load_additional_data(), |data| data);
|
||||
|
||||
let final_task;
|
||||
let mut final_task = reading_additional_task;
|
||||
let state_guard = state.app_state.lock();
|
||||
if state_guard.sync_data.auto_web_fetch {
|
||||
let key = state_guard.sync_data.key.clone().unwrap();
|
||||
if state_guard.sync_data.auto_web_fetch
|
||||
&& let Some(key) = state_guard.sync_data.key.clone()
|
||||
{
|
||||
final_task = Task::batch([
|
||||
reading_additional_task,
|
||||
final_task,
|
||||
Task::perform(Self::load_web_backup(key), |result| match result {
|
||||
Ok(update) => {
|
||||
if update {
|
||||
@@ -127,8 +129,6 @@ impl ScreenState {
|
||||
Err(err) => PushNotification(NotificationRequest::Warning(err)),
|
||||
}),
|
||||
]);
|
||||
} else {
|
||||
final_task = reading_additional_task;
|
||||
}
|
||||
|
||||
drop(state_guard);
|
||||
@@ -138,21 +138,23 @@ impl ScreenState {
|
||||
|
||||
async fn load_additional_data() -> RootMessage {
|
||||
let directory = history_dir();
|
||||
|
||||
if let Ok(directory) = directory {
|
||||
let mut map = HashMap::new();
|
||||
for file in directory.read_dir().unwrap().flatten() {
|
||||
for file in directory
|
||||
.read_dir()
|
||||
.expect("Cant read history directory")
|
||||
.flatten()
|
||||
{
|
||||
let mut vec = vec![];
|
||||
let history_file_name = match file.file_name().into_string() {
|
||||
Err(_) => continue,
|
||||
Ok(v) => v,
|
||||
};
|
||||
|
||||
let id = history_file_name[4..history_file_name.len() - 12]
|
||||
.parse::<Id>()
|
||||
.unwrap();
|
||||
|
||||
let history = get_history_of_set(id);
|
||||
if let Ok(history) = history {
|
||||
if let Ok(id) = history_file_name[4..history_file_name.len() - 12].parse::<Id>()
|
||||
&& let Ok(history) = get_history_of_set(id)
|
||||
{
|
||||
let by_date = history.chunk_by(|x, x1| {
|
||||
x.timestamp.naive_local().date() == x1.timestamp.naive_local().date()
|
||||
});
|
||||
@@ -161,7 +163,7 @@ impl ScreenState {
|
||||
}
|
||||
map.insert(id, vec);
|
||||
} else {
|
||||
return PushNotification(NotificationRequest::Warning(history.err().unwrap()));
|
||||
return PushNotification(NotificationRequest::Warning(PermissionDenied));
|
||||
}
|
||||
}
|
||||
return DataLoaded(map);
|
||||
@@ -203,7 +205,9 @@ impl ScreenState {
|
||||
}
|
||||
let db_file = db_path();
|
||||
let temp_db_file = temp_db_path();
|
||||
std::fs::rename(temp_db_file, &db_file).unwrap();
|
||||
if std::fs::rename(temp_db_file, &db_file).is_err(){
|
||||
return push_error_task!(PermissionDenied)
|
||||
}
|
||||
|
||||
state.connection = default_connection();
|
||||
fill_state(&mut state);
|
||||
@@ -290,11 +294,13 @@ impl ScreenState {
|
||||
}
|
||||
|
||||
fn handle_keyboard(&mut self, message: Event) -> Task<RootMessage> {
|
||||
let page = self.stack.last_mut().unwrap();
|
||||
match page {
|
||||
Repetition(page) => page.press(&message),
|
||||
_ => Task::none(),
|
||||
if let Some(page) = self.stack.last_mut(){
|
||||
return match page {
|
||||
Repetition(page) => page.press(&message),
|
||||
_ => Task::none(),
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,7 +312,7 @@ pub struct AppSettings {
|
||||
#[macro_export]
|
||||
macro_rules! view_navigation {
|
||||
($stack:expr, $($e:ident), *) => {
|
||||
match &$stack.last().unwrap() {
|
||||
match &$stack.last().expect("Unexpected stack state") {
|
||||
$(
|
||||
$e(s) => s.view().map(RootMessage::$e),
|
||||
)*
|
||||
@@ -320,7 +326,7 @@ macro_rules! state_update {
|
||||
match $message {
|
||||
$(
|
||||
RootMessage::$e(msg) => {
|
||||
if let $e(s) = $stack.last_mut().unwrap() {
|
||||
if let $e(s) = $stack.last_mut().expect("Unexpected stack state") {
|
||||
message_navigation!(msg, $stack, s)
|
||||
}
|
||||
else {
|
||||
@@ -355,7 +361,7 @@ macro_rules! message_navigation {
|
||||
#[macro_export]
|
||||
macro_rules! state_navigate_handle {
|
||||
($stack:expr, $($e:ident), *) => {
|
||||
match $stack.last_mut().unwrap() {
|
||||
match $stack.last_mut().expect("Unexpected stack state") {
|
||||
$(
|
||||
Page::$e(page) => {
|
||||
page.navigated();
|
||||
|
||||
Reference in new issue
Block a user