yog-cli 0.49.2

Yog build tool — `yog build` compiles a mod and packages it as .yog.
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
//! One element of a view-model screen: its attributes read (states,
//! bindings, commands), its interactive parts and actions, its bindings'
//! update code, its children.

mod consts;
use consts as st;

use std::collections::HashMap;
use std::fmt::Write as _;

use yog_ids::yui as y;
use yog_ids::yui_attr as at;
use yog_ids::yui_vm as v;

use super::bind::Spot;
use super::binding::{Binding, Bound, Mode, WriteBack, parse_binding};
use super::expr::{Ex, Head, Scope};
use super::state::{Ctx, SCREEN_OWNER, VmGen, path_lit};
use crate::yui::{self, Element, RAW_CALL, err};
use yog_data::Diagnostic;

/// The states an attribute may be prefixed with, and their flags.
const STATES: &[(&str, &str)] = &[
    (st::HOVER, "HOVER"),
    (st::PRESSED, "PRESSED"),
    (st::FOCUSED, "FOCUSED"),
    (st::SELECTED, "SELECTED"),
    (st::CHECKED, "CHECKED"),
    (st::DISABLED, "DISABLED"),
];
/// What separates a state from the attribute it changes: `hover:bg`.
const STATE_SEP: char = ':';
/// The bindings that feed a list rather than set a widget.
const LIST_BINDINGS: [&str; 4] = [v::ITEMS, v::CONTENT, v::KEY, at::SELECTED];
/// The view-model screen's own attributes, not a widget's.
const SCREEN_ATTRS: [&str; 3] = [at::X_VIEW_MODEL, at::X_CREATE, at::X_DATA_TYPE];
/// What `<Image>` and `<Surface>` show until a bound source is set — valid
/// for the static checks.
const IMAGE_STAND_IN: &str = "item:air";
const SURFACE_STAND_IN: &str = "yog:none";

/// What an element's attributes come to.
struct Read {
    /// The static element: its plain attributes and builder calls.
    out: Element,
    raw: usize,
    /// State style patches: flag → fields.
    patches: Vec<(&'static str, Vec<String>)>,
    bindings: Vec<(String, Binding)>,
    command: Option<Binding>,
    list_attrs: HashMap<String, String>,
    /// Whether the element is a list.
    is_list: bool,
}

impl Read {
    /// A builder call on the static element.
    fn push_raw(&mut self, call: String) {
        self.out
            .attrs
            .push((format!("{RAW_CALL}{}", self.raw), call));
        self.raw += 1;
    }
}

/// The built-in element `el` stands for, and whether it's a list.
fn kind_of(el: &Element) -> Result<(&str, bool), Diagnostic> {
    let name = el.name.as_str();
    let kind = match name {
        y::TOGGLE_BUTTON => y::BUTTON,
        y::LIST_VIEW => y::SCROLL_VIEW,
        y::ITEMS_CONTROL
            if el
                .attr(at::ORIENTATION)
                .is_some_and(|o| o.trim() == v::HORIZONTAL) =>
        {
            y::ROW
        }
        y::ITEMS_CONTROL | y::CONTENT_PRESENTER => y::COLUMN,
        v::SCREEN_RESOURCES | v::DATA_TEMPLATE => return err(el, "isn't allowed here"),
        k => k,
    };
    Ok((
        kind,
        matches!(name, y::LIST_VIEW | y::ITEMS_CONTROL | y::CONTENT_PRESENTER),
    ))
}

impl VmGen<'_> {
    /// Compiles `el` at `path` in `ctx`; returns the static element.
    pub(super) fn element(
        &mut self,
        ctx: &mut Ctx,
        el: &Element,
        path: &[u16],
        items: Option<&str>,
    ) -> Result<Element, Diagnostic> {
        let el = &self.resolve(el)?;
        let (kind, is_list) = kind_of(el)?;
        let mut r = self.read_attrs(el, kind, is_list)?;
        for (flag, fields) in std::mem::take(&mut r.patches) {
            r.push_raw(format!("state_style(::yog_api::ui::StateFlags::{flag}, ::yog_api::ui::StylePatch {{ {}, ..::std::default::Default::default() }})", fields.join(", ")));
        }
        if let Some(id) = r.out.attr(at::ID) {
            self.ids.push(id);
        }
        let spot = self.interactive(ctx, el, kind, path)?;
        self.actions(ctx, el, &spot, &mut r, items)?;
        if is_list {
            if ctx.scope != Scope::Screen {
                return err(el, "lists inside item templates aren't supported");
            }
            self.list(ctx, el, &r.bindings, &r.list_attrs, &spot)?;
        }
        self.bindings(ctx, el, &spot, &mut r)?;
        if !is_list {
            self.children(ctx, el, path, items, &mut r.out)?;
        }
        Ok(r.out)
    }

