dotzuki-cli 0.1.1

dotzuki CLI: scaffold (dotzuki new), compile-check (dotzuki check) and play (dotzuki run) zero-Rust JRPG projects
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! `dotzuki check` — compile every DSL file in a game project (in memory) and
//! report diagnostics. Exit code 0 when clean, 1 when any diagnostic fires.
//!
//! When the manifest carries a `battle` section, it is validated too: the
//! referenced table ids must exist in the data activity, the referenced
//! stat/skill fields must exist in the table schemas, an explicitly declared
//! rules file must exist on disk, and the rules file — whenever it exists —
//! must parse as a dotzuki-rules `Ruleset`. Record JSONs are not loaded — the
//! manifest's table definitions are enough.

use std::fs;
use std::path::Path;

use anyhow::{bail, Context, Result};
use dotzuki_engine_dsl::compiler::compile_dirs;
use dotzuki_runner::manifest::{Manifest, TableDef, DEFAULT_RULES_FILE};

pub fn run(dir: &Path) -> Result<()> {
    let manifest_path = dir.join(".dotzuki-editor.json");
    if !manifest_path.is_file() {
        bail!(
            "No .dotzuki-editor.json found in {} — not a jrpg game project",
            dir.display()
        );
    }
    let text = fs::read_to_string(&manifest_path)
        .with_context(|| format!("failed to read {}", manifest_path.display()))?;
    let manifest: Manifest = serde_json::from_str(&text)
        .with_context(|| format!("failed to parse {}", manifest_path.display()))?;

    let dirs = manifest.dsl_dirs(dir);
    let dir_refs: Vec<&Path> = dirs.iter().map(|d| d.as_path()).collect();
    let report = compile_dirs(&dir_refs, None);

    println!("Project: {} ({})", manifest.name, dir.display());
    println!("DSL dirs:");
    for d in &dirs {
        if d.is_dir() {
            println!("  {}", d.display());
        } else {
            println!("  {} (missing, skipped)", d.display());
        }
    }
    println!(
        "Compiled: {} scene(s), {} layout(s), {} theme(s), {} style(s) from {} file(s)",
        report.scenes.len(),
        report.ui_layouts.len(),
        report.themes.len(),
        report.styles.len(),
        report.files.len()
    );

    let battle_diags = battle_diagnostics(&manifest, dir);
    let total = report.diagnostics.len() + battle_diags.len();
    if total == 0 {
        println!("OK — no diagnostics");
        return Ok(());
    }
    for diag in &report.diagnostics {
        eprintln!("error: {}", diag);
    }
    for diag in &battle_diags {
        eprintln!("error: {}", diag);
    }
    eprintln!("check failed: {} diagnostic(s)", total);
    std::process::exit(1);
}

