diff options
| author | mo8it <mo8it@proton.me> | 2024-04-07 04:59:22 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-04-07 04:59:22 +0200 |
| commit | b0a475062445705853b4f861ee9e3135065f0660 (patch) | |
| tree | b7782712119ec7865a056c3626b52decb1f6b2fd /src/state.rs | |
| parent | 4f69285375342951da36346f1a1b93f7903a362f (diff) | |
Implement "continue at"
Diffstat (limited to 'src/state.rs')
| -rw-r--r-- | src/state.rs | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/state.rs b/src/state.rs index f29dc13..5a64487 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, Result}; +use anyhow::{bail, Context, Result}; use serde::{Deserialize, Serialize}; use std::fs; @@ -6,8 +6,8 @@ use crate::exercise::Exercise; #[derive(Serialize, Deserialize)] pub struct State { - pub next_exercise_ind: usize, - pub progress: Vec<bool>, + next_exercise_ind: usize, + progress: Vec<bool>, } impl State { @@ -30,11 +30,30 @@ impl State { }) } - pub fn write(&self) -> Result<()> { + fn write(&self) -> Result<()> { // TODO: Capacity - let mut buf = Vec::with_capacity(1 << 12); + let mut buf = Vec::with_capacity(1024); serde_json::ser::to_writer(&mut buf, self).context("Failed to serialize the state")?; - dbg!(buf.len()); + Ok(()) } + + #[inline] + pub fn next_exercise_ind(&self) -> usize { + self.next_exercise_ind + } + + pub fn set_next_exercise_ind(&mut self, ind: usize) -> Result<()> { + if ind >= self.progress.len() { + bail!("The next exercise index is higher than the number of exercises"); + } + + self.next_exercise_ind = ind; + self.write() + } + + #[inline] + pub fn progress(&self) -> &[bool] { + &self.progress + } } |