    /// Reads `el`'s attributes: state paints, bindings and the command,
    /// list attributes, the rest as they are.
    fn read_attrs(&self, el: &Element, kind: &str, is_list: bool) -> Result<Read, Diagnostic> {
        let out = Element {
            name: kind.to_string(),
            attrs: Vec::new(),
            children: Vec::new(),
            line: el.line,
        };
        let mut r = Read {
            out,
            raw: 0,
            patches: Vec::new(),
            bindings: Vec::new(),
            command: None,
            list_attrs: HashMap::new(),
            is_list,
        };
        for (k, val) in &el.attrs {
            let k = k.as_str();
            if k.starts_with(yog_ids::yui_markup::DESIGN) || SCREEN_ATTRS.contains(&k) {
                continue;
            }
            if matches!(k, at::ON_CLICK | at::ON_CHANGE | at::ON_SUBMIT) {
                return err(
                    el,
                    format!(
                        "{k}: view-model screens use command=\"{{x:Bind method()}}\" (and Mode=TwoWay text) instead"
                    ),
                );
            }
            if let Some((state, attr)) = k.split_once(STATE_SEP) {
                let (flag, field) = self.state_paint(el, k, state, attr, val)?;
                match r.patches.iter_mut().find(|(f, _)| *f == flag) {
                    Some((_, fs)) => fs.push(field),
                    None => r.patches.push((flag, vec![field])),
                }
                continue;
            }
            match parse_binding(val) {
                Some(b) => read_binding(
                    el,
                    &mut r,
                    k,
                    b.map_err(|e| Diagnostic::from(format!("{e}")).at_line(el.line))?,
                )?,
                None => plain_attr(el, &mut r, k, val)?,
            }
        }
        Ok(r)
    }

    /// A state-prefixed paint (`hover:bg`): the state's flag, and the style
    /// patch's field.
    fn state_paint(
        &self,
        el: &Element,
        k: &str,
        state: &str,
        attr: &str,
        val: &str,
    ) -> Result<(&'static str, String), Diagnostic> {
        let Some(&(_, flag)) = STATES.iter().find(|(s, _)| *s == state) else {
            return err(
                el,
                format!(
                    "unknown attribute prefix `{state}:` (states: hover, pressed, focused, selected, checked, disabled)"
                ),
            );
        };
        let field = match attr {
            at::BG if yui::is_image_value(val) => {
                let src = yui::image_source(el, k, val.trim(), self.mod_id)?;
                format!(
                    "bg_image: ::std::option::Option::Some(::std::string::String::from({src:?}))"
                )
            }
            at::BG | at::COLOR | at::FOCUS_COLOR => format!(
                "{attr}: ::std::option::Option::Some(::yog_api::ui::Color(0x{:08X}))",
                yui::color_attr(el, k, val)?
            ),
            at::SHADOW => format!(
                "{attr}: ::std::option::Option::Some({})",
                yui::boolean(el, k, val)?
            ),
            _ => {
                return err(
                    el,
                    format!(
                        "{k}: only paint attributes (bg, color, shadow, focus_color) take a state prefix"
                    ),
                );
            }
        };
        Ok((flag, field))
    }

