wh40kdc 0.2.0

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
//! Resolve a [`ParsedRoster`] onto 40kdc entity ids, producing a [`Roster`].
//!
//! Resolution is lenient: a name that doesn't match a 40kdc entity yields a
//! [`ResolvedRef`] with `id: None`, `resolved: false`, and up to five candidate
//! suggestions — the roster is never dropped or rejected. Everything that didn't
//! resolve cleanly is summarised in the [`Diagnostics`] block.
//!
//! Matching reuses the dataset's own lookups ([`Dataset::find_*`], `find_all`,
//! `by_faction`) and [`normalize_name`](crate::normalize_name); there is no
//! bespoke fuzzy matcher. Faction is resolved first so unit/detachment/
//! enhancement lookups can be scoped to it — the same unit id can appear under
//! several factions, so scoping disambiguates.
//!
//! Rust mirror of `tools/src/import/resolve.ts`.

use crate::data::{normalize_name, Dataset};

use super::types::{
    BattleSize, Candidate, Diagnostics, ParsedRoster, ParsedUnit, ResolvedRef, Roster,
    RosterFormat, RosterLeaderAttachment, RosterPoints, RosterSource, RosterUnit, RosterWargear,
    Warning, WarningCode,
};

/// The dataset edition/dataslate stamped onto an imported roster.
const ROSTER_EDITION: &str = "11th";
const ROSTER_DATASLATE: &str = "pre-launch-provisional";

const MAX_CANDIDATES: usize = 5;

/// Accumulates warnings and resolved/unresolved tallies during an import.
#[derive(Default)]
struct DiagnosticsBuilder {
    resolved_units: u64,
    unresolved_units: u64,
    resolved_weapons: u64,
    unresolved_weapons: u64,
    warnings: Vec<Warning>,
}

impl DiagnosticsBuilder {
    fn warn(&mut self, code: WarningCode, message: &str, raw_name: Option<&str>) {
        self.warnings.push(Warning {
            code,
            message: message.to_string(),
            raw_name: raw_name.map(str::to_string),
        });
    }

    fn build(self) -> Diagnostics {
        Diagnostics {
            resolved_units: self.resolved_units,
            unresolved_units: self.unresolved_units,
            resolved_weapons: self.resolved_weapons,
            unresolved_weapons: self.unresolved_weapons,
            warnings: self.warnings,
        }
    }
}

fn unresolved(raw_name: &str, candidates: Vec<Candidate>) -> ResolvedRef {
    ResolvedRef {
        id: None,
        raw_name: raw_name.to_string(),
        resolved: false,
        candidates,
    }
}

fn resolved(id: &str, raw_name: &str) -> ResolvedRef {
    ResolvedRef {
        id: Some(id.to_string()),
        raw_name: raw_name.to_string(),
        resolved: true,
        candidates: Vec::new(),
    }
}

/// Map a source battle-size label to the 40kdc enum, if recognisable.
fn map_battle_size(raw: Option<&str>) -> Option<BattleSize> {
    let key = normalize_name(raw?);
    if key.contains("strike force") {
        Some(BattleSize::StrikeForce)
    } else if key.contains("incursion") {
        Some(BattleSize::Incursion)
    } else {
        None
    }
}

