Skip to main content

workshop_rs/output/
emitter.rs

1//! Deterministic localized Workshop emitter.
2//!
3//! Serializes validated public Workshop programs into localized Workshop text with a
4//! selectable output locale. Canonical catalog identities resolve to
5//! locale-specific spellings; missing target-locale mappings fail explicitly
6//! with a [`WorkshopError::MissingMapping`] diagnostic — never a guess, never
7//! a silent passthrough of another locale's spelling. Fallback to another
8//! declared locale is opt-in ([`EmitOptions`]) and every fell-back identity
9//! is recorded in [`EmitOutput::fallback_ids`].
10
11pub(crate) use std::fmt::Write;
12
13pub(crate) use crate::catalog::{Catalog, Kind, Locale};
14pub(crate) use crate::core::error::{Result, WorkshopError};
15pub(crate) use crate::core::format::format_number;
16pub(crate) use crate::settings::table::KeyKind;
17pub(crate) use crate::settings::{PathPart, table};
18pub(crate) use crate::settings::{Settings as SettingsTree, SettingsNode};
19pub(crate) use crate::wir;
20
21/// Emission options: opt-in fallback for missing target-locale mappings.
22#[derive(Debug, Clone, Default, PartialEq, Eq)]
23pub struct EmitOptions {
24    /// When a canonical identity has no spelling for the target locale, its
25    /// spelling in this declared locale is used instead. `None` (the default)
26    /// keeps missing mappings failing explicitly. The fallback choice is
27    /// visible in [`EmitOutput::fallback_ids`].
28    pub fallback_locale: Option<Locale>,
29}
30
31/// The result of a localized emission.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct EmitOutput {
34    /// The emitted localized Workshop text.
35    pub text: String,
36    /// Canonical identities (and the `settings` marker) whose spelling came
37    /// from the opt-in fallback locale instead of the target locale. Empty
38    /// when no fallback occurred.
39    pub fallback_ids: Vec<String>,
40}
41
42/// Emit a public Workshop program as localized Workshop text, failing explicitly
43/// on any missing target-locale mapping (no fallback).
44pub fn emit(program: &crate::Program, catalog: &Catalog, locale: &Locale) -> Result<String> {
45    emit_with_options(program, catalog, locale, &EmitOptions::default()).map(|out| out.text)
46}
47
48#[cfg(test)]
49pub(crate) fn emit_wir(
50    program: &wir::Program,
51    catalog: &Catalog,
52    locale: &Locale,
53) -> Result<String> {
54    emit_with_options_inner(program, catalog, locale, &EmitOptions::default(), false)
55        .map(|out| out.text)
56}
57
58/// Emit a public Workshop program as localized Workshop text with emission
59/// options (opt-in fallback locale).
60pub fn emit_with_options(
61    program: &crate::Program,
62    catalog: &Catalog,
63    locale: &Locale,
64    options: &EmitOptions,
65) -> Result<EmitOutput> {
66    let storage = program.to_wir()?;
67    emit_with_options_inner(&storage, catalog, locale, options, false)
68}
69
70pub(crate) fn emit_with_options_for_conversion(
71    program: &crate::Program,
72    catalog: &Catalog,
73    locale: &Locale,
74    options: &EmitOptions,
75) -> Result<EmitOutput> {
76    let storage = program.to_wir()?;
77    emit_with_options_inner(&storage, catalog, locale, options, true)
78}
79
80#[cfg(test)]
81pub(crate) fn emit_wir_with_options(
82    program: &wir::Program,
83    catalog: &Catalog,
84    locale: &Locale,
85    options: &EmitOptions,
86) -> Result<EmitOutput> {
87    emit_with_options_inner(program, catalog, locale, options, false)
88}
89
90fn emit_with_options_inner(
91    program: &wir::Program,
92    catalog: &Catalog,
93    locale: &Locale,
94    options: &EmitOptions,
95    force_hero_constructors: bool,
96) -> Result<EmitOutput> {
97    let mut emitter = EmitContext {
98        program,
99        catalog,
100        locale: locale.clone(),
101        fallback: options.fallback_locale.clone(),
102        force_hero_constructors,
103        fallback_ids: Vec::new(),
104        out: String::new(),
105        line_count: 0,
106    };
107    emitter.run()?;
108    Ok(EmitOutput {
109        text: emitter.out,
110        fallback_ids: emitter.fallback_ids,
111    })
112}
113
114pub(crate) struct EmitContext<'a> {
115    pub(crate) program: &'a wir::Program,
116    pub(crate) catalog: &'a Catalog,
117    pub(crate) locale: Locale,
118    /// The opt-in fallback locale for missing target-locale mappings.
119    pub(crate) fallback: Option<Locale>,
120    /// Canonical ids emitted with a fallback-locale spelling.
121    pub(crate) fallback_ids: Vec<String>,
122    pub(crate) force_hero_constructors: bool,
123    pub(crate) out: String,
124    pub(crate) line_count: usize,
125}
126
127impl EmitContext<'_> {
128    pub(crate) fn run(&mut self) -> Result<()> {
129        // Section order: settings, variables, subroutines, rules.
130        if let Some(settings) = &self.program.settings {
131            self.emit_settings(settings)?;
132            self.out.push('\n');
133        }
134        if !self.program.global_variables.is_empty() || !self.program.player_variables.is_empty() {
135            let variables = self.structural("variables")?;
136            self.line(0, &format!("{variables} {{"))?;
137            if !self.program.global_variables.is_empty() {
138                let global = self.structural("global")?;
139                self.line(1, &format!("{global}:"))?;
140                for variable in self.program.global_variables.iter() {
141                    self.line(2, &format!("{}: {}", variable.index, variable.name))?;
142                }
143            }
144            if !self.program.player_variables.is_empty() {
145                let player = self.structural("player")?;
146                self.line(1, &format!("{player}:"))?;
147                for variable in self.program.player_variables.iter() {
148                    self.line(2, &format!("{}: {}", variable.index, variable.name))?;
149                }
150            }
151            self.line(0, "}")?;
152            self.out.push('\n');
153        }
154        if !self.program.subroutines.is_empty() {
155            let subroutines = self.structural("subroutines")?;
156            self.line(0, &format!("{subroutines} {{"))?;
157            for subroutine in self.program.subroutines.iter() {
158                self.line(1, &format!("{}: {}", subroutine.index, subroutine.name))?;
159            }
160            self.line(0, "}")?;
161            self.out.push('\n');
162        }
163        for (emitted_rules, rule) in self.program.rules.iter().enumerate() {
164            if emitted_rules > 0 {
165                self.out.push('\n');
166            }
167            self.rule(rule)?;
168        }
169        // The oracle's raw artifact ends with a trailing blank line (the
170        // committed snapshots strip it via the acquisition normalizer; the
171        // pinned oracle's own output keeps it).
172        if !self.out.is_empty() && !self.out.ends_with("\n\n") {
173            self.out.push('\n');
174        }
175        Ok(())
176    }
177
178    pub(crate) fn malformed(&self, message: impl Into<String>) -> WorkshopError {
179        WorkshopError::Malformed {
180            message: message.into(),
181            span: None,
182        }
183    }
184
185    pub(crate) fn indent(&mut self, level: usize) {
186        for _ in 0..level {
187            self.out.push_str("    ");
188        }
189    }
190
191    pub(crate) fn line(&mut self, level: usize, text: &str) -> Result<()> {
192        self.indent(level);
193        self.out.push_str(text);
194        self.out.push('\n');
195        self.line_count += 1;
196        Ok(())
197    }
198}
199
200/// Format a float like the reference frontend: integers print without a
201/// decimal point, and non-integers print the shortest round-trip
202/// representation truncated to 16 significant digits (OverPy behavior;
203/// evidence: the pinned oracle snapshots).
204pub(crate) fn is_comparison_operator(name: &str) -> bool {
205    matches!(name, "==" | "!=" | "<" | "<=" | ">" | ">=")
206}
207
208pub(crate) fn escape_string(value: &str) -> String {
209    value.replace('"', "\\\"")
210}
211
212/// Re-escape a decoded value string the way the pinned oracle does (#87):
213/// `\`, `"`, newline, and carriage return re-escape; tabs pass through raw
214/// (byte-measured oracle behavior: `a\tb` emits a real tab, `a\nb` emits the
215/// literal two-character `\n`).
216pub(crate) fn escape_value_string(value: &str) -> String {
217    let mut out = String::with_capacity(value.len());
218    for ch in value.chars() {
219        match ch {
220            '\\' => out.push_str("\\\\"),
221            '"' => out.push_str("\\\""),
222            '\n' => out.push_str("\\n"),
223            '\r' => out.push_str("\\r"),
224            other => out.push(other),
225        }
226    }
227    out
228}
229
230/// Split a decoded string per the oracle's long-string rule (#87): when the
231/// decoded length exceeds the Workshop 128-char limit, non-final segments
232/// hold exactly 125 decoded chars and are emitted with a `{0}` continuation
233/// placeholder (128 total text chars), chained as nested `Custom String`
234/// arguments; the final segment holds the remainder without a placeholder.
235/// Segment texts are re-escaped. Byte-measured basis: chunk sizes are
236/// counted on the decoded string (70 escaped newlines — 140 escaped chars,
237/// 70 decoded — emit unsplit; 129 decoded newlines split at 125 decoded).
238pub(crate) fn split_string(value: &str) -> Vec<String> {
239    if value.chars().count() <= 128 {
240        return vec![escape_value_string(value)];
241    }
242    let mut segments = Vec::new();
243    let mut rest = value;
244    while rest.chars().count() > 125 {
245        let chunk: String = rest.chars().take(125).collect();
246        let mut text = escape_value_string(&chunk);
247        text.push_str("{0}");
248        segments.push(text);
249        rest = &rest[chunk.len()..];
250    }
251    if !rest.is_empty() {
252        segments.push(escape_value_string(rest));
253    }
254    segments
255}
256
257/// Escape a settings string value the way the pinned oracle does: every
258/// decode the JSONC parser performed is re-escaped, so decoded values
259/// round-trip to the oracle's spelling. Evidence: the inputhud description
260/// (`\n` in the source block) is emitted by the oracle as the literal
261/// two-character sequence `\n` in the Workshop settings section.
262pub(crate) fn escape_settings_string(value: &str) -> String {
263    let mut out = String::with_capacity(value.len());
264    for ch in value.chars() {
265        match ch {
266            '\\' => out.push_str("\\\\"),
267            '"' => out.push_str("\\\""),
268            '\n' => out.push_str("\\n"),
269            '\t' => out.push_str("\\t"),
270            '\r' => out.push_str("\\r"),
271            other => out.push(other),
272        }
273    }
274    out
275}
276
277/// Emit the nested continuation chain
278/// `Custom String(seg0, Custom String(seg1, ...))`; segment texts are
279/// pre-escaped, non-final segments carry the `{0}` placeholder. Iterative:
280/// every segment except the first opens a `Custom String` level, then all
281/// levels close.
282pub(crate) fn emit_string_chain(spelling: &str, segments: &[String], out: &mut String) {
283    let Some((first, rest)) = segments.split_first() else {
284        return;
285    };
286    out.push_str(spelling);
287    out.push('(');
288    write!(out, "\"{first}\"").unwrap();
289    for segment in rest {
290        out.push_str(", ");
291        out.push_str(spelling);
292        out.push('(');
293        write!(out, "\"{segment}\"").unwrap();
294    }
295    for _ in 0..=rest.len() {
296        out.push(')');
297    }
298}
299
300/// Render a constant format argument the way the oracle folds it: integers
301/// without decimals, non-integers with exactly two decimals (JS `toFixed(2)`
302/// rounding: `0.5` -> `0.50`, `0.125` -> `0.13`, #87).
303pub(crate) fn fold_number(value: f64) -> String {
304    if value.fract() == 0.0 && value.abs() < 1e15 {
305        format!("{}", value as i64)
306    } else {
307        let scaled = (value * 100.0).round();
308        let sign = if scaled < 0.0 { "-" } else { "" };
309        let scaled = scaled.abs() as i64;
310        format!("{sign}{}.{:02}", scaled / 100, scaled % 100)
311    }
312}