pepl-ui 0.1.2

UI component model for the PEPL language
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
//! Interactive component builders — Button, TextInput.
//!
//! These are leaf components with no children. They handle user interactions
//! via action references (`on_tap`) or lambda callbacks (`on_change`).

use crate::accessibility;
use crate::prop_value::PropValue;
use crate::surface::SurfaceNode;

// ── Button Variant Enum ───────────────────────────────────────────────────────

/// Visual style for a Button.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonVariant {
    Filled,
    Outlined,
    Text,
}

impl ButtonVariant {
    fn as_str(self) -> &'static str {
        match self {
            Self::Filled => "filled",
            Self::Outlined => "outlined",
            Self::Text => "text",
        }
    }
}

// ── Keyboard Type Enum ────────────────────────────────────────────────────────

/// Virtual keyboard type for a TextInput.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyboardType {
    Text,
    Number,
    Email,
    Phone,
    Url,
}

impl KeyboardType {
    fn as_str(self) -> &'static str {
        match self {
            Self::Text => "text",
            Self::Number => "number",
            Self::Email => "email",
            Self::Phone => "phone",
            Self::Url => "url",
        }
    }
}

// ── ButtonBuilder ─────────────────────────────────────────────────────────────

/// Builder for a Button component.
///
/// Required: `label` (String), `on_tap` (ActionRef).
/// Optional: `variant`, `icon`, `disabled`, `loading`.
pub struct ButtonBuilder {
    label: String,
    on_tap: PropValue,
    variant: Option<ButtonVariant>,
    icon: Option<String>,
    disabled: Option<bool>,
    loading: Option<bool>,
}

impl ButtonBuilder {
    /// Create a new ButtonBuilder with required props.
    ///
    /// `on_tap` must be a `PropValue::ActionRef` — use `PropValue::action()` or
    /// `PropValue::action_with_args()`.
    pub fn new(label: impl Into<String>, on_tap: PropValue) -> Self {
        Self {
            label: label.into(),
            on_tap,
            variant: None,
            icon: None,
            disabled: None,
            loading: None,
        }
    }

    pub fn variant(mut self, variant: ButtonVariant) -> Self {
        self.variant = Some(variant);
        self
    }

    pub fn icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = Some(disabled);
        self
    }

    pub fn loading(mut self, loading: bool) -> Self {
        self.loading = Some(loading);
        self
    }

    pub fn build(self) -> SurfaceNode {
        let mut node = SurfaceNode::new("Button");
        node.set_prop("label", PropValue::String(self.label));
        node.set_prop("on_tap", self.on_tap);
        if let Some(variant) = self.variant {
            node.set_prop("variant", PropValue::String(variant.as_str().to_string()));
        }
        if let Some(icon) = self.icon {
            node.set_prop("icon", PropValue::String(icon));
        }
        if let Some(disabled) = self.disabled {
            node.set_prop("disabled", PropValue::Bool(disabled));
        }
        if let Some(loading) = self.loading {
            node.set_prop("loading", PropValue::Bool(loading));
        }
        accessibility::ensure_accessible(&mut node);
        node
    }
}

// ── TextInputBuilder ──────────────────────────────────────────────────────────

/// Builder for a TextInput component.
///
/// Required: `value` (String), `on_change` (Lambda).
/// Optional: `placeholder`, `label`, `keyboard`, `max_length`, `multiline`.
pub struct TextInputBuilder {
    value: String,
    on_change: PropValue,
    placeholder: Option<String>,
    label: Option<String>,
    keyboard: Option<KeyboardType>,
    max_length: Option<f64>,
    multiline: Option<bool>,
}

impl TextInputBuilder {
    /// Create a new TextInputBuilder with required props.
    ///
    /// `on_change` must be a `PropValue::Lambda` — use `PropValue::lambda(id)`.
    pub fn new(value: impl Into<String>, on_change: PropValue) -> Self {
        Self {
            value: value.into(),
            on_change,
            placeholder: None,
            label: None,
            keyboard: None,
            max_length: None,
            multiline: None,
        }
    }

    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.placeholder = Some(placeholder.into());
        self
    }

    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    pub fn keyboard(mut self, keyboard: KeyboardType) -> Self {
        self.keyboard = Some(keyboard);
        self
    }

    pub fn max_length(mut self, max_length: f64) -> Self {
        self.max_length = Some(max_length);
        self
    }

    pub fn multiline(mut self, multiline: bool) -> Self {
        self.multiline = Some(multiline);
        self
    }

    pub fn build(self) -> SurfaceNode {
        let mut node = SurfaceNode::new("TextInput");
        node.set_prop("value", PropValue::String(self.value));
        node.set_prop("on_change", self.on_change);
        if let Some(placeholder) = self.placeholder {
            node.set_prop("placeholder", PropValue::String(placeholder));
        }
        if let Some(label) = self.label {
            node.set_prop("label", PropValue::String(label));
        }
        if let Some(keyboard) = self.keyboard {
            node.set_prop("keyboard", PropValue::String(keyboard.as_str().to_string()));
        }
        if let Some(max_length) = self.max_length {
            node.set_prop("max_length", PropValue::Number(max_length));
        }
        if let Some(multiline) = self.multiline {
            node.set_prop("multiline", PropValue::Bool(multiline));
        }
        accessibility::ensure_accessible(&mut node);
        node
    }
}

// ── Validation ────────────────────────────────────────────────────────────────

