Skip to main content

workshop_rs/output/
emitter.rs

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