    /// The view's text box and scroller `el` is (screen scope only), and
    /// where it is.
    fn interactive<'s>(
        &mut self,
        ctx: &Ctx,
        el: &Element,
        kind: &'s str,
        path: &'s [u16],
    ) -> Result<Spot<'s>, Diagnostic> {
        let screen = ctx.scope == Scope::Screen;
        let text_box = (kind == y::TEXT_BOX && screen).then(|| {
            let _ = writeln!(self.parts, "        parts.text_box({});", path_lit(path));
            self.n_text_boxes += 1;
            self.n_text_boxes - 1
        });
        if kind == y::SCROLL_VIEW && !screen {
            return err(el, "scroll views inside item templates aren't supported");
        }
        let scroller = (kind == y::SCROLL_VIEW).then(|| {
            let _ = writeln!(self.parts, "        parts.scroller({});", path_lit(path));
            self.n_scrollers += 1;
            self.n_scrollers - 1
        });
        Ok(Spot {
            kind,
            path,
            text_box,
            scroller,
        })
    }

    /// A text box's two-way text and submit command; any other element's
    /// command and two-way toggle, as an action.
    fn actions(
        &mut self,
        ctx: &Ctx,
        el: &Element,
        spot: &Spot,
        r: &mut Read,
        items: Option<&str>,
    ) -> Result<(), Diagnostic> {
        if spot.kind == y::TEXT_BOX {
            return self.text_box_events(el, spot, r);
        }
        let toggle = r
            .bindings
            .iter()
            .find(|(k, b)| k == at::CHECKED && b.mode == Mode::TwoWay)
            .map(|(_, b)| b.clone());
        if r.command.is_none() && toggle.is_none() {
            return Ok(());
        }
        let owner = ctx.owner;
        let slot = self.next_cmd(owner);
        r.push_raw(format!(
            "action(::yog_api::ui::Action {{ owner: {owner}, slot: {slot}, index: 0 }})"
        ));
        let mut body = String::new();
        if let Some(t) = &toggle {
            if ctx.scope == Scope::Template {
                return err(
                    el,
                    "Mode=TwoWay toggles in item templates aren't supported — use a command",
                );
            }
            body.push_str(&toggled(el, t)?);
        }
        if let Some(c) = &r.command {
            body.push_str(&self.command(el, c, ctx.scope, items)?);
        }
        let index = if owner == SCREEN_OWNER { ".." } else { "index" };
        let pat = format!(
            "::yog_kit::ViewEvent::Action(::yog_api::ui::Action {{ owner: {owner}, slot: {slot}, {index} }})"
        );
        self.dispatch.push((pat, body.trim_end().to_string()));
        if ctx.scope == Scope::Screen {
            let _ = writeln!(
                self.parts,
                "        parts.focusable({});",
                path_lit(spot.path)
            );
        }
        Ok(())
    }

    /// A text box's events: its text written back, its command on submit.
    fn text_box_events(&mut self, el: &Element, spot: &Spot, r: &Read) -> Result<(), Diagnostic> {
        let tb = spot.text_box.ok_or_else(|| {
            Diagnostic::from("text boxes in item templates aren't supported".to_string())
                .at_line(el.line)
        })?;
        if let Some((_, b)) = r.bindings.iter().find(|(k, _)| k == at::TEXT) {
            match b.write_back("text", "text")? {
                WriteBack::Stmt(write) => self.dispatch.push((
                    format!("::yog_kit::ViewEvent::TextChanged {{ slot: {tb}, text }}"),
                    write,
                )),
                WriteBack::Nowhere => {
                    return err(
                        el,
                        "Mode=TwoWay needs a field (text=\"{x:Bind search, Mode=TwoWay}\") or BindBack=method",
                    );
                }
                WriteBack::OneWay => {}
            }
        }
        if let Some(c) = &r.command {
            let stmt = self.command(el, c, Scope::Screen, None)?;
            self.dispatch.push((
                format!("::yog_kit::ViewEvent::TextSubmitted {{ slot: {tb}, text }}"),
                format!("let _ = text; {stmt}"),
            ));
        }
        Ok(())
    }

    /// The element's bindings: a component's properties rebuilding it,
    /// a bound source's stand-in, the rest applied one by one.
    fn bindings(
        &mut self,
        ctx: &mut Ctx,
        el: &Element,
        spot: &Spot,
        r: &mut Read,
    ) -> Result<(), Diagnostic> {
        // A component's bound properties: when any changes, the instance is
        // rebuilt with the new values (then its other bindings re-applied).
        let rebuild = if yui::is_custom_component(spot.kind)
            && r.bindings.iter().any(|(k, _)| !yui::is_common_attr(k))
        {
            Some(self.component_props(ctx, el, &r.out, &mut r.bindings, spot.path)?)
        } else {
            None
        };
        let slots_before = self.n_slots;
        if r.bindings.iter().any(|(k, _)| k == at::SOURCE) {
            let stand_in = match spot.kind {
                y::IMAGE => Some(IMAGE_STAND_IN),
                y::SURFACE => Some(SURFACE_STAND_IN),
                _ => None,
            };
            r.out
                .attrs
                .extend(stand_in.map(|s| (at::SOURCE.to_string(), s.to_string())));
        }
        for (k, b) in r
            .bindings
            .iter()
            .filter(|(k, _)| !LIST_BINDINGS.contains(&k.as_str()))
        {
            self.bind_attr(ctx, el, spot, k, b)?;
        }
        if let Some(marker) = rebuild {
            let resets: String = (slots_before..self.n_slots)
                .map(|i| format!(" {}b{i}.reset();", ctx.fields))
                .collect();
            ctx.update = ctx.update.replace(&marker, &resets);
        }
        Ok(())
    }

    /// The element's children, each at its path; the screen's resources left out.
    fn children(
        &mut self,
        ctx: &mut Ctx,
        el: &Element,
        path: &[u16],
        items: Option<&str>,
        out: &mut Element,
    ) -> Result<(), Diagnostic> {
        let mut i = 0u16;
        for child in &el.children {
            if child.name.contains(yog_ids::sep::MEMBER) {
                if child.name == v::SCREEN_RESOURCES && path.is_empty() {
                    continue;
                }
                return err(child, "isn't allowed here");
            }
            let p: Vec<u16> = path.iter().copied().chain([i]).collect();
            out.children.push(self.element(ctx, child, &p, items)?);
            i += 1;
        }
        Ok(())
    }
}

