summaryrefslogtreecommitdiff
path: root/src/state.rs
blob: e3e3299050b55acf2708f4bc64ec4e503f1636c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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(())
    }
}