wh40kdc 0.4.12

Warhammer 40K dataset for the 40kdc-data schema layer: generated types, an embedded dataset behind a linked typed API, plus ListForge + NewRecruit roster importers and exporters.
Documentation
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
//! [`Dataset`] ties the embedded records together: it owns every
//! [`Collection`], builds the cross-entity indexes once, and is the value the
//! link-resolution methods borrow against.
//!
//! Where the TypeScript package exposes lazy view getters
//! (`unit.abilities`, `ability.phases`), Rust expresses the same join graph as
//! `&Dataset` methods returning borrows (`dataset.abilities_of(&unit)`,
//! `dataset.phases_of(&ability)`) — a borrowing view object would be
//! self-referential. The join graph is identical.

use std::collections::HashMap;
use std::sync::OnceLock;

use crate::generated::{
    Ability, DeploymentPattern, Detachment, Enhancement, Faction, ForceDisposition, GameVersion,
    InteractionFlag, LeaderAttachment, Mission, MissionMatchup, Phase, PhaseMapping, ResourcePool,
    SecondaryCard, Stratagem, TerrainLayout, TerrainTemplate, TimingFlag, Unit, UnitComposition,
    Wargear, WargearOption, Weapon, WeaponKeyword,
};

use super::collection::Collection;

/// The shape of the embedded data bundle: one array per entity collection.
///
/// This is the boundary between the generated JSON-Schema types and the linked
/// API. The committed `bundle.generated.json` (written by
/// `cargo run -p xtask -- bundle-data`) deserializes into this; [`Dataset`]
/// wraps it with linked accessors. Every field is `#[serde(default)]` so a
/// collection with no authored data yet stays an empty array and the API
/// surface is stable.
#[derive(Debug, Clone, Default, serde::Deserialize)]
pub struct RawData {
    #[serde(default)]
    pub units: Vec<Unit>,
    #[serde(default)]
    pub weapons: Vec<Weapon>,
    /// Catalog of weapon keywords (Lethal Hits, Sustained Hits N, Anti-X N+, ...).
    #[serde(default)]
    pub weapon_keywords: Vec<WeaponKeyword>,
    #[serde(default)]
    pub factions: Vec<Faction>,
    /// Community-authored ability mechanics (keyed on `ability_id`, not `id`).
    #[serde(default)]
    pub abilities: Vec<Ability>,
    /// Phase assignments, joined to abilities/stratagems/etc. via `source_id`.
    #[serde(default)]
    pub phase_mappings: Vec<PhaseMapping>,
    #[serde(default)]
    pub detachments: Vec<Detachment>,
    #[serde(default)]
    pub stratagems: Vec<Stratagem>,
    #[serde(default)]
    pub enhancements: Vec<Enhancement>,
    #[serde(default)]
    pub leader_attachments: Vec<LeaderAttachment>,
    #[serde(default)]
    pub unit_compositions: Vec<UnitComposition>,
    #[serde(default)]
    pub wargear_options: Vec<WargearOption>,
    /// Non-weapon wargear items (icons, attachments) referenced by wargear options.
    #[serde(default)]
    pub wargear: Vec<Wargear>,
    #[serde(default)]
    pub game_versions: Vec<GameVersion>,
    #[serde(default)]
    pub missions: Vec<Mission>,
    #[serde(default)]
    pub mission_matchups: Vec<MissionMatchup>,
    #[serde(default)]
    pub mission_cards: Vec<SecondaryCard>,
    #[serde(default)]
    pub deployment_patterns: Vec<DeploymentPattern>,
    #[serde(default)]
    pub force_dispositions: Vec<ForceDisposition>,
    /// Reusable terrain catalog: standard areas and scenery features.
    #[serde(default)]
    pub terrain_templates: Vec<TerrainTemplate>,
    /// Terrain layouts: arrangements of catalog/inline pieces on the board.
    #[serde(default)]
    pub terrain_layouts: Vec<TerrainLayout>,
    #[serde(default)]
    pub resource_pools: Vec<ResourcePool>,
    #[serde(default)]
    pub timing_flags: Vec<TimingFlag>,
    #[serde(default)]
    pub interaction_flags: Vec<InteractionFlag>,
}