/// A two-way toggle's statement: its field flipped, or its `BindBack`
/// method called with the flipped value.
fn toggled(el: &Element, t: &Binding) -> Result<String, Diagnostic> {
    match (&t.bind_back, t.expr()) {
        (Some(m), Some(e)) => Ok(format!(
            "let __v = !{}; vm.{m}(__v); ",
            e.emit(Scope::Screen, false)?
        )),
        (None, Some(e)) if e.is_place() => {
            let p = e.emit(Scope::Screen, false)?;
            Ok(format!("{p} = !{p}; "))
        }
        _ => err(
            el,
            "checked Mode=TwoWay needs a bool field or BindBack=method",
        ),
    }
}

/// A bound attribute: the command, a list's binding (on a list only), or
/// one to apply.
fn read_binding(el: &Element, r: &mut Read, k: &str, b: Binding) -> Result<(), Diagnostic> {
    if k == v::COMMAND {
        r.command = Some(b);
        return Ok(());
    }
    if LIST_BINDINGS.contains(&k) && !r.is_list {
        return err(
            el,
            format!("{k} is for ListView, ItemsControl and ContentPresenter"),
        );
    }
    r.bindings.push((k.to_string(), b));
    Ok(())
}

/// A plain attribute: a list's own, a fixed visibility or check, a
/// `.yres` text (bound, so it follows the language), or one as it is.
fn plain_attr(el: &Element, r: &mut Read, k: &str, val: &str) -> Result<(), Diagnostic> {
    let res_text =
        val.starts_with(yog_ids::sep::TYPED) && !val.starts_with(ESCAPED_AT) && val.len() > 1;
    match k {
        v::COMMAND => return err(el, "command needs {x:Bind method(...)}"),
        _ if LIST_BINDINGS.contains(&k) => {
            return err(el, format!("{k} needs a binding: {k}=\"{{x:Bind ...}}\""));
        }
        at::ITEM_HEIGHT | at::ORIENTATION if r.is_list => {
            r.list_attrs.insert(k.into(), val.to_string());
        }
        at::VISIBLE => {
            let b = yui::boolean(el, k, val)?;
            r.push_raw(format!("visible({b})"));
        }
        at::CHECKED if r.out.name == y::BUTTON => {
            let fixed = Ex::Post(Head::Lit(yui::boolean(el, k, val)?.to_string()), Vec::new());
            r.bindings
                .push((k.into(), Binding::one_way(val, Bound::Expr(fixed))));
        }
        at::TEXT | at::PLACEHOLDER | at::TOOLTIP if res_text => {
            // Looked up on every update, so it follows the language.
            r.bindings.push((
                k.into(),
                Binding::one_way(val, Bound::Rust(yui::text_attr(el, k, val)?)),
            ));
        }
        _ => r.out.attrs.push((k.to_string(), val.to_string())),
    }
    Ok(())
}

/// A literal `@` at a text's start: `@@`.
const ESCAPED_AT: &str = yog_ids::concatcp!(yog_ids::sep::TYPED, yog_ids::sep::TYPED);