Skip to main content

cli_tutor/content/
mod.rs

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
11// command_modules.LOADING.1 — all TOML files embedded at compile time
12const 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");
22// more_modules.CONTENT.1 — new modules
23const 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
45// command_modules.LOADING.2-5, VALIDATION.1-4
46pub fn load_modules() -> Vec<ModuleFile> {
47    // command_modules.LOADING.5 — order from modules.toml
48    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        // command_modules.LOADING.3 — panic on bad TOML with clear message
56        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    // command_modules.VALIDATION.1 — duplicate exercise IDs cause panic
62    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    // command_modules.VALIDATION.3 — compile regex match_mode exercises at startup
72    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    // command_modules.VALIDATION.4 — log total count in debug builds
81    #[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    // Basic regex validity check using std — real regex matching in matcher.rs
100    // We use a simple heuristic: attempt to detect obviously invalid patterns.
101    // For full validation, matcher.rs will use its own regex engine if added later.
102    // For now we just ensure the pattern is non-empty.
103    if pattern.trim().is_empty() {
104        panic!("Exercise '{id}' has match_mode=regex but empty expected_output");
105    }
106}