opy-rs 0.3.7

Standalone OverPy-compatible .opy implementation with parsing, tooling, and bounded Workshop compilation.
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
use super::*;
use workshop_rs::source::{Position as WorkshopPosition, Span as WorkshopSpan};

/// Convert resolved HIR settings into the canonical Workshop settings
/// carrier exactly as lowering does: constant expansion, then `#!extension`
/// merging, then pass-through of members outside the settings catalog.
/// Shared by lowering and by `check`'s emission-acceptance pass so `check`
/// and `compile` see the same settings tree (#411). The second value lists
/// the members passed through unchanged.
pub(crate) fn workshop_settings(
    hir: &crate::hir::Program,
) -> Result<(Option<workshop_rs::settings::Settings>, Vec<UnknownSetting>), IntegrationError> {
    let settings_constants: HashMap<String, &Expr> = hir
        .declarations
        .iter()
        .filter_map(|declaration| match declaration {
            hir::Declaration::Constant { name, value, .. } => Some((name.clone(), value.as_ref())),
            _ => None,
        })
        .collect();
    let mut settings = merge_extensions(
        hir.settings
            .clone()
            .map(|settings| expand_settings_constants(settings, &settings_constants)),
        &hir.preprocessing.directives,
    )?;
    let unknown = settings
        .as_mut()
        .map(pass_through_unknown_members)
        .transpose()?
        .unwrap_or_default();
    Ok((settings, unknown))
}

/// A settings member the catalog does not declare, emitted as written.
pub(crate) struct UnknownSetting {
    /// The member's key.
    pub(crate) name: String,
    /// The undeclared value of a catalogued key; `None` for an unknown key.
    pub(crate) value: Option<String>,
    pub(crate) span: Option<WorkshopSpan>,
}

/// Whether the pinned OverPy schema applies a hero settings key to a
/// specific hero. Its post-load schema merge expands
/// `heroes.values.__generalAndEachHero__` plus the `__eachHero__` keys whose
/// `include`/`exclude` hero filters admit the hero into that hero's `values`
/// set; `compileCustomGameSettingsDict` then looks authored keys up in the
/// merged set and writes the ones it cannot find back verbatim. The
/// `data/hero_applicability.json` artifact records each merged key's
/// applying heroes in the smaller of include/exclude form and is generated
/// from the pinned compiler's own merge by
/// `tools/overpy/gen_hero_applicability.cjs`.
fn hero_setting_applies(hero: &str, key: &str) -> bool {
    let data = applicability();
    if data.all.iter().any(|candidate| candidate == key) {
        return true;
    }
    if let Some(heroes) = data.only.get(key) {
        return heroes.iter().any(|candidate| candidate == hero);
    }
    if let Some(heroes) = data.except.get(key) {
        return !heroes.iter().any(|candidate| candidate == hero);
    }
    false
}

#[derive(serde::Deserialize)]
struct HeroApplicability {
    all: Vec<String>,
    only: std::collections::HashMap<String, Vec<String>>,
    except: std::collections::HashMap<String, Vec<String>>,
}

fn applicability() -> &'static HeroApplicability {
    static DATA: std::sync::OnceLock<HeroApplicability> = std::sync::OnceLock::new();
    DATA.get_or_init(|| {
        serde_json::from_str(include_str!("data/hero_applicability.json"))
            .expect("hero applicability data is valid JSON")
    })
}

/// The pinned compiler rewrites these authored hero spellings under
/// `heroes.<team>` — group names and hero-list elements alike — before the
/// schema lookup (`compileCustomGameSettingsDict`). The order matters: group
/// renames run as one pass per alias in this order.
const HERO_NAME_ALIASES: &[(&str, &str)] = &[("mccree", "cassidy"), ("hammond", "wreckingBall")];

fn canonical_hero_name(name: &str) -> &str {
    HERO_NAME_ALIASES
        .iter()
        .find_map(|(alias, canonical)| (*alias == name).then_some(*canonical))
        .unwrap_or(name)
}