/// The whole dataset, with linked accessors over every entity collection.
///
/// Build it from the embedded bundle with [`Dataset::embedded`], or from custom
/// data with [`Dataset::from_raw`].
///
/// # Examples
///
/// The headline lookup — note `find_unit("Kharn")` resolves "Khârn the
/// Betrayer" through diacritic folding, and an ability's phases come from the
/// phase-mappings, not the ability record:
///
/// ```
/// use wh40kdc::{Dataset, Phase};
///
/// let ds = Dataset::embedded();
/// let kharn = ds.find_unit("Kharn").expect("Khârn is in the dataset");
///
/// let shooting_abilities: Vec<&str> = ds
///     .abilities_of(kharn)
///     .into_iter()
///     .filter(|a| ds.phases_of(a).contains(&Phase::Shooting))
///     .map(|a| a.ability_id.as_str())
///     .collect();
///
/// assert_eq!(shooting_abilities, ["berzerker-frenzy"]);
/// assert_eq!(ds.faction_of(kharn).unwrap().id.as_str(), "world-eaters");
/// ```
pub struct Dataset {
    // Richly-linked collections.
    pub units: Collection<Unit>,
    pub weapons: Collection<Weapon>,
    pub weapon_keywords: Collection<WeaponKeyword>,
    pub factions: Collection<Faction>,
    pub abilities: Collection<Ability>,

    // Id-bearing passthrough collections.
    pub detachments: Collection<Detachment>,
    pub enhancements: Collection<Enhancement>,
    pub stratagems: Collection<Stratagem>,
    pub wargear_options: Collection<WargearOption>,
    pub wargear: Collection<Wargear>,
    pub missions: Collection<Mission>,
    pub mission_matchups: Collection<MissionMatchup>,
    pub mission_cards: Collection<SecondaryCard>,
    pub deployment_patterns: Collection<DeploymentPattern>,
    pub force_dispositions: Collection<ForceDisposition>,
    pub terrain_templates: Collection<TerrainTemplate>,
    pub terrain_layouts: Collection<TerrainLayout>,
    pub resource_pools: Collection<ResourcePool>,

    // Id-less collections, exposed as plain slices.
    pub leader_attachments: Vec<LeaderAttachment>,
    pub unit_compositions: Vec<UnitComposition>,
    pub game_versions: Vec<GameVersion>,
    pub timing_flags: Vec<TimingFlag>,
    pub interaction_flags: Vec<InteractionFlag>,
    pub phase_mappings: Vec<PhaseMapping>,

    /// `source_type:source_id` → unioned phases.
    phase_index: HashMap<String, Vec<Phase>>,
    /// ability id → indices of units that list it (into `units`).
    units_by_ability: HashMap<String, Vec<usize>>,
    /// weapon id → indices of units that list it (into `units`).
    units_by_weapon: HashMap<String, Vec<usize>>,
    /// unit id → indices of wargear options for it (into `wargear_options`).
    wargear_options_by_unit: HashMap<String, Vec<usize>>,
}

/// The embedded dataset bundle, inlined at build time and parsed once.
#[cfg(feature = "bundled-data")]
const BUNDLE_JSON: &str = include_str!("bundle.generated.json");

