1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2024-11-08 09:09:17 +01:00

Start with the state

This commit is contained in:
mo8it 2024-04-05 03:05:07 +02:00
parent b0f19fd862
commit 3f2d41de9e
2 changed files with 33 additions and 0 deletions

@ -16,6 +16,7 @@ mod embedded;
mod exercise;
mod init;
mod run;
mod state;
mod tui;
mod verify;

32
src/state.rs Normal file

@ -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(())
}
}