fn rename_member(member: &mut workshop_rs::settings::SettingsNode, name: &str) {
    use workshop_rs::settings::SettingsNode;
    match member {
        SettingsNode::Group { name: slot, .. }
        | SettingsNode::Number { name: slot, .. }
        | SettingsNode::Bool { name: slot, .. }
        | SettingsNode::Flag { name: slot, .. }
        | SettingsNode::String { name: slot, .. }
        | SettingsNode::List { name: slot, .. }
        | SettingsNode::Raw { name: slot, .. }
        | SettingsNode::RawValue { name: slot, .. } => *slot = name.to_string(),
        SettingsNode::Workshop { .. } => {}
    }
}

/// The pinned OverPy writes a `main`, `lobby`, mode, team `general`, or hero
/// member it cannot translate as authored instead of rejecting it: an unknown
/// key with its value (`key: value`, a list or object as a block), and a
/// catalogued enum key with a value outside its domain. Projects rely on this
/// for settings the catalog lacks, often rewriting them in a post-compile
/// hook. Replace each such member with its written form, drop a mode's
/// non-Boolean `enabled` as upstream does, and lift each team's `general`
/// members ahead of its hero groups as the pinned OverPy writes them.
fn pass_through_unknown_members(
    settings: &mut workshop_rs::settings::Settings,
) -> Result<Vec<UnknownSetting>, IntegrationError> {
    use workshop_rs::settings::{PathPart, SettingsNode};

    let mut unknown = Vec::new();
    for group in &mut settings.children {
        let SettingsNode::Group { name, children, .. } = group else {
            continue;
        };
        match name.as_str() {
            "main" | "lobby" => {
                pass_through_members(children, &[PathPart::Part(name)], &mut unknown, None);
            }
            "gamemodes" => {
                for mode in children {
                    if let SettingsNode::Group { name, children, .. } = mode {
                        children.retain(|member| {
                            member.name() != "enabled"
                                || matches!(member, SettingsNode::Bool { .. })
                        });
                        let path = [PathPart::Part("gamemodes"), PathPart::Part(name)];
                        pass_through_members(children, &path, &mut unknown, None);
                    }
                }
            }
            "heroes" => {
                for team in children {
                    let SettingsNode::Group {
                        name: team_name,
                        children,
                        ..
                    } = team
                    else {
                        continue;
                    };
                    // Any other team member is a hero name to upstream.
                    let mut general = Vec::new();
                    let mut rest = Vec::new();
                    for child in std::mem::take(children) {
                        match child {
                            SettingsNode::Group { name, children, .. } if name == "general" => {
                                general.extend(children);
                            }
                            // Upstream iterates the value's `Object.keys`: a
                            // non-dict `general` yields index-keyed members for
                            // strings and lists, and drops numbers/booleans.
                            child if child.name() == "general" => match child {
                                SettingsNode::String { value, span, .. } => {
                                    general.extend(value.chars().enumerate().map(|(index, ch)| {
                                        SettingsNode::Raw {
                                            name: index.to_string(),
                                            value: ch.to_string(),
                                            span,
                                        }
                                    }));
                                }
                                SettingsNode::List { elements, .. } => {
                                    general.extend(elements.iter().enumerate().map(
                                        |(index, element)| SettingsNode::Raw {
                                            name: index.to_string(),
                                            value: element.value.clone(),
                                            span: element.span,
                                        },
                                    ));
                                }
                                _ => {}
                            },
                            child => rest.push(child),
                        }
                    }
                    let team_path = [PathPart::Part("heroes"), PathPart::Team];
                    pass_through_members(&mut general, &team_path, &mut unknown, None);
                    let mut enabled = false;
                    let mut disabled = false;
                    for child in &rest {
                        enabled |= child.name() == "enabledHeroes";
                        disabled |= child.name() == "disabledHeroes";
                    }
                    if enabled && disabled {
                        return Err(IntegrationError::new(
                            "settings-hero-lists",
                            format!(
                                "Cannot have both 'enabledHeroes' and 'disabledHeroes' in team '{team_name}'"
                            ),
                            None,
                        ));
                    }
                    // The pinned compiler emits `enabledHeroes`/`disabledHeroes`
                    // after every hero group regardless of authored position.
                    let (mut hero_lists, mut rest): (Vec<_>, Vec<_>) =
                        rest.into_iter().partition(|child| {
                            matches!(child, SettingsNode::List { name, .. } if matches!(
                                name.as_str(),
                                "enabledHeroes" | "disabledHeroes"
                            ))
                        });
                    // Canonical hero-group names take upstream's assign+delete
                    // rename: the source group lands at the destination's
                    // position, collapsing a duplicate, or at the end of the
                    // hero groups when the destination is absent. Upstream
                    // runs one pass per alias in `HERO_NAME_ALIASES` order, so
                    // appended groups follow that order, not authored order.
                    for (alias, canonical) in HERO_NAME_ALIASES {
                        let mut index = 0;
                        while index < rest.len() {
                            let SettingsNode::Group { name, .. } = &rest[index] else {
                                index += 1;
                                continue;
                            };
                            if name != alias {
                                index += 1;
                                continue;
                            }
                            let mut moved = rest.remove(index);
                            if let SettingsNode::Group { name, .. } = &mut moved {
                                *name = (*canonical).to_string();
                            }
                            match rest.iter_mut().find(|m| m.name() == *canonical) {
                                Some(dest) => *dest = moved,
                                None => rest.push(moved),
                            }
                        }
                    }
                    // The flattened `general` members are not reprocessed:
                    // only authored hero groups and hero rosters take the
                    // hero-name and applicability passes.
                    for hero in rest.iter_mut().chain(hero_lists.iter_mut()) {
                        match hero {
                            SettingsNode::Group { name, children, .. } => {
                                let canonical = canonical_hero_name(name).to_string();
                                // The pinned compiler rewrites `ability1KB%`
                                // to `ability1Kb%` inside hero settings before
                                // the schema lookup; the rewritten member takes
                                // the source value at the destination key's
                                // position, collapsing a duplicate, and moves to
                                // the end when the destination is absent.
                                if let Some(source) =
                                    children.iter().position(|m| m.name() == "ability1KB%")
                                {
                                    let mut moved = children.remove(source);
                                    rename_member(&mut moved, "ability1Kb%");
                                    match children.iter_mut().find(|m| m.name() == "ability1Kb%") {
                                        Some(dest) => *dest = moved,
                                        None => children.push(moved),
                                    }
                                }
                                let path =
                                    [PathPart::Part("heroes"), PathPart::Team, PathPart::Hero];
                                pass_through_members(
                                    children,
                                    &path,
                                    &mut unknown,
                                    Some(canonical.as_str()),
                                );
                            }
                            SettingsNode::List { name, elements, .. }
                                if matches!(name.as_str(), "enabledHeroes" | "disabledHeroes") =>
                            {
                                for element in elements {
                                    let canonical = canonical_hero_name(&element.value);
                                    if canonical != element.value {
                                        element.value = canonical.to_string();
                                    }
                                }
                            }
                            _ => {}
                        }
                    }
                    general.extend(rest);
                    general.extend(hero_lists);
                    *children = general;
                }
            }
            _ => {}
        }
    }
    Ok(unknown)
}