impl Dataset {
    /// The dataset built from the crate's embedded data, parsed once and cached.
    ///
    /// Requires the default `bundled-data` feature.
    #[cfg(feature = "bundled-data")]
    pub fn embedded() -> &'static Dataset {
        static EMBEDDED: OnceLock<Dataset> = OnceLock::new();
        EMBEDDED.get_or_init(|| {
            let raw: RawData =
                serde_json::from_str(BUNDLE_JSON).expect("embedded data bundle is valid JSON");
            Dataset::from_raw(raw)
        })
    }

    /// Build a dataset from arbitrary [`RawData`], wiring all collections and
    /// cross-entity indexes.
    pub fn from_raw(raw: RawData) -> Dataset {
        let units = Collection::build(
            raw.units,
            |u| u.id.to_string(),
            |u| Some(u.name.as_str()),
            |u| Some(u.faction_id.as_str()),
            // The same unit id is shared across factions (e.g. ministorum-priest);
            // keep each faction's copy, collapse only true within-faction dupes.
            |u| format!("{}::{}", u.faction_id.as_str(), u.id.as_str()),
        );
        let weapons = Collection::build(
            raw.weapons,
            |w| w.id.to_string(),
            |w| Some(w.name.as_str()),
            |_| None,
            |w| w.id.to_string(),
        );
        let weapon_keywords = id_name_collection(
            raw.weapon_keywords,
            |k| k.id.to_string(),
            |k| Some(k.name.as_str()),
        );
        let factions = Collection::build(
            raw.factions,
            |f| f.id.to_string(),
            |f| Some(f.name.as_str()),
            |_| None,
            |f| f.id.to_string(),
        );
        let abilities = Collection::build(
            raw.abilities,
            |a| a.ability_id.to_string(),
            |a| Some(a.name.as_str()),
            |a| a.faction_id.as_ref().map(|e| e.as_str()),
            |a| a.ability_id.to_string(),
        );

        let detachments = Collection::build(
            raw.detachments,
            |d| d.id.to_string(),
            |d| Some(d.name.as_str()),
            |d| Some(d.faction_id.as_str()),
            |d| d.id.to_string(),
        );
        let enhancements = id_name_collection(
            raw.enhancements,
            |e| e.id.to_string(),
            |e| Some(e.name.as_str()),
        );
        let stratagems = id_name_collection(
            raw.stratagems,
            |s| s.id.to_string(),
            |s| Some(s.name.as_str()),
        );
        let wargear_options =
            id_name_collection(raw.wargear_options, |w| w.id.to_string(), |_| None);
        let wargear =
            id_name_collection(raw.wargear, |w| w.id.to_string(), |w| Some(w.name.as_str()));
        let missions = id_name_collection(
            raw.missions,
            |m| m.id.to_string(),
            |m| Some(m.name.as_str()),
        );
        let mission_matchups =
            id_name_collection(raw.mission_matchups, |m| m.id.to_string(), |_| None);
        let mission_cards = id_name_collection(
            raw.mission_cards,
            |s| s.id.to_string(),
            |s| Some(s.name.as_str()),
        );
        let deployment_patterns = id_name_collection(
            raw.deployment_patterns,
            |d| d.id.to_string(),
            |d| Some(d.name.as_str()),
        );
        // ForceDispositionId is a Display string-enum, not a newtype.
        let force_dispositions = id_name_collection(
            raw.force_dispositions,
            |f| f.id.to_string(),
            |f| Some(f.name.as_str()),
        );
        let terrain_templates = id_name_collection(
            raw.terrain_templates,
            |t| t.id.to_string(),
            |t| Some(t.name.as_str()),
        );
        let terrain_layouts = id_name_collection(
            raw.terrain_layouts,
            |l| l.id.to_string(),
            |l| Some(l.name.as_str()),
        );
        let resource_pools = Collection::build(
            raw.resource_pools,
            |r| r.id.to_string(),
            |r| Some(r.name.as_str()),
            |r| Some(r.faction_id.as_str()),
            |r| r.id.to_string(),
        );

        let phase_index = build_phase_index(&raw.phase_mappings);
        let (units_by_ability, units_by_weapon) = build_reverse_indexes(&units);
        let mut wargear_options_by_unit: HashMap<String, Vec<usize>> = HashMap::new();
        for (idx, option) in wargear_options.all().iter().enumerate() {
            wargear_options_by_unit
                .entry(option.unit_id.to_string())
                .or_default()
                .push(idx);
        }

        Dataset {
            units,
            weapons,
            weapon_keywords,
            factions,
            abilities,
            detachments,
            enhancements,
            stratagems,
            wargear_options,
            wargear,
            missions,
            mission_matchups,
            mission_cards,
            deployment_patterns,
            force_dispositions,
            terrain_templates,
            terrain_layouts,
            resource_pools,
            leader_attachments: raw.leader_attachments,
            unit_compositions: raw.unit_compositions,
            game_versions: raw.game_versions,
            timing_flags: raw.timing_flags,
            interaction_flags: raw.interaction_flags,
            phase_mappings: raw.phase_mappings,
            phase_index,
            units_by_ability,
            units_by_weapon,
            wargear_options_by_unit,
        }
    }

    // --- Convenience finders (delegate to the matching collection) ----------

    /// Find a unit by id or name (diacritic-insensitive). See
    /// [`Collection::find`].
    pub fn find_unit(&self, query: &str) -> Option<&Unit> {
        self.units.find(query)
    }

    /// Find a weapon by id or name.
    pub fn find_weapon(&self, query: &str) -> Option<&Weapon> {
        self.weapons.find(query)
    }

    /// Find a faction by id or name.
    pub fn find_faction(&self, query: &str) -> Option<&Faction> {
        self.factions.find(query)
    }

    /// Find an ability by id (`ability_id`) or name.
    pub fn find_ability(&self, query: &str) -> Option<&Ability> {
        self.abilities.find(query)
    }

    // --- Link resolution (the &Dataset replacement for view getters) --------

    /// The unit's faction, or `None` if its `faction_id` is unknown.
    pub fn faction_of(&self, unit: &Unit) -> Option<&Faction> {
        self.factions.get(unit.faction_id.as_str())
    }

    /// Resolve a terrain layout to absolute board-space vertices, using this
    /// dataset's embedded terrain-template catalog. This is the layout-id →
    /// renderable-geometry hop a consumer (e.g. shadowboxing) wants. Errors if
    /// the layout references a template absent from the catalog. The generated
    /// types round-trip through their canonical JSON into the resolver's input
    /// structs — the same JSON the conformance corpus pins.
    pub fn resolve_terrain(
        &self,
        layout: &TerrainLayout,
    ) -> Result<Vec<crate::terrain::ResolvedPiece>, crate::terrain::TerrainResolveError> {
        fn convert<T: serde::de::DeserializeOwned, S: serde::Serialize>(value: &S) -> T {
            serde_json::from_value(
                serde_json::to_value(value).expect("generated terrain serializes"),
            )
            .expect("generated terrain type matches resolver shape")
        }
        let r_layout: crate::terrain::TerrainLayout = convert(layout);
        let templates: Vec<crate::terrain::TerrainTemplate> =
            self.terrain_templates.all().iter().map(convert).collect();
        crate::terrain::resolve_layout(&r_layout, &templates)
    }

    /// The terrain layouts a deployment pattern recommends, in declared order,
    /// skipping any ids absent from the dataset.
    pub fn recommended_terrain_layouts(&self, pattern: &DeploymentPattern) -> Vec<&TerrainLayout> {
        pattern
            .recommended_terrain_layout_ids
            .iter()
            .flatten()
            .filter_map(|id| self.terrain_layouts.get(id.as_str()))
            .collect()
    }

    /// Weapons referenced by `weapon_ids`; unresolved ids are skipped.
    pub fn weapons_of(&self, unit: &Unit) -> Vec<&Weapon> {
        unit.weapon_ids
            .iter()
            .filter_map(|id| self.weapons.get(id.as_str()))
            .collect()
    }

    /// Wargear options authored for the given unit, in declared order. Mirror of
    /// TS `Dataset.wargearOptionsOf`. Empty for a unit with no options.
    pub fn wargear_options_of(&self, unit: &Unit) -> Vec<&WargearOption> {
        self.wargear_options_by_unit
            .get(unit.id.as_str())
            .map(|idxs| idxs.iter().map(|&i| self.wargear_options.at(i)).collect())
            .unwrap_or_default()
    }

    /// Abilities referenced by `ability_ids`; unresolved ids are skipped.
    pub fn abilities_of(&self, unit: &Unit) -> Vec<&Ability> {
        unit.ability_ids
            .iter()
            .filter_map(|id| self.abilities.get(id.as_str()))
            .collect()
    }

    /// Game phases an ability acts in, unioned across its phase-mappings.
    ///
    /// Phases are not stored on the ability — they live in `phase_mappings`
    /// records where `source_type == "ability"` and `source_id == ability_id`.
    pub fn phases_of(&self, ability: &Ability) -> &[Phase] {
        self.phases_for("ability", ability.ability_id.as_str())
    }

    /// Phases a source acts in, unioned across its phase-mappings, keyed by the
    /// `source_type` / `source_id` pair (e.g. `("ability", "berzerker-frenzy")`).
    pub fn phases_for(&self, source_type: &str, source_id: &str) -> &[Phase] {
        self.phase_index
            .get(&format!("{source_type}:{source_id}"))
            .map_or(&[], Vec::as_slice)
    }

    /// Units that list the given ability id in their `ability_ids`.
    pub fn units_with_ability(&self, ability_id: &str) -> Vec<&Unit> {
        self.units_by_ability
            .get(ability_id)
            .map(|idxs| idxs.iter().map(|&i| self.units.at(i)).collect())
            .unwrap_or_default()
    }

    /// Units that list the given weapon id in their `weapon_ids`.
    pub fn units_with_weapon(&self, weapon_id: &str) -> Vec<&Unit> {
        self.units_by_weapon
            .get(weapon_id)
            .map(|idxs| idxs.iter().map(|&i| self.units.at(i)).collect())
            .unwrap_or_default()
    }

    /// Leaders whose leader-attachment data lists `bodyguard_unit_id` among its
    /// eligible body units, sorted by name. The attachment is stored on the
    /// leader pointing down to its bodyguards, so answering "which leaders can
    /// attach to this unit?" means scanning the attachment list. Returns an
    /// empty vec for a unit nothing attaches to (including leader units).
    pub fn leaders_attachable_to(&self, bodyguard_unit_id: &str) -> Vec<&Unit> {
        let mut out: Vec<&Unit> = self
            .leader_attachments
            .iter()
            .filter(|la| {
                la.eligible_bodyguard_ids
                    .iter()
                    .any(|id| id.as_str() == bodyguard_unit_id)
            })
            .filter_map(|la| self.units.get(la.leader_id.as_str()))
            .collect();
        out.sort_by(|a, b| a.name.cmp(&b.name));
        out
    }

    /// The inverse of [`leaders_attachable_to`](Self::leaders_attachable_to):
    /// the body units the given leader can attach to, sorted by name. Scans the
    /// same data from the leader's side (`leader_id` matches; resolve each
    /// `eligible_bodyguard_ids` entry), deduped by id. Empty for a non-leader
    /// unit. The two together give the bidirectional attachment graph.
    pub fn bodyguards_attachable_from(&self, leader_unit_id: &str) -> Vec<&Unit> {
        let mut seen = std::collections::HashSet::new();
        let mut out: Vec<&Unit> = Vec::new();
        for la in &self.leader_attachments {
            if la.leader_id.as_str() != leader_unit_id {
                continue;
            }
            for bodyguard_id in &la.eligible_bodyguard_ids {
                if !seen.insert(bodyguard_id.as_str()) {
                    continue;
                }
                if let Some(unit) = self.units.get(bodyguard_id.as_str()) {
                    out.push(unit);
                }
            }
        }
        out.sort_by(|a, b| a.name.cmp(&b.name));
        out
    }

    /// Faction-scoped abilities (abilities whose `faction_id` is this faction).
    pub fn abilities_of_faction(&self, faction_id: &str) -> Vec<&Ability> {
        self.abilities.by_faction(faction_id)
    }

    /// Distinct weapons carried by a faction's units (deduplicated by id).
    pub fn weapons_of_faction(&self, faction_id: &str) -> Vec<&Weapon> {
        let mut seen = std::collections::HashSet::new();
        let mut out = Vec::new();
        for unit in self.units.by_faction(faction_id) {
            for weapon in self.weapons_of(unit) {
                if seen.insert(weapon.id.as_str().to_string()) {
                    out.push(weapon);
                }
            }
        }
        out
    }
}

