summaryrefslogtreecommitdiff
path: root/src/state_file.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-11 02:51:02 +0200
committermo8it <mo8it@proton.me>2024-04-11 02:51:02 +0200
commitfa1f239a702eb2c0b7e0115e986481156961bbc8 (patch)
tree08ac7c8638546f80ac650474dfe4126103a15e54 /src/state_file.rs
parent4bb6bda9f6416e30233342e73fc9a8486faa3f98 (diff)
Remove "I AM NOT DONE" and the verify mode and add AppState
Diffstat (limited to 'src/state_file.rs')
-rw-r--r--src/state_file.rs68
1 files changed, 0 insertions, 68 deletions
diff --git a/src/state_file.rs b/src/state_file.rs
deleted file mode 100644
index 6b80354..0000000
--- a/src/state_file.rs
+++ /dev/null
@@ -1,68 +0,0 @@
-use anyhow::{bail, Context, Result};
-use serde::{Deserialize, Serialize};
-use std::fs;
-
-use crate::exercise::Exercise;
-
-#[derive(Serialize, Deserialize)]
-#[serde(deny_unknown_fields)]
-pub struct StateFile {
- next_exercise_ind: usize,
- progress: Vec<bool>,
-}
-
-const BAD_INDEX_ERR: &str = "The next exercise index is higher than the number of exercises";
-
-impl StateFile {
- fn read(exercises: &[Exercise]) -> Option<Self> {
- let file_content = fs::read(".rustlings-state.json").ok()?;
-
- let slf: Self = serde_json::de::from_slice(&file_content).ok()?;
-
- if slf.progress.len() != exercises.len() || slf.next_exercise_ind >= exercises.len() {
- return None;
- }
-
- Some(slf)
- }
-
- pub fn read_or_default(exercises: &[Exercise]) -> Self {
- Self::read(exercises).unwrap_or_else(|| Self {
- next_exercise_ind: 0,
- progress: vec![false; exercises.len()],
- })
- }
-
- fn write(&self) -> Result<()> {
- let mut buf = Vec::with_capacity(1024);
- serde_json::ser::to_writer(&mut buf, self).context("Failed to serialize the state")?;
- fs::write(".rustlings-state.json", buf)
- .context("Failed to write the state file `.rustlings-state.json`")?;
-
- 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!(BAD_INDEX_ERR);
- }
- self.next_exercise_ind = ind;
- self.write()
- }
-
- #[inline]
- pub fn progress(&self) -> &[bool] {
- &self.progress
- }
-
- pub fn reset(&mut self, ind: usize) -> Result<()> {
- let done = self.progress.get_mut(ind).context(BAD_INDEX_ERR)?;
- *done = false;
- self.write()
- }
-}