1
- use std:: { fs , path :: Path } ;
1
+ use std:: fs ;
2
2
3
3
use chrono:: Utc ;
4
4
use clap:: Parser ;
5
5
use inquire:: { Select , Text } ;
6
6
use rs_fsrs:: { Card as FSRSCard , Rating as FSRSRating , FSRS } ;
7
7
8
8
use crate :: config:: Config ;
9
- use crate :: deck:: { Deck , Log } ;
10
- use crate :: progress:: { Progress , ProgressCard , Rating } ;
9
+ use crate :: deck:: log:: LogEntry ;
10
+ use crate :: deck:: {
11
+ progress:: { Progress , ProgressCard , Rating } ,
12
+ Deck ,
13
+ } ;
11
14
use rand:: seq:: SliceRandom ;
12
15
13
16
use crate :: tui:: study:: run as tui;
@@ -58,15 +61,11 @@ fn terminal(args: StudyArgs) {
58
61
}
59
62
} ,
60
63
} ;
61
- let deck_path = Path :: new ( deck_entry. path . as_str ( ) ) ;
64
+ let deck_path = deck_entry. path ;
62
65
63
66
let deck: Deck = match fs:: read_to_string ( deck_path. join ( "deck.json" ) ) {
64
67
Ok ( contents) => match serde_json:: from_str :: < Deck > ( & contents) {
65
- Ok ( mut deck) => {
66
- deck. config = Some ( config) ;
67
-
68
- deck
69
- }
68
+ Ok ( deck) => deck,
70
69
Err ( err) => {
71
70
panic ! ( "Could not parse deck file: {}" , err) ;
72
71
}
@@ -82,7 +81,7 @@ fn terminal(args: StudyArgs) {
82
81
83
82
let progress_path = deck_path. join ( ".progress.json" ) ;
84
83
85
- let mut new_progress : Progress = match fs:: read_to_string ( & progress_path) {
84
+ let mut progress : Progress = match fs:: read_to_string ( & progress_path) {
86
85
Ok ( contents) => match serde_json:: from_str ( & contents) {
87
86
Ok ( progress) => progress,
88
87
Err ( err) => {
@@ -91,23 +90,28 @@ fn terminal(args: StudyArgs) {
91
90
} ,
92
91
Err ( err) => {
93
92
if err. kind ( ) == std:: io:: ErrorKind :: NotFound {
94
- Progress { cards : Vec :: new ( ) }
93
+ match Progress :: get ( & deck) {
94
+ Ok ( progress) => progress,
95
+ Err ( err) => {
96
+ panic ! ( "Could not get progress: {}" , err) ;
97
+ }
98
+ }
95
99
} else {
96
100
panic ! ( "Could not read progress file: {}" , err) ;
97
101
}
98
102
}
99
103
} ;
100
104
101
105
// Remove any cards that have been removed from the deck by checking ids
102
- new_progress . cards . retain ( |progress_card| {
106
+ progress . cards . retain ( |progress_card| {
103
107
deck. cards
104
108
. iter ( )
105
109
. find ( |card| card. id == progress_card. id )
106
110
. is_some ( )
107
111
} ) ;
108
112
109
113
// Get cards from progress whose due date is before now
110
- let mut cards = new_progress
114
+ let mut cards = progress
111
115
. cards
112
116
. iter ( )
113
117
. filter ( |progress_card| progress_card. fsrs . due . clone ( ) < now)
@@ -116,7 +120,7 @@ fn terminal(args: StudyArgs) {
116
120
117
121
// Add any new cards to the progress file
118
122
for card in deck. cards . iter ( ) {
119
- if new_progress
123
+ if progress
120
124
. cards
121
125
. iter ( )
122
126
. find ( |progress_card| progress_card. id == card. id )
@@ -127,12 +131,12 @@ fn terminal(args: StudyArgs) {
127
131
fsrs : FSRSCard :: default ( ) ,
128
132
} ;
129
133
130
- new_progress . cards . push ( progress_card. clone ( ) ) ;
134
+ progress . cards . push ( progress_card. clone ( ) ) ;
131
135
cards. push ( progress_card) ;
132
136
}
133
137
}
134
138
135
- let progress_json = serde_json:: to_string_pretty ( & new_progress ) . unwrap ( ) ;
139
+ let progress_json = serde_json:: to_string_pretty ( & progress ) . unwrap ( ) ;
136
140
fs:: write ( & progress_path, progress_json) . unwrap ( ) ;
137
141
138
142
if cards. is_empty ( ) {
@@ -176,22 +180,27 @@ fn terminal(args: StudyArgs) {
176
180
let new_schedule = schedules[ & FSRSRating :: from ( rating) ] . clone ( ) ;
177
181
178
182
// Update card in progress
179
- new_progress
183
+ progress
180
184
. cards
181
185
. iter_mut ( )
182
186
. find ( |progress_card| progress_card. id == card. id )
183
187
. unwrap ( )
184
188
. fsrs = new_schedule. card ;
185
189
186
- let progress_json = serde_json:: to_string_pretty ( & new_progress) . unwrap ( ) ;
187
- fs:: write ( & progress_path, progress_json) . unwrap ( ) ;
190
+ progress. save ( ) ;
191
+
192
+ let mut log = match deck. log ( ) {
193
+ Ok ( log) => log,
194
+ Err ( err) => {
195
+ panic ! ( "Could not get log: {}" , err) ;
196
+ }
197
+ } ;
198
+
199
+ log. entries . push ( LogEntry {
200
+ last_card : card. clone ( ) ,
201
+ log : new_schedule. review_log ,
202
+ } ) ;
188
203
189
- deck. add_log (
190
- deck. id . clone ( ) ,
191
- Log {
192
- last_card : card. clone ( ) ,
193
- log : new_schedule. review_log ,
194
- } ,
195
- ) ;
204
+ log. save ( ) ;
196
205
}
197
206
}
0 commit comments