use crate::exercise::Exercise;
use anyhow::{Context, Result};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
const STATE_FILE: &str = ".cmetal-state.txt";
const LEGACY_STATE_FILE: &str = ".clings-state.txt";
const MAX_DAMAGED_BACKUPS: u32 = 100;
static DAMAGED_PREFIX: LazyLock<String> = LazyLock::new(|| format!("{STATE_FILE}.damaged"));
#[derive(PartialEq, Eq)]
enum ProgressFile {
Loaded,
Unreadable,
Preserved,
}
pub struct AppState {
state_path: PathBuf,
pub exercises: Vec<Exercise>,
done: HashSet<String>,
pub current_index: usize,
progress_file: ProgressFile,
}
impl AppState {
pub fn new(exercises: Vec<Exercise>, base_dir: &Path, mut warn: impl FnMut(&str)) -> Self {
let state_path = base_dir.join(STATE_FILE);
let legacy = base_dir.join(LEGACY_STATE_FILE);
if !state_path.exists() && legacy.exists() {
let _ = std::fs::rename(&legacy, &state_path);
}
let mut state = Self {
state_path,
exercises,
done: HashSet::new(),
current_index: 0,
progress_file: ProgressFile::Loaded,
};
state.load(&mut warn);
state
}
fn load(&mut self, warn: &mut impl FnMut(&str)) {
if !self.state_path.exists() {
return;
}
let content = match std::fs::read_to_string(&self.state_path) {
Ok(content) => content,
Err(e) => {
self.progress_file = ProgressFile::Unreadable;
warn(&format!(
"Could not read {} ({e}) — starting from empty progress. \
The file is kept: delete it yourself, or it is moved \
aside as a .damaged copy the first time progress is \
saved.",
self.state_path.display()
));
return;
}
};
let mut lines = content.lines();
lines.next();
lines.next();
if let Some(current_name) = lines.next() {
let current_name = current_name.trim();
if !current_name.is_empty() {
if let Some(idx) = self.exercises.iter().position(|e| e.name() == current_name) {
self.current_index = idx;
}
}
}
lines.next();
for line in lines {
let name = line.trim();
if !name.is_empty() {
self.done.insert(name.to_string());
}
}
}
fn preserve_if_unreadable(&mut self) -> Result<()> {
if self.progress_file != ProgressFile::Unreadable {
return Ok(());
}
let kept = self.free_backup_path().with_context(|| {
format!(
"Refusing to destroy {}: it could not be read and there is no \
free .damaged name left to move it to. Remove some of them, \
or delete the file yourself.",
self.state_path.display()
)
})?;
std::fs::rename(&self.state_path, &kept).with_context(|| {
format!(
"Refusing to destroy {}: it could not be read and could not be \
moved to {}.",
self.state_path.display(),
kept.display()
)
})?;
self.progress_file = ProgressFile::Preserved;
Ok(())
}
fn free_backup_path(&self) -> Option<PathBuf> {
let dir = self.state_path.parent()?;
let prefix = &*DAMAGED_PREFIX;
std::iter::once(dir.join(prefix))
.chain((1..MAX_DAMAGED_BACKUPS).map(|n| dir.join(format!("{prefix}.{n}"))))
.find(|candidate| !candidate.exists())
}
pub fn damaged_backups(&self) -> Vec<PathBuf> {
let Some(dir) = self.state_path.parent() else {
return Vec::new();
};
let prefix = &*DAMAGED_PREFIX;
let Ok(entries) = std::fs::read_dir(dir) else {
return Vec::new();
};
let mut found: Vec<(u32, PathBuf)> = entries
.flatten()
.filter_map(|entry| {
let path = entry.path();
let name = path.file_name()?.to_str()?.to_owned();
let suffix = name.strip_prefix(prefix.as_str())?;
let n = match suffix {
"" => 0,
rest => rest.strip_prefix('.')?.parse().ok()?,
};
Some((n, path))
})
.collect();
found.sort();
found.into_iter().map(|(_, path)| path).collect()
}
pub fn save(&mut self) -> Result<()> {
self.preserve_if_unreadable()?;
let current_name = self
.exercises
.get(self.current_index)
.map(|e| e.name())
.unwrap_or("");
let mut content = String::new();
content.push_str("DON'T EDIT THIS FILE!\n");
content.push('\n');
content.push_str(current_name);
content.push('\n');
content.push('\n');
for name in &self.done {
content.push_str(name);
content.push('\n');
}
std::fs::write(&self.state_path, content).with_context(|| "Failed to write state file")?;
Ok(())
}
pub fn current_exercise(&self) -> Option<&Exercise> {
self.exercises.get(self.current_index)
}
pub fn is_done(&self, name: &str) -> bool {
self.done.contains(name)
}
pub fn mark_done(&mut self, name: &str) {
self.done.insert(name.to_string());
}
pub fn mark_pending(&mut self, name: &str) {
self.done.remove(name);
}
pub fn complete(&mut self, name: &str) -> Result<()> {
self.mark_done(name);
self.save()
}
pub fn prev(&mut self) -> bool {
if self.current_index > 0 {
self.current_index -= 1;
true
} else {
false
}
}
pub fn next_pending(&mut self) -> bool {
let start = self.current_index;
let len = self.exercises.len();
for i in 1..=len {
let idx = (start + i) % len;
let ex = &self.exercises[idx];
if ex.supported && !self.done.contains(ex.name()) {
self.current_index = idx;
return true;
}
}
false
}
pub fn all_done(&self) -> bool {
self.exercises
.iter()
.filter(|e| e.supported)
.all(|e| self.done.contains(e.name()))
}
pub fn progress(&self) -> (usize, usize) {
let supported: Vec<_> = self.exercises.iter().filter(|e| e.supported).collect();
let done = supported
.iter()
.filter(|e| self.done.contains(e.name()))
.count();
(done, supported.len())
}
fn find_exercise(&self, name: &str) -> Option<usize> {
self.exercises.iter().position(|e| e.name() == name)
}
pub fn resolve(&self, name: Option<String>) -> Result<usize> {
let name = name.unwrap_or_else(|| {
self.current_exercise()
.map(|e| e.name().to_string())
.unwrap_or_default()
});
self.find_exercise(&name)
.with_context(|| crate::errors::exercise_not_found(&name))
}
#[cfg(test)]
pub(crate) fn from_parts(exercises: Vec<Exercise>, base_dir: &Path) -> Self {
Self {
state_path: base_dir.join(STATE_FILE),
exercises,
done: HashSet::new(),
current_index: 0,
progress_file: ProgressFile::Loaded,
}
}
pub fn reset(&mut self) -> Result<()> {
if self.state_path.exists() {
if self.progress_file == ProgressFile::Unreadable {
self.preserve_if_unreadable()?;
} else {
std::fs::remove_file(&self.state_path)
.with_context(|| "Failed to remove state file")?;
}
}
self.done.clear();
self.current_index = 0;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::info_file::ExerciseInfo;
fn make_info(name: &str) -> ExerciseInfo {
ExerciseInfo {
name: name.to_string(),
dir: "test".to_string(),
test: false,
sanitizers: false,
flags: Vec::new(),
compilers: None,
hint: None,
hints: None,
}
}
fn make_exercise(name: &str) -> Exercise {
let info = make_info(name);
Exercise {
path: PathBuf::from(format!("/tmp/{name}.c")),
solution_path: PathBuf::from(format!("/tmp/{name}_sol.c")),
reveal_path: PathBuf::from(format!("/tmp/{name}_revealed.c")),
supported: true,
info,
}
}
fn make_unsupported_exercise(name: &str) -> Exercise {
let mut ex = make_exercise(name);
ex.supported = false;
ex
}
fn make_state(names: &[&str], base_dir: &Path) -> AppState {
let exercises: Vec<Exercise> = names.iter().map(|n| make_exercise(n)).collect();
AppState::from_parts(exercises, base_dir)
}
#[test]
fn unsupported_exercises_skipped_and_uncounted() {
let tmp = tempfile::TempDir::new().unwrap();
let mut state = make_state(&["a", "c"], tmp.path());
state
.exercises
.insert(1, make_unsupported_exercise("b_gcc_only"));
assert_eq!(state.progress(), (0, 2));
state.current_index = 0;
assert!(state.next_pending());
assert_eq!(state.current_exercise().unwrap().name(), "c");
state.mark_done("a");
state.mark_done("c");
assert!(state.all_done());
assert_eq!(state.progress(), (2, 2));
}
#[test]
fn prev_at_zero_returns_false() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b", "c"], tmp.path());
assert!(!state.prev());
assert_eq!(state.current_index, 0);
}
#[test]
fn prev_moves_back() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b", "c", "d"], tmp.path());
state.current_index = 3;
assert!(state.prev());
assert_eq!(state.current_index, 2);
}
#[test]
fn next_pending_skips_done() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b", "c"], tmp.path());
state.mark_done("b");
assert!(state.next_pending());
assert_eq!(state.current_index, 2);
}
#[test]
fn next_pending_wraps_around() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b", "c"], tmp.path());
state.current_index = 2;
state.mark_done("a");
state.mark_done("c");
assert!(state.next_pending());
assert_eq!(state.current_index, 1);
}
#[test]
fn next_pending_all_done_returns_false() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b"], tmp.path());
state.mark_done("a");
state.mark_done("b");
assert!(!state.next_pending());
}
#[test]
fn mark_done_and_is_done() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b"], tmp.path());
assert!(!state.is_done("a"));
state.mark_done("a");
assert!(state.is_done("a"));
assert!(!state.is_done("b"));
}
#[test]
fn all_done_false_then_true() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b"], tmp.path());
assert!(!state.all_done());
state.mark_done("a");
assert!(!state.all_done());
state.mark_done("b");
assert!(state.all_done());
}
#[test]
fn progress_counts() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b", "c"], tmp.path());
assert_eq!(state.progress(), (0, 3));
state.mark_done("a");
assert_eq!(state.progress(), (1, 3));
state.mark_done("c");
assert_eq!(state.progress(), (2, 3));
}
#[test]
fn find_exercise_found_and_not_found() {
let tmp = tempfile::tempdir().unwrap();
let state = make_state(&["alpha", "beta"], tmp.path());
assert_eq!(state.find_exercise("beta"), Some(1));
assert_eq!(state.find_exercise("gamma"), None);
}
#[test]
fn current_exercise_valid() {
let tmp = tempfile::tempdir().unwrap();
let state = make_state(&["a", "b"], tmp.path());
assert_eq!(state.current_exercise().unwrap().name(), "a");
}
#[test]
fn save_and_reload_preserves_state() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b", "c"], tmp.path());
state.current_index = 1;
state.mark_done("a");
state.save().unwrap();
let mut warnings = Vec::new();
let state2 = AppState::new(
vec![make_exercise("a"), make_exercise("b"), make_exercise("c")],
tmp.path(),
|w| warnings.push(w.to_string()),
);
assert!(warnings.is_empty(), "a healthy file warns nothing");
assert_eq!(state2.current_index, 1);
assert!(state2.is_done("a"));
assert!(!state2.is_done("b"));
}
#[test]
fn reset_removes_state_file() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a"], tmp.path());
state.mark_done("a");
state.save().unwrap();
assert!(tmp.path().join(STATE_FILE).exists());
state.reset().unwrap();
assert!(!tmp.path().join(STATE_FILE).exists());
}
#[test]
fn reset_no_file_is_ok() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a"], tmp.path());
assert!(state.reset().is_ok());
}
fn write_damaged(dir: &Path) {
std::fs::write(dir.join(STATE_FILE), [0xff, 0xfe, 0x00, b'x']).unwrap();
}
fn damaged_copies(state: &AppState) -> Vec<String> {
state
.damaged_backups()
.iter()
.map(|p| p.file_name().unwrap().to_string_lossy().to_string())
.collect()
}
#[test]
fn the_damaged_file_warning_recommends_nothing_destructive() {
let tmp = tempfile::tempdir().unwrap();
write_damaged(tmp.path());
let mut warnings = Vec::new();
let _ = AppState::new(vec![make_exercise("a")], tmp.path(), |w| {
warnings.push(w.to_string())
});
assert_eq!(warnings.len(), 1);
assert!(
!warnings[0].contains("cmetal "),
"the warning must not send the learner to run anything: {}",
warnings[0]
);
}
#[test]
fn repeated_saves_preserve_only_once() {
let tmp = tempfile::tempdir().unwrap();
write_damaged(tmp.path());
let mut state = AppState::new(vec![make_exercise("a")], tmp.path(), |_| {});
state.save().unwrap();
state.save().unwrap();
state.save().unwrap();
assert_eq!(
damaged_copies(&state),
vec![".cmetal-state.txt.damaged".to_string()],
"only the unreadable file belongs in a .damaged copy"
);
}
#[test]
fn loading_leaves_the_damaged_file_in_place() {
let tmp = tempfile::tempdir().unwrap();
write_damaged(tmp.path());
let mut warnings = Vec::new();
let state = AppState::new(vec![make_exercise("a")], tmp.path(), |w| {
warnings.push(w.to_string())
});
assert_eq!(warnings.len(), 1, "the learner must be told");
assert!(
warnings[0].contains("delete it"),
"the warning must say how to clear it: {}",
warnings[0]
);
assert!(damaged_copies(&state).is_empty());
assert!(tmp.path().join(STATE_FILE).exists());
}
#[test]
fn each_corruption_gets_its_own_backup() {
let tmp = tempfile::tempdir().unwrap();
let mut state = None;
for _ in 0..2 {
write_damaged(tmp.path());
let mut fresh = AppState::new(vec![make_exercise("a")], tmp.path(), |_| {});
fresh.save().unwrap();
state = Some(fresh);
}
assert_eq!(
damaged_copies(state.as_ref().unwrap()),
vec![
".cmetal-state.txt.damaged".to_string(),
".cmetal-state.txt.damaged.1".to_string()
]
);
}
#[test]
fn reset_moves_an_unreadable_file_aside() {
let tmp = tempfile::tempdir().unwrap();
write_damaged(tmp.path());
let mut state = AppState::new(vec![make_exercise("a")], tmp.path(), |_| {});
state.reset().unwrap();
assert!(!tmp.path().join(STATE_FILE).exists());
assert_eq!(
damaged_copies(&state),
vec![".cmetal-state.txt.damaged".to_string()]
);
}
#[test]
fn reset_clears_progress_in_memory() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b"], tmp.path());
state.mark_done("a");
state.current_index = 1;
state.save().unwrap();
state.reset().unwrap();
assert!(!state.is_done("a"), "progress must not survive a reset");
assert_eq!(state.current_index, 0);
assert_eq!(state.progress(), (0, 2));
state.save().unwrap();
let written = std::fs::read_to_string(tmp.path().join(STATE_FILE)).unwrap();
let completions: Vec<&str> = written
.lines()
.skip(4)
.map(str::trim)
.filter(|l| !l.is_empty())
.collect();
assert!(
completions.is_empty(),
"a save after reset wrote back the cleared progress: {completions:?}"
);
}
#[test]
fn resolve_defaults_to_the_current_exercise() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b", "c"], tmp.path());
state.current_index = 2;
assert_eq!(state.resolve(None).unwrap(), 2);
}
#[test]
fn resolve_finds_by_name_regardless_of_current() {
let tmp = tempfile::tempdir().unwrap();
let mut state = make_state(&["a", "b", "c"], tmp.path());
state.current_index = 0;
assert_eq!(state.resolve(Some("b".to_string())).unwrap(), 1);
}
#[test]
fn resolve_reports_the_missing_name() {
let tmp = tempfile::tempdir().unwrap();
let state = make_state(&["a"], tmp.path());
let err = state.resolve(Some("nope".to_string())).unwrap_err();
assert_eq!(err.to_string(), "Exercise 'nope' not found");
}
}