1
0
Fork 0
mirror of https://github.com/notohh/rustlings.git synced 2025-05-25 02:07:42 -04:00
rustlings/src/state.rs
2024-04-06 01:46:22 +02:00

38 lines
960 B
Rust

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use crate::exercise::Exercise;
#[derive(Serialize, Deserialize)]
pub struct State {
pub progress: Vec<bool>,
}
impl State {
fn read(exercises: &[Exercise]) -> Option<Self> {
let file_content = fs::read(".rustlings.json").ok()?;
let slf: Self = serde_json::de::from_slice(&file_content).ok()?;
if slf.progress.len() != exercises.len() {
return None;
}
Some(slf)
}
pub fn read_or_default(exercises: &[Exercise]) -> Self {
Self::read(exercises).unwrap_or_else(|| Self {
progress: vec![false; exercises.len()],
})
}
pub fn write(&self) -> 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(())
}
}