/// Resolve a [`ParsedRoster`] against the dataset.
///
/// # Examples
///
/// ```
/// use wh40kdc::Dataset;
/// use wh40kdc::import::{import_roster, decode_listforge, RosterFormat};
///
/// let payload = decode_listforge(r#"{
///     "name": "Demo",
///     "roster": { "name": "Demo", "forces": [] }
/// }"#).unwrap();
/// let roster = import_roster(&payload, Dataset::embedded()).unwrap();
/// assert_eq!(roster.source.format, RosterFormat::Listforge);
/// ```
pub fn resolve(parsed: &ParsedRoster, ds: &Dataset, format: RosterFormat) -> Roster {
    let mut diag = DiagnosticsBuilder::default();

    if parsed.multi_force {
        diag.warn(
            WarningCode::MultiForce,
            "Source list contains more than one faction; the primary faction was used for scoping.",
            None,
        );
    }

    // --- Faction (resolved first so other lookups can scope to it). ---------
    let mut faction_id: Option<String> = None;
    if let Some(raw) = &parsed.faction_raw_name {
        if let Some(hit) = ds.factions.find(raw) {
            faction_id = Some(hit.id.as_str().to_string());
        } else {
            diag.warn(
                WarningCode::FactionUnresolved,
                "Faction name did not match any 40kdc faction.",
                Some(raw),
            );
        }
    }

    // --- Detachment (scoped to faction, then global fallback). --------------
    let mut detachment_id: Option<String> = None;
    if let Some(raw) = &parsed.detachment_raw_name {
        let key = normalize_name(raw);
        let scoped = faction_id.as_deref().and_then(|f| {
            ds.detachments
                .by_faction(f)
                .into_iter()
                .find(|d| normalize_name(&d.name) == key)
        });
        let hit = scoped.or_else(|| ds.detachments.find(raw));
        if let Some(hit) = hit {
            detachment_id = Some(hit.id.as_str().to_string());
        } else {
            diag.warn(
                WarningCode::DetachmentUnresolved,
                "Detachment name did not match any 40kdc detachment.",
                Some(raw),
            );
        }
    }

    // --- Battle size. -------------------------------------------------------
    let battle_size = map_battle_size(parsed.battle_size_raw.as_deref());
    if parsed.battle_size_raw.is_some() && battle_size.is_none() {
        diag.warn(
            WarningCode::BattleSizeUnmapped,
            "Battle size label could not be mapped.",
            parsed.battle_size_raw.as_deref(),
        );
    }

    // --- Units (and their enhancements / wargear). --------------------------
    let mut units: Vec<RosterUnit> = parsed
        .units
        .iter()
        .map(|u| {
            resolve_unit(
                u,
                faction_id.as_deref(),
                detachment_id.as_deref(),
                ds,
                &mut diag,
            )
        })
        .collect();

    // --- Leader attachments (second pass: needs all resolved unit ids). -----
    infer_leader_attachments(&parsed.units, &mut units, ds, &mut diag);

    // --- Points reconciliation (reported vs computed kept distinct). --------
    if let Some(reported) = parsed.total_reported {
        if reported != parsed.total_computed {
            diag.warn(
                WarningCode::PointsMismatch,
                &format!(
                    "Source-reported total ({reported}) differs from the sum of cost lines ({}).",
                    parsed.total_computed
                ),
                None,
            );
        }
    }

    Roster {
        name: parsed.name.clone(),
        source: RosterSource {
            format,
            generated_by: parsed.generated_by.clone(),
        },
        faction_id,
        detachment_id,
        battle_size,
        points: RosterPoints {
            declared_limit: parsed.declared_limit,
            total_reported: parsed.total_reported,
            total_computed: parsed.total_computed,
        },
        units,
        game_version: super::types::GameVersionRef {
            edition: ROSTER_EDITION.to_string(),
            dataslate: ROSTER_DATASLATE.to_string(),
        },
        diagnostics: diag.build(),
    }
}

fn resolve_unit(
    parsed: &ParsedUnit,
    faction_id: Option<&str>,
    detachment_id: Option<&str>,
    ds: &Dataset,
    diag: &mut DiagnosticsBuilder,
) -> RosterUnit {
    // Prefer a faction-scoped match (the same unit id recurs across factions),
    // then fall back to a global name lookup.
    let key = normalize_name(&parsed.raw_name);
    let all = ds.units.find_all(&parsed.raw_name);
    let scoped_id = faction_id.and_then(|f| {
        ds.units
            .by_faction(f)
            .into_iter()
            .find(|u| normalize_name(&u.name) == key)
            .map(|u| u.id.as_str().to_string())
    });
    let hit_id = scoped_id.or_else(|| all.first().map(|u| u.id.as_str().to_string()));

    let ref_ = if let Some(id) = &hit_id {
        diag.resolved_units += 1;
        resolved(id, &parsed.raw_name)
    } else {
        diag.unresolved_units += 1;
        diag.warn(
            WarningCode::UnitUnresolved,
            "Unit name did not match any 40kdc unit.",
            Some(&parsed.raw_name),
        );
        unresolved(&parsed.raw_name, unit_candidates(&all))
    };

    let enhancement = parsed
        .enhancement_raw_name
        .as_deref()
        .map(|name| resolve_enhancement(name, detachment_id, ds, diag));
    let enhancement_points = if enhancement.is_some() {
        parsed.enhancement_points
    } else {
        None
    };

    let wargear = parsed
        .wargear
        .iter()
        .map(|w| {
            let hits = ds.weapons.find_all(&w.raw_name);
            if let Some(first) = hits.first() {
                diag.resolved_weapons += 1;
                RosterWargear {
                    ref_: resolved(first.id.as_str(), &w.raw_name),
                    count: w.count,
                }
            } else {
                diag.unresolved_weapons += 1;
                diag.warn(
                    WarningCode::WeaponUnresolved,
                    "Weapon name did not match any 40kdc weapon.",
                    Some(&w.raw_name),
                );
                RosterWargear {
                    ref_: unresolved(&w.raw_name, weapon_candidates(&hits)),
                    count: w.count,
                }
            }
        })
        .collect();

    RosterUnit {
        ref_,
        model_count: parsed.model_count,
        points: parsed.points,
        is_warlord: parsed.is_warlord,
        enhancement,
        enhancement_points,
        wargear,
        leader_attachment: None,
    }
}