/// Validate an interactive component node (Button or TextInput).
pub fn validate_interactive_node(node: &SurfaceNode) -> Vec<String> {
    match node.component_type.as_str() {
        "Button" => validate_button(node),
        "TextInput" => validate_text_input(node),
        _ => vec![format!(
            "Unknown interactive component: {}",
            node.component_type
        )],
    }
}

fn validate_button(node: &SurfaceNode) -> Vec<String> {
    let mut errors = Vec::new();

    // Required: label (string)
    match node.props.get("label") {
        Some(PropValue::String(_)) => {}
        Some(other) => errors.push(format!(
            "Button.label: expected string, got {}",
            other.type_name()
        )),
        None => errors.push("Button.label: required prop missing".to_string()),
    }

    // Required: on_tap (action)
    match node.props.get("on_tap") {
        Some(PropValue::ActionRef { .. }) => {}
        Some(other) => errors.push(format!(
            "Button.on_tap: expected action, got {}",
            other.type_name()
        )),
        None => errors.push("Button.on_tap: required prop missing".to_string()),
    }

    // Optional: variant (string enum)
    if let Some(prop) = node.props.get("variant") {
        match prop {
            PropValue::String(s) if matches!(s.as_str(), "filled" | "outlined" | "text") => {}
            _ => errors.push(format!(
                "Button.variant: expected one of [filled, outlined, text], got {:?}",
                prop
            )),
        }
    }

    // Optional: icon (string)
    if let Some(prop) = node.props.get("icon") {
        if !matches!(prop, PropValue::String(_)) {
            errors.push(format!(
                "Button.icon: expected string, got {}",
                prop.type_name()
            ));
        }
    }

    // Optional: disabled (bool)
    if let Some(prop) = node.props.get("disabled") {
        if !matches!(prop, PropValue::Bool(_)) {
            errors.push(format!(
                "Button.disabled: expected bool, got {}",
                prop.type_name()
            ));
        }
    }

    // Optional: loading (bool)
    if let Some(prop) = node.props.get("loading") {
        if !matches!(prop, PropValue::Bool(_)) {
            errors.push(format!(
                "Button.loading: expected bool, got {}",
                prop.type_name()
            ));
        }
    }

    // No children
    if !node.children.is_empty() {
        errors.push(format!(
            "Button: does not accept children, but got {}",
            node.children.len()
        ));
    }

    // Optional: accessible (record)
    if let Some(prop) = node.props.get("accessible") {
        errors.extend(accessibility::validate_accessible_prop("Button", prop));
    }

    // Unknown props
    for key in node.props.keys() {
        if !matches!(
            key.as_str(),
            "label" | "on_tap" | "variant" | "icon" | "disabled" | "loading" | "accessible"
        ) {
            errors.push(format!("Button: unknown prop '{key}'"));
        }
    }

    errors
}

fn validate_text_input(node: &SurfaceNode) -> Vec<String> {
    let mut errors = Vec::new();

    // Required: value (string)
    match node.props.get("value") {
        Some(PropValue::String(_)) => {}
        Some(other) => errors.push(format!(
            "TextInput.value: expected string, got {}",
            other.type_name()
        )),
        None => errors.push("TextInput.value: required prop missing".to_string()),
    }

    // Required: on_change (lambda)
    match node.props.get("on_change") {
        Some(PropValue::Lambda { .. }) => {}
        Some(other) => errors.push(format!(
            "TextInput.on_change: expected lambda, got {}",
            other.type_name()
        )),
        None => errors.push("TextInput.on_change: required prop missing".to_string()),
    }

    // Optional: placeholder (string)
    if let Some(prop) = node.props.get("placeholder") {
        if !matches!(prop, PropValue::String(_)) {
            errors.push(format!(
                "TextInput.placeholder: expected string, got {}",
                prop.type_name()
            ));
        }
    }

    // Optional: label (string)
    if let Some(prop) = node.props.get("label") {
        if !matches!(prop, PropValue::String(_)) {
            errors.push(format!(
                "TextInput.label: expected string, got {}",
                prop.type_name()
            ));
        }
    }

    // Optional: keyboard (string enum)
    if let Some(prop) = node.props.get("keyboard") {
        match prop {
            PropValue::String(s)
                if matches!(s.as_str(), "text" | "number" | "email" | "phone" | "url") => {}
            _ => errors.push(format!(
                "TextInput.keyboard: expected one of [text, number, email, phone, url], got {:?}",
                prop
            )),
        }
    }

    // Optional: max_length (number)
    if let Some(prop) = node.props.get("max_length") {
        if !matches!(prop, PropValue::Number(_)) {
            errors.push(format!(
                "TextInput.max_length: expected number, got {}",
                prop.type_name()
            ));
        }
    }

    // Optional: multiline (bool)
    if let Some(prop) = node.props.get("multiline") {
        if !matches!(prop, PropValue::Bool(_)) {
            errors.push(format!(
                "TextInput.multiline: expected bool, got {}",
                prop.type_name()
            ));
        }
    }

    // No children
    if !node.children.is_empty() {
        errors.push(format!(
            "TextInput: does not accept children, but got {}",
            node.children.len()
        ));
    }

    // Optional: accessible (record)
    if let Some(prop) = node.props.get("accessible") {
        errors.extend(accessibility::validate_accessible_prop("TextInput", prop));
    }

    // Unknown props
    for key in node.props.keys() {
        if !matches!(
            key.as_str(),
            "value"
                | "on_change"
                | "placeholder"
                | "label"
                | "keyboard"
                | "max_length"
                | "multiline"
                | "accessible"
        ) {
            errors.push(format!("TextInput: unknown prop '{key}'"));
        }
    }

    errors
}