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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
mod app_state;
mod commands;
mod compiler;
mod errors;
mod exercise;
mod info_file;
mod runner;
mod solutions;
mod term;
mod view;
mod watch;
mod workspace;
use anyhow::{Context, Result};
use app_state::AppState;
use clap::{Parser, Subcommand};
use compiler::{Compiler, CompilerKind};
use info_file::InfoFile;
use std::path::PathBuf;
#[derive(Parser)]
#[command(
name = "cmetal",
version,
about = "Small exercises to learn advanced C concepts"
)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
/// Compiler to use (gcc or clang)
#[arg(long, global = true, default_value = "gcc")]
compiler: String,
}
#[derive(Subcommand)]
enum Commands {
/// Create a self-contained cmetal workspace (no git clone needed)
Init {
/// Directory to create the workspace in (default: ./cmetal-workspace)
dir: Option<PathBuf>,
},
/// Run a specific exercise
Run {
/// Exercise name
name: Option<String>,
},
/// Show hint for an exercise (use --level N to reveal up to hint N)
Hint {
/// Exercise name (defaults to current)
name: Option<String>,
/// Hint level to show (1 = first hint, 2 = first two, etc.)
#[arg(short, long, default_value = "1")]
level: usize,
},
/// Reveal the official solution (only after the exercise passes)
Solution {
/// Exercise name (defaults to current)
name: Option<String>,
},
/// List all exercises and their status
List,
/// Verify all exercises
Verify,
/// Reset progress and discard your edits, restoring pristine exercises
Reset {
/// Exercise name: restore only its working copy (progress kept)
name: Option<String>,
/// Skip the confirmation prompt
#[arg(long)]
force: bool,
},
/// Update the workspace to the curriculum embedded in this binary
Update,
/// Show how your working copy differs from the pristine exercise
Diff {
/// Exercise name (defaults to current)
name: Option<String>,
},
}
fn main() -> Result<()> {
let cli = Cli::parse();
let compiler_kind = match cli.compiler.to_lowercase().as_str() {
"gcc" => CompilerKind::Gcc,
"clang" => CompilerKind::Clang,
other => anyhow::bail!("Unknown compiler: {other}. Use 'gcc' or 'clang'."),
};
// File-only commands are dispatched before the engine spins up:
// they must not require a C compiler, a populated workspace — or,
// for init, any project at all. Their match arms below are
// unreachable by construction.
match &cli.command {
// Creates a workspace: works OUTSIDE any existing project.
Some(Commands::Init { dir }) => return workspace::init(dir.clone()),
// Reconciles files; must run even in a workspace whose
// info.toml sits in .cmetal/backup after an interrupted update,
// which is why it resolves the workspace by its own marker.
Some(Commands::Update) => {
let base_dir = workspace::resolve_workspace_dir()?;
return workspace::update(&base_dir);
}
Some(Commands::Diff { name }) => {
let base_dir = workspace::resolve_base_dir()?;
return commands::diff(&base_dir, name.clone(), compiler_kind);
}
Some(Commands::Reset {
name: Some(name),
force,
}) => {
let base_dir = workspace::resolve_base_dir()?;
return commands::reset_one(&base_dir, name.clone(), compiler_kind, *force);
}
_ => {}
}
// Everything below needs the exercise list, the materialized
// workspace and the learner's progress. Materializing is idempotent
// and is what every engine command has always done on startup —
// `cmetal list` is how a learner first gets their working copies.
let base_dir = workspace::resolve_base_dir()
.context("Could not find cmetal project. Make sure you're inside the cmetal directory.")?;
let info = InfoFile::parse(&base_dir.join("info.toml"))?;
let work_dir = workspace::prepare_workspace(&info, &base_dir)?;
let exercises = workspace::load_exercises(&info, &base_dir, &work_dir, compiler_kind);
let build_dir = base_dir.join("target").join("cmetal");
let mut state = AppState::new(exercises, &base_dir, term::warn_stderr);
// The C toolchain is NOT part of that: `list`, `hint` and `reset`
// compile nothing, and probing for a compiler they will never use
// would lock out a machine that has none. Only the arms below that
// actually compile call this. Which exercise supports which
// compiler is decided from `compiler_kind` alone.
let compiler = || Compiler::new(compiler_kind, &base_dir);
match cli.command {
// Dispatched before engine setup; see the match at the top of main.
Some(Commands::Init { .. })
| Some(Commands::Update)
| Some(Commands::Diff { .. })
| Some(Commands::Reset { name: Some(_), .. }) => {
unreachable!("file-only commands are dispatched before engine setup")
}
None => watch::run_watch(
&mut state,
&compiler()?,
&work_dir,
&build_dir,
info.welcome_message.as_deref(),
)?,
Some(Commands::Run { name }) => commands::run(&mut state, &compiler()?, &build_dir, name)?,
Some(Commands::Hint { name, level }) => commands::hint(&state, name, level)?,
Some(Commands::Solution { name }) => {
commands::solution(&mut state, &compiler()?, &build_dir, name)?
}
Some(Commands::List) => commands::list(&state),
Some(Commands::Verify) => commands::verify(&mut state, &compiler()?, &build_dir)?,
Some(Commands::Reset { name: None, force }) => {
commands::reset_all(&mut state, &info, &base_dir, force)?
}
}
Ok(())
}