1pub mod types;
2
3use std::collections::HashSet;
4use types::ModuleFile;
5
6#[derive(Debug, serde::Deserialize)]
7struct ModuleIndex {
8 modules: Vec<String>,
9}
10
11const MODULES_TOML: &str = include_str!("../../content/modules.toml");
13const GREP_TOML: &str = include_str!("../../content/grep.toml");
14const AWK_TOML: &str = include_str!("../../content/awk.toml");
15const SED_TOML: &str = include_str!("../../content/sed.toml");
16const FIND_TOML: &str = include_str!("../../content/find.toml");
17const XARGS_TOML: &str = include_str!("../../content/xargs.toml");
18const CUT_TOML: &str = include_str!("../../content/cut.toml");
19const SORT_TOML: &str = include_str!("../../content/sort.toml");
20const UNIQ_TOML: &str = include_str!("../../content/uniq.toml");
21const TR_TOML: &str = include_str!("../../content/tr.toml");
22const WC_TOML: &str = include_str!("../../content/wc.toml");
24const TAR_TOML: &str = include_str!("../../content/tar.toml");
25const CHMOD_TOML: &str = include_str!("../../content/chmod.toml");
26
27fn raw_by_name(name: &str) -> Option<&'static str> {
28 match name {
29 "grep" => Some(GREP_TOML),
30 "awk" => Some(AWK_TOML),
31 "sed" => Some(SED_TOML),
32 "find" => Some(FIND_TOML),
33 "xargs" => Some(XARGS_TOML),
34 "cut" => Some(CUT_TOML),
35 "sort" => Some(SORT_TOML),
36 "uniq" => Some(UNIQ_TOML),
37 "tr" => Some(TR_TOML),
38 "wc" => Some(WC_TOML),
39 "tar" => Some(TAR_TOML),
40 "chmod" => Some(CHMOD_TOML),
41 _ => None,
42 }
43}
44
45pub fn load_modules() -> Vec<ModuleFile> {
47 let index: ModuleIndex =
49 toml::from_str(MODULES_TOML).expect("content/modules.toml failed to parse");
50
51 let mut modules = Vec::with_capacity(index.modules.len());
52 for name in &index.modules {
53 let raw =
54 raw_by_name(name).unwrap_or_else(|| panic!("No embedded TOML for module '{name}'"));
55 let module: ModuleFile = toml::from_str(raw)
57 .unwrap_or_else(|e| panic!("Failed to parse content/{name}.toml: {e}"));
58 modules.push(module);
59 }
60
61 let mut seen_ids: HashSet<String> = HashSet::new();
63 for m in &modules {
64 for ex in &m.exercises {
65 if !seen_ids.insert(ex.id.clone()) {
66 panic!("Duplicate exercise ID '{}' found", ex.id);
67 }
68 }
69 }
70
71 for m in &modules {
73 for ex in &m.exercises {
74 if ex.match_mode == types::MatchMode::Regex {
75 regex_compile_check(&ex.id, &ex.expected_output);
76 }
77 }
78 }
79
80 #[cfg(debug_assertions)]
82 {
83 let total: usize = modules.iter().map(|m| m.exercises.len()).sum();
84 eprintln!("Loaded {} modules, {} exercises", modules.len(), total);
85 for m in &modules {
86 eprintln!(
87 " {} (v{}): {} exercises",
88 m.module.name,
89 m.module.version,
90 m.exercises.len()
91 );
92 }
93 }
94
95 modules
96}
97
98fn regex_compile_check(id: &str, pattern: &str) {
99 if pattern.trim().is_empty() {
104 panic!("Exercise '{id}' has match_mode=regex but empty expected_output");
105 }
106}