/// Validate the manifest's `battle` section against the data activity's table
/// definitions and the rules file. Empty when there is no battle section (or
/// the section is valid).
fn battle_diagnostics(manifest: &Manifest, root: &Path) -> Vec<String> {
    let Some(battle) = &manifest.battle else {
        return Vec::new();
    };
    let mut diags = Vec::new();

    // Referenced table ids must exist in the data activity.
    let party = battle
        .party
        .as_ref()
        .and_then(|r| resolve_table(manifest, "battle.party.table", &r.table, &mut diags));
    let enemies = battle
        .enemies
        .as_ref()
        .and_then(|r| resolve_table(manifest, "battle.enemies.table", &r.table, &mut diags));
    let skills = battle
        .skills
        .as_ref()
        .and_then(|s| resolve_table(manifest, "battle.skills.table", &s.table, &mut diags));
    let items = battle
        .items
        .as_ref()
        .and_then(|i| resolve_table(manifest, "battle.items.table", &i.table, &mut diags));
    let encounters = battle
        .encounters
        .as_ref()
        .and_then(|e| resolve_table(manifest, "battle.encounters.table", &e.table, &mut diags));

    // The mapped stat fields must exist in the combatant table schemas.
    let stats = battle.stats.clone().unwrap_or_default();
    for (what, table) in [("party", &party), ("enemies", &enemies)] {
        let Some(table) = table.as_ref() else { continue };
        for (role, field) in [
            ("hp", &stats.hp),
            ("attack", &stats.attack),
            ("defense", &stats.defense),
            ("speed", &stats.speed),
        ] {
            require_field(
                &mut diags,
                &format!("battle.stats.{role}"),
                field.as_str(),
                table,
                what,
            );
        }
        // The combatant resource field.
        if let Some(resource) = &battle.resource {
            require_field(&mut diags, "battle.resource", resource.as_str(), table, what);
        }
        // The combatant skill-list field.
        let skills_field = battle
            .skills
            .as_ref()
            .map(|s| s.field.as_str())
            .unwrap_or(dotzuki_runner::manifest::DEFAULT_SKILLS_FIELD);
        if battle.skills.is_some() {
            require_field(
                &mut diags,
                "battle.skills.field",
                skills_field,
                table,
                what,
            );
        }
    }

    // The skill-record fields must exist in the skills table schema.
    if let (Some(skill_cfg), Some(table)) = (&battle.skills, &skills) {
        require_field(
            &mut diags,
            "battle.skills.categoryField",
            skill_cfg.category_field.as_str(),
            table,
            "skills",
        );
        require_field(
            &mut diags,
            "battle.skills.costField",
            skill_cfg.cost_field.as_str(),
            table,
            "skills",
        );
    }

    // The item heal field must exist in the items table schema.
    if let (Some(item_cfg), Some(table)) = (&battle.items, &items) {
        require_field(
            &mut diags,
            "battle.items.healField",
            item_cfg.heal_field.as_str(),
            table,
            "items",
        );
    }

    // The encounters table schema must declare the `enemies` list field
    // (encounter records are `{ id, name, enemies[], trainer, money }`).
    if let Some(table) = &encounters {
        require_field(
            &mut diags,
            "battle.encounters",
            "enemies",
            table,
            "encounters",
        );
    }

    // The rules file, when the manifest names one explicitly, must exist on
    // disk — a missing file would silently leave battles with an empty type
    // chart at runtime. Whenever the file exists, it must parse as a Ruleset
    // AND compile against the closed vocabulary (unknown events/ops/stat/
    // type/resource/status names in hooks are load-time errors, never
    // mid-battle). An absent default file (battle.rules unset) is legal: such
    // projects run without a type chart.
    let rules_rel = battle.rules.as_deref().unwrap_or(DEFAULT_RULES_FILE);
    let rules_path = root.join(rules_rel.strip_prefix("./").unwrap_or(rules_rel));
    if rules_path.is_file() {
        match fs::read_to_string(&rules_path) {
            Ok(text) => {
                for d in dotzuki_runner::validate_ruleset(&text) {
                    diags.push(format!("battle.rules '{}': {d}", rules_path.display()));
                }
            }
            Err(e) => diags.push(format!("failed to read {}: {e}", rules_path.display())),
        }
    } else if battle.rules.is_some() {
        diags.push(format!(
            "battle.rules '{}' not found — battles would run with an empty type chart",
            rules_path.display()
        ));
    }

    diags
}

/// Resolve a referenced table id to its definition, recording a diagnostic
/// when the id names no declared data table.
fn resolve_table(
    manifest: &Manifest,
    key: &str,
    table_id: &str,
    diags: &mut Vec<String>,
) -> Option<TableDef> {
    let table = manifest.data_table(table_id);
    if table.is_none() {
        diags.push(format!("{key} '{table_id}' is not a declared data table"));
    }
    table
}

