pub mod types;
use std::collections::HashSet;
use types::ModuleFile;
#[derive(Debug, serde::Deserialize)]
struct ModuleIndex {
modules: Vec<String>,
}
const MODULES_TOML: &str = include_str!("../../content/modules.toml");
const GREP_TOML: &str = include_str!("../../content/grep.toml");
const AWK_TOML: &str = include_str!("../../content/awk.toml");
const SED_TOML: &str = include_str!("../../content/sed.toml");
const FIND_TOML: &str = include_str!("../../content/find.toml");
const XARGS_TOML: &str = include_str!("../../content/xargs.toml");
const CUT_TOML: &str = include_str!("../../content/cut.toml");
const SORT_TOML: &str = include_str!("../../content/sort.toml");
const UNIQ_TOML: &str = include_str!("../../content/uniq.toml");
const TR_TOML: &str = include_str!("../../content/tr.toml");
fn raw_by_name(name: &str) -> Option<&'static str> {
match name {
"grep" => Some(GREP_TOML),
"awk" => Some(AWK_TOML),
"sed" => Some(SED_TOML),
"find" => Some(FIND_TOML),
"xargs" => Some(XARGS_TOML),
"cut" => Some(CUT_TOML),
"sort" => Some(SORT_TOML),
"uniq" => Some(UNIQ_TOML),
"tr" => Some(TR_TOML),
_ => None,
}
}
pub fn load_modules() -> Vec<ModuleFile> {
let index: ModuleIndex =
toml::from_str(MODULES_TOML).expect("content/modules.toml failed to parse");
let mut modules = Vec::with_capacity(index.modules.len());
for name in &index.modules {
let raw =
raw_by_name(name).unwrap_or_else(|| panic!("No embedded TOML for module '{name}'"));
let module: ModuleFile = toml::from_str(raw)
.unwrap_or_else(|e| panic!("Failed to parse content/{name}.toml: {e}"));
modules.push(module);
}
let mut seen_ids: HashSet<String> = HashSet::new();
for m in &modules {
for ex in &m.exercises {
if !seen_ids.insert(ex.id.clone()) {
panic!("Duplicate exercise ID '{}' found", ex.id);
}
}
}
for m in &modules {
for ex in &m.exercises {
if ex.match_mode == types::MatchMode::Regex {
regex_compile_check(&ex.id, &ex.expected_output);
}
}
}
#[cfg(debug_assertions)]
{
let total: usize = modules.iter().map(|m| m.exercises.len()).sum();
eprintln!("Loaded {} modules, {} exercises", modules.len(), total);
for m in &modules {
eprintln!(
" {} (v{}): {} exercises",
m.module.name,
m.module.version,
m.exercises.len()
);
}
}
modules
}
fn regex_compile_check(id: &str, pattern: &str) {
if pattern.trim().is_empty() {
panic!("Exercise '{id}' has match_mode=regex but empty expected_output");
}
}