summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-05 03:05:07 +0200
committermo8it <mo8it@proton.me>2024-04-05 03:05:07 +0200
commit3f2d41de9ecd174ff2b099d3000bf7eca781779d (patch)
tree15d0c9374d2eeacf41cb479d3f2849efed1cfc6d
parentb0f19fd862d659d2d4b01f2faa6b006fe2c60561 (diff)
Start with the state
-rw-r--r--src/main.rs1
-rw-r--r--src/state.rs32
2 files changed, 33 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 47afd01..5051785 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,6 +16,7 @@ mod embedded;
mod exercise;
mod init;
mod run;
+mod state;
mod tui;
mod verify;
diff --git a/src/state.rs b/src/state.rs
new file mode 100644
index 0000000..e3e3299
--- /dev/null
+++ b/src/state.rs
@@ -0,0 +1,32 @@
+use anyhow::{Context, Result};
+use serde::{Deserialize, Serialize};
+use std::{fs, io, path::PathBuf};
+
+#[derive(Serialize, Deserialize)]
+pub struct ExerciseState {
+ pub path: PathBuf,
+ pub done: bool,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct State {
+ pub progress: Vec<ExerciseState>,
+}
+
+impl State {
+ pub fn read() -> Result<Self> {
+ let file_content =
+ fs::read(".rustlings.json").context("Failed to read the file `.rustlings.json`")?;
+
+ serde_json::de::from_slice(&file_content)
+ .context("Failed to deserialize the file `.rustlings.json`")
+ }
+
+ pub fn write(&self) -> io::Result<()> {
+ // TODO: Capacity
+ let mut buf = Vec::with_capacity(1 << 12);
+ serde_json::ser::to_writer(&mut buf, self).context("Failed to serialize the state");
+ dbg!(buf.len());
+ Ok(())
+ }
+}