fn pass_through_members(
    members: &mut [workshop_rs::settings::SettingsNode],
    path: &[workshop_rs::settings::PathPart<'_>],
    unknown: &mut Vec<UnknownSetting>,
    hero: Option<&str>,
) {
    use workshop_rs::settings::{PathPart, SettingValueDomain, SettingsNode};

    for member in members {
        // A mode's Boolean `enabled` is consumed by the mode header.
        if member.name() == "enabled" && matches!(path, [PathPart::Part("gamemodes"), _]) {
            continue;
        }
        let name = member.name().to_string();
        let span = member.span();
        let mut full = path.to_vec();
        full.push(PathPart::Part(&name));
        let definition = workshop_rs::settings::definition(&full);
        // A catalogued key the pinned schema does not apply to this hero is
        // unknown for it and passes through like any other unknown key, but
        // fully verbatim: a `RawValue` at the catalogued path would still
        // emit the canonical name.
        let inapplicable = definition.is_some()
            && hero.is_some_and(|hero| !hero_setting_applies(hero, name.as_str()));
        match definition.filter(|_| !inapplicable) {
            None => {
                if matches!(member, SettingsNode::Raw { .. }) {
                    continue;
                }
                *member = if inapplicable {
                    verbatim_form(member.clone())
                } else {
                    written_form(member.clone())
                };
                unknown.push(UnknownSetting {
                    name,
                    value: None,
                    span,
                });
            }
            Some(definition) => {
                if !matches!(definition.domain(), SettingValueDomain::Enum { .. }) {
                    continue;
                }
                let value = match member {
                    SettingsNode::String { value, .. }
                        if !definition.enum_members().any(|member| member.id() == value) =>
                    {
                        value.clone()
                    }
                    SettingsNode::Number { .. } | SettingsNode::Bool { .. } => {
                        scalar_text(member).expect("scalar member")
                    }
                    _ => continue,
                };
                *member = SettingsNode::RawValue {
                    name: name.clone(),
                    value: value.clone(),
                    span,
                };
                unknown.push(UnknownSetting {
                    name,
                    value: Some(value),
                    span,
                });
            }
        }
    }
}

/// The written form of a member under an unknown key, as the pinned OverPy
/// serializes it: scalars as `key: value`, lists as a block of bare lines,
/// and objects as a block of their members.
fn written_form(node: workshop_rs::settings::SettingsNode) -> workshop_rs::settings::SettingsNode {
    use workshop_rs::settings::SettingsNode;

    match node {
        SettingsNode::List {
            name,
            elements,
            span,
        } => SettingsNode::Group {
            name,
            children: elements
                .into_iter()
                .map(|element| SettingsNode::Raw {
                    name: element.value,
                    value: String::new(),
                    span: element.span,
                })
                .collect(),
            span,
        },
        SettingsNode::Group {
            name,
            children,
            span,
        } => SettingsNode::Group {
            name,
            children: children.into_iter().map(written_form).collect(),
            span,
        },
        node => match scalar_text(&node) {
            Some(value) => SettingsNode::RawValue {
                name: node.name().to_string(),
                value,
                span: node.span(),
            },
            None => node,
        },
    }
}

/// The fully verbatim form of a member: like [`written_form`], but a scalar
/// becomes `Raw` so its name is written as authored even when the key
/// resolves in the catalog.
fn verbatim_form(node: workshop_rs::settings::SettingsNode) -> workshop_rs::settings::SettingsNode {
    use workshop_rs::settings::SettingsNode;

    match scalar_text(&node) {
        Some(value) => SettingsNode::Raw {
            name: node.name().to_string(),
            value,
            span: node.span(),
        },
        None => written_form(node),
    }
}

/// A scalar value as JavaScript's `String(value)` writes it.
fn scalar_text(node: &workshop_rs::settings::SettingsNode) -> Option<String> {
    use workshop_rs::settings::SettingsNode;

    match node {
        SettingsNode::Number { value, .. } => Some(super::number_format::javascript_text(*value)),
        SettingsNode::Bool { value, .. } => Some(value.to_string()),
        SettingsNode::String { value, .. } => Some(value.clone()),
        _ => None,
    }
}

pub(super) fn merge_extensions(
    settings: Option<crate::hir::Settings>,
    directives: &[crate::hir::DirectiveRecord],
) -> Result<Option<workshop_rs::settings::Settings>, IntegrationError> {
    let mut extension_nodes: Vec<workshop_rs::settings::SettingsNode> = Vec::new();
    for directive in directives
        .iter()
        .filter(|directive| directive.name == "extension")
    {
        let Some(name) = directive.value.as_deref() else {
            return Err(IntegrationError::new(
                "directive-invalid",
                "`#!extension` expects one argument",
                directive.span,
            ));
        };
        let span = directive.span;
        if extension_nodes.iter().any(|node| node.name() == name) {
            continue;
        }
        let path = [
            workshop_rs::settings::PathPart::Part("extensions"),
            workshop_rs::settings::PathPart::Part(name),
        ];
        let Some(definition) = workshop_rs::settings::definition(&path) else {
            return Err(IntegrationError::new(
                "directive-invalid",
                format!("unknown Workshop extension `{name}`"),
                span,
            ));
        };
        if !matches!(
            definition.domain(),
            workshop_rs::settings::SettingValueDomain::PresenceOnly
        ) {
            return Err(IntegrationError::new(
                "directive-invalid",
                format!("Workshop extension `{name}` is not a presence-only setting"),
                span,
            ));
        }
        extension_nodes.push(workshop_rs::settings::SettingsNode::Flag {
            name: name.to_string(),
            span: span.map(convert_settings_span),
        });
    }
    if extension_nodes.is_empty() {
        return Ok(settings.map(convert_settings));
    }

    let mut settings = settings
        .map(convert_settings)
        .unwrap_or(workshop_rs::settings::Settings {
            span: None,
            children: Vec::new(),
        });
    if let Some(workshop_rs::settings::SettingsNode::Group { children, .. }) = settings
        .children
        .iter_mut()
        .find(|node| node.name() == "extensions")
    {
        for extension in extension_nodes {
            if !children.iter().any(|node| node.name() == extension.name()) {
                children.push(extension);
            }
        }
    } else {
        settings
            .children
            .push(workshop_rs::settings::SettingsNode::Group {
                name: "extensions".to_string(),
                children: extension_nodes,
                span: None,
            });
    }
    Ok(Some(settings))
}

pub(super) fn expand_settings_constants(
    settings: crate::hir::Settings,
    constants: &HashMap<String, &Expr>,
) -> crate::hir::Settings {
    crate::hir::Settings {
        span: settings.span,
        children: settings
            .children
            .into_iter()
            .map(|node| expand_settings_node(node, constants))
            .collect(),
    }
}

fn expand_settings_node(
    node: crate::hir::SettingsNode,
    constants: &HashMap<String, &Expr>,
) -> crate::hir::SettingsNode {
    use crate::hir::SettingsNode;
    match node {
        SettingsNode::Group {
            name,
            children,
            span,
        } => {
            let children = children
                .into_iter()
                .map(|child| expand_settings_node(child, constants))
                .collect::<Vec<_>>();
            SettingsNode::Group {
                name,
                children,
                span,
            }
        }
        SettingsNode::Raw { name, value, span } => constants
            .get(&value)
            .and_then(|expr| settings_node_from_expr(name.clone(), expr, constants, span))
            .unwrap_or(SettingsNode::Raw { name, value, span }),
        node => node,
    }
}

fn settings_node_from_expr(
    name: String,
    expr: &Expr,
    constants: &HashMap<String, &Expr>,
    span: Option<HirSpan>,
) -> Option<crate::hir::SettingsNode> {
    use crate::hir::SettingsNode;
    match expr {
        Expr::Constant { name: value, .. } => constants
            .get(value)
            .and_then(|expr| settings_node_from_expr(name, expr, constants, span)),
        Expr::Dict { entries, .. } => Some(SettingsNode::Group {
            name,
            children: entries
                .iter()
                .filter_map(|entry| {
                    let Expr::String {
                        value: child_name, ..
                    } = entry.key.as_ref()
                    else {
                        return None;
                    };
                    settings_node_from_expr(child_name.clone(), &entry.value, constants, entry.span)
                })
                .collect(),
            span,
        }),
        Expr::Number { value, .. } => Some(SettingsNode::Number {
            name,
            value: *value,
            span,
        }),
        Expr::Bool { value, .. } => Some(SettingsNode::Bool {
            name,
            value: *value,
            span,
        }),
        Expr::String { value, .. } => Some(SettingsNode::String {
            name,
            value: value.clone(),
            span,
        }),
        _ => None,
    }
}

pub(super) fn convert_settings(settings: crate::hir::Settings) -> workshop_rs::settings::Settings {
    workshop_rs::settings::Settings {
        span: settings.span.map(convert_settings_span),
        children: settings
            .children
            .into_iter()
            .map(convert_settings_node)
            .collect(),
    }
}

fn convert_settings_node(node: crate::hir::SettingsNode) -> workshop_rs::settings::SettingsNode {
    use crate::hir::SettingsNode as SourceNode;
    use workshop_rs::settings::{SettingsListElement, SettingsNode as TargetNode};

    match node {
        SourceNode::Group {
            name,
            children,
            span,
        } if name == "workshop" => TargetNode::Workshop {
            children: children.into_iter().map(convert_workshop_node).collect(),
            span: span.map(convert_settings_span),
        },
        SourceNode::Group {
            name,
            children,
            span,
        } => TargetNode::Group {
            children: children
                .into_iter()
                .map(|child| escape_main_string(&name, child))
                .map(convert_settings_node)
                .collect(),
            name,
            span: span.map(convert_settings_span),
        },
        SourceNode::Number { name, value, span } => TargetNode::Number {
            name,
            value,
            span: span.map(convert_settings_span),
        },
        SourceNode::Bool { name, value, span } => TargetNode::Bool {
            name,
            value,
            span: span.map(convert_settings_span),
        },
        SourceNode::String { name, value, span } => TargetNode::String {
            name: name.clone(),
            value: if name == "mapRotation" && value == "afterGame" {
                "afterAGame".to_string()
            } else {
                value
            },
            span: span.map(convert_settings_span),
        },
        SourceNode::Raw { name, value, span } => TargetNode::Raw {
            name,
            value,
            span: span.map(convert_settings_span),
        },
        SourceNode::List {
            name,
            elements,
            span,
        } => TargetNode::List {
            name,
            elements: elements
                .into_iter()
                .map(|element| SettingsListElement {
                    value: element.value,
                    span: element.span.map(convert_settings_span),
                })
                .collect(),
            span: span.map(convert_settings_span),
        },
    }
}

/// `main.modeName` and `main.description` are the settings strings the pinned
/// OverPy escapes for filtered words.
fn escape_main_string(group: &str, node: crate::hir::SettingsNode) -> crate::hir::SettingsNode {
    use crate::hir::SettingsNode;
    match node {
        SettingsNode::String { name, value, span }
            if group == "main" && matches!(name.as_str(), "modeName" | "description") =>
        {
            SettingsNode::String {
                value: super::lowering::escape_bad_words(&value),
                name,
                span,
            }
        }
        node => node,
    }
}

fn convert_workshop_node(node: crate::hir::SettingsNode) -> workshop_rs::settings::SettingsNode {
    use crate::hir::SettingsNode as SourceNode;
    use workshop_rs::settings::SettingsNode as TargetNode;

    match node {
        SourceNode::Group {
            name,
            children,
            span,
        } => TargetNode::Group {
            name,
            children: children.into_iter().map(convert_workshop_node).collect(),
            span: span.map(convert_settings_span),
        },
        SourceNode::Number { name, value, span } => TargetNode::Raw {
            name,
            value: number_format::javascript_text(value),
            span: span.map(convert_settings_span),
        },
        SourceNode::Bool { name, value, span } => TargetNode::Raw {
            name,
            value: value.to_string(),
            span: span.map(convert_settings_span),
        },
        SourceNode::String { name, value, span } | SourceNode::Raw { name, value, span } => {
            TargetNode::Raw {
                name,
                value,
                span: span.map(convert_settings_span),
            }
        }
        SourceNode::List {
            name,
            elements,
            span,
        } => TargetNode::Raw {
            name,
            value: format!(
                "[{}]",
                elements
                    .into_iter()
                    .map(|element| element.value)
                    .collect::<Vec<_>>()
                    .join(", ")
            ),
            span: span.map(convert_settings_span),
        },
    }
}

fn convert_settings_span(span: HirSpan) -> WorkshopSpan {
    WorkshopSpan::new(
        workshop_rs::source::FileId::from_index(span.file as usize),
        WorkshopPosition::new(span.start.line, span.start.col),
        WorkshopPosition::new(span.end.line, span.end.col),
    )
}