fn resolve_enhancement(
    raw_name: &str,
    detachment_id: Option<&str>,
    ds: &Dataset,
    diag: &mut DiagnosticsBuilder,
) -> ResolvedRef {
    let key = normalize_name(raw_name);
    // Enhancements belong to a detachment, not a faction — scope by detachment_id.
    let scoped = detachment_id.and_then(|det| {
        ds.enhancements
            .all()
            .iter()
            .find(|e| e.detachment_id.as_str() == det && normalize_name(&e.name) == key)
    });
    let hit = scoped.or_else(|| ds.enhancements.find(raw_name));
    if let Some(hit) = hit {
        return resolved(hit.id.as_str(), raw_name);
    }
    diag.warn(
        WarningCode::EnhancementUnresolved,
        "Enhancement name did not match any 40kdc enhancement.",
        Some(raw_name),
    );
    let candidates = ds
        .enhancements
        .find_all(raw_name)
        .iter()
        .take(MAX_CANDIDATES)
        .map(|e| Candidate {
            id: e.id.as_str().to_string(),
            name: e.name.to_string(),
        })
        .collect();
    unresolved(raw_name, candidates)
}

/// Infer leader→bodyguard attachments. The source format does not encode an
/// unambiguous attachment, so each inferred link is marked provisional: a
/// resolved character unit is matched against a resolved non-character unit in
/// the same roster using the dataset's leader-attachment data.
fn infer_leader_attachments(
    parsed_units: &[ParsedUnit],
    units: &mut [RosterUnit],
    ds: &Dataset,
    diag: &mut DiagnosticsBuilder,
) {
    let bodyguard_ids: std::collections::HashSet<String> = units
        .iter()
        .zip(parsed_units)
        .filter(|(u, p)| u.ref_.id.is_some() && !p.is_character)
        .filter_map(|(u, _)| u.ref_.id.clone())
        .collect();

    // First compute the attachments (immutable borrow of units), then apply
    // them (mutable borrow) to avoid overlapping borrows.
    let mut planned: Vec<(usize, String, String)> = Vec::new(); // (leader idx, bodyguard id, bodyguard raw name)
    for (i, (unit, parsed)) in units.iter().zip(parsed_units).enumerate() {
        let Some(leader_id) = &unit.ref_.id else {
            continue;
        };
        if !parsed.is_character {
            continue;
        }
        let Some(attachment) = ds
            .leader_attachments
            .iter()
            .find(|la| la.leader_id.as_str() == leader_id)
        else {
            continue;
        };
        let Some(bodyguard_id) = attachment
            .eligible_bodyguard_ids
            .iter()
            .map(|e| e.as_str())
            .find(|id| bodyguard_ids.contains(*id))
        else {
            continue;
        };
        let Some(bodyguard) = units
            .iter()
            .find(|u| u.ref_.id.as_deref() == Some(bodyguard_id))
        else {
            continue;
        };
        planned.push((i, bodyguard_id.to_string(), bodyguard.ref_.raw_name.clone()));
    }

    for (idx, bodyguard_id, bodyguard_raw_name) in planned {
        units[idx].leader_attachment = Some(RosterLeaderAttachment {
            bodyguard_ref: resolved(&bodyguard_id, &bodyguard_raw_name),
            provisional: true,
        });
        let leader_raw = units[idx].ref_.raw_name.clone();
        diag.warn(
            WarningCode::LeaderAttachmentInferred,
            "Leader attachment was inferred from leader-attachment data and is provisional.",
            Some(&leader_raw),
        );
    }
}

fn unit_candidates(records: &[&crate::Unit]) -> Vec<Candidate> {
    records
        .iter()
        .take(MAX_CANDIDATES)
        .map(|u| Candidate {
            id: u.id.as_str().to_string(),
            name: u.name.to_string(),
        })
        .collect()
}

fn weapon_candidates(records: &[&crate::Weapon]) -> Vec<Candidate> {
    records
        .iter()
        .take(MAX_CANDIDATES)
        .map(|w| Candidate {
            id: w.id.as_str().to_string(),
            name: w.name.to_string(),
        })
        .collect()
}