/// Record a diagnostic when `field` is not declared in the table schema.
fn require_field(diags: &mut Vec<String>, key: &str, field: &str, table: &TableDef, what: &str) {
    if !table.fields.iter().any(|f| f == field) {
        diags.push(format!(
            "{key} '{field}' is not a field of the {what} table '{}'",
            table.id
        ));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU32, Ordering};

    static NEXT_ID: AtomicU32 = AtomicU32::new(0);

    /// Unique temp directory, removed on drop.
    struct TestDir(std::path::PathBuf);

    impl TestDir {
        fn new(test: &str) -> Self {
            let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
            let dir = std::env::temp_dir()
                .join(format!("dotzuki-cli-check-{test}-{}-{id}", std::process::id()));
            let _ = fs::remove_dir_all(&dir);
            fs::create_dir_all(&dir).unwrap();
            TestDir(dir)
        }
    }

    impl Drop for TestDir {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.0);
        }
    }

    // Mirrors the editor's real manifest shape: field entries are objects
    // with `key`/`type` (string fields are also accepted for hand-grown
    // projects — covered by the runner's manifest unit tests).
    const TABLES: &str = r#"[
        { "id": "heroes", "dir": "heroes",
          "fields": [ {"key": "name", "type": "string"}, {"key": "hp", "type": "number"},
                      {"key": "atk", "type": "number"}, {"key": "def", "type": "number"},
                      {"key": "spd", "type": "number"}, {"key": "mp", "type": "number"},
                      {"key": "skills", "type": "array"} ] },
        { "id": "monsters", "dir": "monsters",
          "fields": [ {"key": "name"}, {"key": "hp"}, {"key": "atk"}, {"key": "def"},
                      {"key": "spd"}, {"key": "mp"}, {"key": "skills"} ] },
        { "id": "spells", "dir": "spells",
          "fields": ["name", "type", "power", "mpCost"] },
        { "id": "items", "dir": "items",
          "fields": ["name", "healHp", "effect"] },
        { "id": "encounters", "dir": "encounters",
          "fields": ["name", "enemies", "trainer", "money"] }
    ]"#;

    /// Write a project with a data activity (`TABLES`) and a battle section
    /// assembled from the given JSON fragments.
    fn write_project(
        test: &str,
        battle: serde_json::Value,
        rules_ron: Option<&str>,
    ) -> (TestDir, Manifest) {
        let tmp = TestDir::new(test);
        let root = tmp.0.join("proj");
        fs::create_dir_all(&root).unwrap();
        let manifest = serde_json::json!({
            "name": "proj",
            "dataRoot": "./data",
            "activities": [
                { "id": "data", "type": "data", "config": { "tables": serde_json::from_str::<serde_json::Value>(TABLES).unwrap() } }
            ],
            "battle": battle,
        });
        fs::write(
            root.join(".dotzuki-editor.json"),
            serde_json::to_string_pretty(&manifest).unwrap(),
        )
        .unwrap();
        if let Some(ron) = rules_ron {
            fs::create_dir_all(root.join("data")).unwrap();
            fs::write(root.join("data/rules.ron"), ron).unwrap();
        }
        let parsed: Manifest = serde_json::from_value(manifest).unwrap();
        (tmp, parsed)
    }

    fn valid_battle() -> serde_json::Value {
        serde_json::json!({
            "party": { "table": "heroes" },
            "enemies": { "table": "monsters" },
            "skills": { "table": "spells" },
            "stats": { "hp": "hp", "attack": "atk", "defense": "def", "speed": "spd" },
            "resource": "mp",
        })
    }

    #[test]
    fn valid_battle_section_passes() {
        let (tmp, manifest) = write_project(
            "valid",
            valid_battle(),
            Some("Ruleset(types: [\"fire\"], type_chart: [])"),
        );
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(diags.is_empty(), "{diags:?}");
    }

    #[test]
    fn no_battle_section_is_not_checked() {
        let tmp = TestDir::new("none");
        let manifest: Manifest = serde_json::from_value(serde_json::json!({
            "name": "proj",
            "dataRoot": "./data",
        }))
        .unwrap();
        assert!(battle_diagnostics(&manifest, tmp.0.as_path()).is_empty());
    }

    #[test]
    fn unknown_table_id_is_a_diagnostic() {
        let mut battle = valid_battle();
        battle["enemies"] = serde_json::json!({ "table": "beasts" });
        let (tmp, manifest) = write_project("badtable", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("beasts") && d.contains("battle.enemies.table")),
            "{diags:?}"
        );
    }

    #[test]
    fn unknown_stat_field_is_a_diagnostic() {
        let mut battle = valid_battle();
        battle["stats"] = serde_json::json!({ "attack": "attack" }); // tables declare "atk"
        let (tmp, manifest) = write_project("badstat", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.stats.attack") && d.contains("attack")),
            "{diags:?}"
        );
    }

    #[test]
    fn unknown_skill_cost_field_is_a_diagnostic() {
        let mut battle = valid_battle();
        battle["skills"] = serde_json::json!({ "table": "spells", "costField": "cost" });
        let (tmp, manifest) = write_project("badcost", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.skills.costField") && d.contains("cost")),
            "{diags:?}"
        );
    }

    #[test]
    fn valid_items_block_passes() {
        let mut battle = valid_battle();
        battle["items"] =
            serde_json::json!({ "table": "items", "healField": "healHp", "starting": { "potion": 3 } });
        let (tmp, manifest) = write_project("gooditems", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(diags.is_empty(), "{diags:?}");
    }

    #[test]
    fn unknown_items_table_is_a_diagnostic() {
        let mut battle = valid_battle();
        battle["items"] = serde_json::json!({ "table": "goods" });
        let (tmp, manifest) = write_project("baditemtable", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("goods") && d.contains("battle.items.table")),
            "{diags:?}"
        );
    }

    #[test]
    fn unknown_items_heal_field_is_a_diagnostic() {
        let mut battle = valid_battle();
        battle["items"] = serde_json::json!({ "table": "items", "healField": "healAmount" });
        let (tmp, manifest) = write_project("badhealfield", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.items.healField") && d.contains("healAmount")),
            "{diags:?}"
        );
    }

    #[test]
    fn valid_encounters_block_passes() {
        let mut battle = valid_battle();
        battle["encounters"] = serde_json::json!({ "table": "encounters" });
        let (tmp, manifest) = write_project("goodencounters", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(diags.is_empty(), "{diags:?}");
    }

    #[test]
    fn unknown_encounters_table_is_a_diagnostic() {
        let mut battle = valid_battle();
        battle["encounters"] = serde_json::json!({ "table": "trainers" });
        let (tmp, manifest) = write_project("badenctable", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags
                .iter()
                .any(|d| d.contains("trainers") && d.contains("battle.encounters.table")),
            "{diags:?}"
        );
    }

    #[test]
    fn encounters_table_without_enemies_field_is_a_diagnostic() {
        // "monsters" exists but declares no `enemies` list field.
        let mut battle = valid_battle();
        battle["encounters"] = serde_json::json!({ "table": "monsters" });
        let (tmp, manifest) = write_project("badencfield", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.encounters")
                && d.contains("enemies")
                && d.contains("monsters")),
            "{diags:?}"
        );
    }

    #[test]
    fn malformed_rules_ron_is_a_diagnostic() {
        let (tmp, manifest) = write_project("badron", valid_battle(), Some("Ruleset(types: ["));
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.rules") && d.contains("rules.ron")),
            "{diags:?}"
        );
    }

    #[test]
    fn declared_missing_rules_path_is_a_diagnostic() {
        let mut battle = valid_battle();
        battle["rules"] = serde_json::json!("data/typo.ron");
        let (tmp, manifest) = write_project("missingrules", battle, None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags
                .iter()
                .any(|d| d.contains("battle.rules") && d.contains("typo.ron")),
            "{diags:?}"
        );
    }

    #[test]
    fn undeclared_rules_with_missing_default_file_passes() {
        // battle.rules unset + no data/rules.ron on disk is a legal project
        // (battles run with an empty type chart) — never a diagnostic.
        let (tmp, manifest) = write_project("nodefault", valid_battle(), None);
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(diags.is_empty(), "{diags:?}");
    }

    #[test]
    fn declared_existing_rules_path_validates_content() {
        let mut battle = valid_battle();
        battle["rules"] = serde_json::json!("data/rules.ron");
        let (tmp, manifest) = write_project("declaredbad", battle, Some("Ruleset(types: ["));
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.rules") && d.contains("rules.ron")),
            "{diags:?}"
        );
    }

    #[test]
    fn valid_hooks_pass() {
        let ron = r#"Ruleset(
            stats: ["hp", "attack", "defense", "speed"],
            resources: ["mp"],
            effects: [
                Effect(id: "venom-sting", kind: Move, power: 15, hooks: [
                    Hook(on: "DamagingHit", chance: [30, 100], do: [
                        InflictStatus(status: "poison", target: Target),
                    ]),
                ]),
                Effect(id: "poison", kind: Status, hooks: [
                    Hook(on: "Residual", do: [
                        DamageFraction(num: 1, den: 8, of: MaxHp, target: Target),
                    ]),
                ]),
            ],
        )"#;
        let (tmp, manifest) = write_project("goodhooks", valid_battle(), Some(ron));
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(diags.is_empty(), "{diags:?}");
    }

    #[test]
    fn unknown_hook_names_are_diagnostics() {
        // An unknown op stat name…
        let (tmp, manifest) = write_project(
            "badstatname",
            valid_battle(),
            Some(
                r#"Ruleset(
                    stats: ["attack"],
                    effects: [Effect(id: "x", kind: Move, hooks: [
                        Hook(on: "DamagingHit", do: [Boost(stat: "speed", stages: 1, target: Source)]),
                    ])],
                )"#,
            ),
        );
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.rules") && d.contains("speed")),
            "{diags:?}"
        );

        // …an unknown event name…
        let (tmp, manifest) = write_project(
            "badevent",
            valid_battle(),
            Some(r#"Ruleset(effects: [Effect(id: "x", kind: Move, hooks: [Hook(on: "OnHit")])])"#),
        );
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.rules") && d.contains("OnHit")),
            "{diags:?}"
        );

        // …an unknown chart type name…
        let (tmp, manifest) = write_project(
            "badtype",
            valid_battle(),
            Some(r#"Ruleset(types: ["fire"], type_chart: [(atk: "fire", def: "grass", mult: [2, 1])])"#),
        );
        let diags = battle_diagnostics(&manifest, &tmp.0.join("proj"));
        assert!(
            diags.iter().any(|d| d.contains("battle.rules") && d.contains("grass")),
            "{diags:?}"
        );
    }
}