/// Build a passthrough collection keyed on id (no faction scoping). The id also
/// serves as the dedupe key.
fn id_name_collection<T>(
    items: Vec<T>,
    id_of: impl Fn(&T) -> String,
    name_of: impl Fn(&T) -> Option<&str>,
) -> Collection<T> {
    Collection::build(items, &id_of, name_of, |_| None, |i| id_of(i))
}

/// Union the phases of every phase-mapping, keyed `source_type:source_id`.
fn build_phase_index(phase_mappings: &[PhaseMapping]) -> HashMap<String, Vec<Phase>> {
    let mut index: HashMap<String, Vec<Phase>> = HashMap::new();
    for pm in phase_mappings {
        let key = format!("{}:{}", pm.source_type, pm.source_id.as_str());
        let entry = index.entry(key).or_default();
        for &phase in pm.phases.iter() {
            if !entry.contains(&phase) {
                entry.push(phase);
            }
        }
    }
    index
}

/// Build ability-id→units and weapon-id→units reverse indexes (values are
/// positions into the deduplicated units collection).
fn build_reverse_indexes(
    units: &Collection<Unit>,
) -> (HashMap<String, Vec<usize>>, HashMap<String, Vec<usize>>) {
    let mut by_ability: HashMap<String, Vec<usize>> = HashMap::new();
    let mut by_weapon: HashMap<String, Vec<usize>> = HashMap::new();
    for (idx, unit) in units.all().iter().enumerate() {
        for ability_id in &unit.ability_ids {
            by_ability
                .entry(ability_id.to_string())
                .or_default()
                .push(idx);
        }
        for weapon_id in &unit.weapon_ids {
            by_weapon
                .entry(weapon_id.to_string())
                .or_default()
                .push(idx);
        }
    }
    (by_ability, by_weapon)
}