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
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
//! Accessibility primitives for PEPL UI components.
//!
//! Every component has built-in accessibility support. The `accessible()` function
//! creates an [`AccessibilityInfo`] that maps to platform accessibility APIs
//! (VoiceOver, TalkBack, ARIA, etc.).
//!
//! # Default Accessibility
//!
//! Components auto-generate sensible defaults:
//! - Button label → accessible label, role "button"
//! - TextInput label/placeholder → accessible label, role "textfield"
//! - Text value → accessible label, role "text"
//! - ProgressBar → "{value}% complete", role "progressbar"
//! - Modal title → accessible label, role "dialog"
//! - Toast message → accessible label, role "alert"
//!
//! Developers can override defaults via the `accessible` prop:
//! ```pepl
//! Button {
//!     label: "Add Water",
//!     on_tap: add_water(250),
//!     accessible: accessible(
//!         label: "Add 250 milliliters of water",
//!         hint: "Double tap to add water to today's intake",
//!     ),
//! }
//! ```

use crate::prop_value::PropValue;
use std::collections::BTreeMap;

// ── Semantic Role ────────────────────────────────────────────────────────────

/// Semantic role for accessibility.
///
/// Maps to platform equivalents:
/// - iOS: `UIAccessibilityTraits`
/// - Android: `AccessibilityNodeInfo.setRoleDescription`
/// - Web: ARIA `role` attribute
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SemanticRole {
    Button,
    TextField,
    ProgressBar,
    Heading,
    Image,
    Link,
    Checkbox,
    Slider,
    List,
    Dialog,
    Alert,
    Group,
    Region,
    Text,
    None,
}

impl SemanticRole {
    /// String representation matching the spec enum values.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Button => "button",
            Self::TextField => "textfield",
            Self::ProgressBar => "progressbar",
            Self::Heading => "heading",
            Self::Image => "image",
            Self::Link => "link",
            Self::Checkbox => "checkbox",
            Self::Slider => "slider",
            Self::List => "list",
            Self::Dialog => "dialog",
            Self::Alert => "alert",
            Self::Group => "group",
            Self::Region => "region",
            Self::Text => "text",
            Self::None => "none",
        }
    }

    /// Parse a role string. Returns `None` for unrecognized values.
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "button" => Some(Self::Button),
            "textfield" => Some(Self::TextField),
            "progressbar" => Some(Self::ProgressBar),
            "heading" => Some(Self::Heading),
            "image" => Some(Self::Image),
            "link" => Some(Self::Link),
            "checkbox" => Some(Self::Checkbox),
            "slider" => Some(Self::Slider),
            "list" => Some(Self::List),
            "dialog" => Some(Self::Dialog),
            "alert" => Some(Self::Alert),
            "group" => Some(Self::Group),
            "region" => Some(Self::Region),
            "text" => Some(Self::Text),
            "none" => Some(Self::None),
            _ => None,
        }
    }

    /// All valid role string values (for validation).
    pub fn valid_values() -> &'static [&'static str] {
        &[
            "button",
            "textfield",
            "progressbar",
            "heading",
            "image",
            "link",
            "checkbox",
            "slider",
            "list",
            "dialog",
            "alert",
            "group",
            "region",
            "text",
            "none",
        ]
    }
}

// ── Live Region ──────────────────────────────────────────────────────────────

/// Live region behavior for dynamic content updates.
///
/// - `Polite`: Announces updates when the user is idle.
/// - `Assertive`: Interrupts current speech to announce updates.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LiveRegion {
    Polite,
    Assertive,
}

impl LiveRegion {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Polite => "polite",
            Self::Assertive => "assertive",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "polite" => Some(Self::Polite),
            "assertive" => Some(Self::Assertive),
            _ => None,
        }
    }
}

// ── AccessibilityInfo ────────────────────────────────────────────────────────

/// Accessibility attributes for a UI component.
///
/// Created via the `accessible()` function in PEPL source:
/// ```pepl
/// accessible(
///     label: "Add 250ml water",
///     hint: "Double tap to add water",
///     role: "button",
///     value: "250",
///     live_region: "polite",
/// )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct AccessibilityInfo {
    /// Screen reader label (required). Describes the element's purpose.
    pub label: String,

    /// Additional context or instructions (optional).
    pub hint: Option<String>,

    /// Semantic role (optional — defaults per component type).
    pub role: Option<SemanticRole>,

    /// Current value for screen readers (optional — e.g., progress percentage).
    pub value: Option<String>,

    /// Live region behavior for dynamic updates (optional).
    pub live_region: Option<LiveRegion>,
}

impl AccessibilityInfo {
    /// Create a new AccessibilityInfo with a label (required).
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            hint: None,
            role: None,
            value: None,
            live_region: None,
        }
    }

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

    /// Set the semantic role.
    pub fn role(mut self, role: SemanticRole) -> Self {
        self.role = Some(role);
        self
    }

    /// Set the current value.
    pub fn value(mut self, value: impl Into<String>) -> Self {
        self.value = Some(value.into());
        self
    }

    /// Set the live region behavior.
    pub fn live_region(mut self, live_region: LiveRegion) -> Self {
        self.live_region = Some(live_region);
        self
    }

    /// Convert to a `PropValue::Record` for insertion into `SurfaceNode.props`.
    pub fn to_prop_value(&self) -> PropValue {
        let mut fields = BTreeMap::new();
        fields.insert("label".to_string(), PropValue::String(self.label.clone()));
        if let Some(ref hint) = self.hint {
            fields.insert("hint".to_string(), PropValue::String(hint.clone()));
        }
        if let Some(role) = self.role {
            fields.insert(
                "role".to_string(),
                PropValue::String(role.as_str().to_string()),
            );
        }
        if let Some(ref value) = self.value {
            fields.insert("value".to_string(), PropValue::String(value.clone()));
        }
        if let Some(live_region) = self.live_region {
            fields.insert(
                "live_region".to_string(),
                PropValue::String(live_region.as_str().to_string()),
            );
        }
        PropValue::Record(fields)
    }
}

// ── Default Role Mapping ─────────────────────────────────────────────────────

/// Returns the default semantic role for a component type.
///
/// | Component    | Default Role   |
/// |-------------|----------------|
/// | Button      | button         |
/// | TextInput   | textfield      |
/// | Text        | text           |
/// | ProgressBar | progressbar    |
/// | Column      | group          |
/// | Row         | group          |
/// | Scroll      | region         |
/// | ScrollList  | list           |
/// | Modal       | dialog         |
/// | Toast       | alert          |
pub fn default_role(component_type: &str) -> SemanticRole {
    match component_type {
        "Button" => SemanticRole::Button,
        "TextInput" => SemanticRole::TextField,
        "Text" => SemanticRole::Text,
        "ProgressBar" => SemanticRole::ProgressBar,
        "Column" => SemanticRole::Group,
        "Row" => SemanticRole::Group,
        "Scroll" => SemanticRole::Region,
        "ScrollList" => SemanticRole::List,
        "Modal" => SemanticRole::Dialog,
        "Toast" => SemanticRole::Alert,
        _ => SemanticRole::None,
    }
}

// ── Auto-Generated Accessibility ─────────────────────────────────────────────

/// Generate default accessibility info from component type and existing props.
///
/// Auto-labeling rules:
/// - Button: `label` prop → accessible label
/// - TextInput: `label` prop, else `placeholder`, else "Text input"
/// - Text: `value` prop (truncated to 100 chars)
/// - ProgressBar: "{value}% complete"
/// - Modal: `title` prop, else "Dialog"
/// - Toast: `message` prop
/// - Column, Row, Scroll, ScrollList: component type name (generic)
pub fn auto_accessible(
    component_type: &str,
    props: &BTreeMap<String, PropValue>,
) -> AccessibilityInfo {
    let role = default_role(component_type);
    let label = auto_label(component_type, props);

    let mut info = AccessibilityInfo::new(label).role(role);

    // Add value for ProgressBar
    if component_type == "ProgressBar" {
        if let Some(PropValue::Number(v)) = props.get("value") {
            let pct = (v * 100.0).round() as i64;
            info = info.value(format!("{pct}%"));
        }
    }

    // Add live_region for Toast (assertive — interrupts to announce)
    if component_type == "Toast" {
        info = info.live_region(LiveRegion::Assertive);
    }

    info
}

/// Extract an auto-generated label from component props.
fn auto_label(component_type: &str, props: &BTreeMap<String, PropValue>) -> String {
    match component_type {
        "Button" => extract_string_prop(props, "label").unwrap_or_else(|| "Button".to_string()),

        "TextInput" => extract_string_prop(props, "label")
            .or_else(|| extract_string_prop(props, "placeholder"))
            .unwrap_or_else(|| "Text input".to_string()),

        "Text" => {
            let value = extract_string_prop(props, "value").unwrap_or_else(|| "Text".to_string());
            // Truncate long text for accessibility labels
            if value.len() > 100 {
                format!("{}", &value[..100])
            } else {
                value
            }
        }

        "ProgressBar" => {
            if let Some(PropValue::Number(v)) = props.get("value") {
                let pct = (v * 100.0).round() as i64;
                format!("{pct}% complete")
            } else {
                "Progress bar".to_string()
            }
        }

        "Modal" => extract_string_prop(props, "title").unwrap_or_else(|| "Dialog".to_string()),

        "Toast" => {
            extract_string_prop(props, "message").unwrap_or_else(|| "Notification".to_string())
        }

        "ScrollList" => "List".to_string(),

        // Layout containers: generic labels
        _ => component_type.to_string(),
    }
}

/// Extract a string prop value.
fn extract_string_prop(props: &BTreeMap<String, PropValue>, key: &str) -> Option<String> {
    match props.get(key) {
        Some(PropValue::String(s)) => Some(s.clone()),
        _ => None,
    }
}

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

/// Validate an `accessible` prop value.
///
/// The `accessible` prop must be a `PropValue::Record` with:
/// - `label`: string (required)
/// - `hint`: string (optional)
/// - `role`: string enum (optional) — one of the valid semantic roles
/// - `value`: string (optional)
/// - `live_region`: string enum (optional) — "polite" or "assertive"
pub fn validate_accessible_prop(component_name: &str, prop: &PropValue) -> Vec<String> {
    let mut errors = Vec::new();

    let fields = match prop {
        PropValue::Record(fields) => fields,
        _ => {
            errors.push(format!(
                "{component_name}.accessible: expected record, got {}",
                prop.type_name()
            ));
            return errors;
        }
    };

    // Required: label (string)
    match fields.get("label") {
        Some(PropValue::String(_)) => {}
        Some(other) => errors.push(format!(
            "{component_name}.accessible.label: expected string, got {}",
            other.type_name()
        )),
        None => errors.push(format!(
            "{component_name}.accessible.label: required field missing"
        )),
    }

    // Optional: hint (string)
    if let Some(val) = fields.get("hint") {
        if !matches!(val, PropValue::String(_)) {
            errors.push(format!(
                "{component_name}.accessible.hint: expected string, got {}",
                val.type_name()
            ));
        }
    }

    // Optional: role (string enum)
    if let Some(val) = fields.get("role") {
        match val {
            PropValue::String(s) if SemanticRole::parse(s).is_some() => {}
            PropValue::String(s) => errors.push(format!(
                "{component_name}.accessible.role: unknown role '{s}', expected one of {:?}",
                SemanticRole::valid_values()
            )),
            other => errors.push(format!(
                "{component_name}.accessible.role: expected string, got {}",
                other.type_name()
            )),
        }
    }

    // Optional: value (string)
    if let Some(val) = fields.get("value") {
        if !matches!(val, PropValue::String(_)) {
            errors.push(format!(
                "{component_name}.accessible.value: expected string, got {}",
                val.type_name()
            ));
        }
    }

    // Optional: live_region (string enum)
    if let Some(val) = fields.get("live_region") {
        match val {
            PropValue::String(s) if LiveRegion::parse(s).is_some() => {}
            PropValue::String(s) => errors.push(format!(
                "{component_name}.accessible.live_region: expected 'polite' or 'assertive', got '{s}'"
            )),
            other => errors.push(format!(
                "{component_name}.accessible.live_region: expected string, got {}",
                other.type_name()
            )),
        }
    }

    // Unknown fields
    for key in fields.keys() {
        if !matches!(
            key.as_str(),
            "label" | "hint" | "role" | "value" | "live_region"
        ) {
            errors.push(format!(
                "{component_name}.accessible: unknown field '{key}'"
            ));
        }
    }

    errors
}

/// Apply default accessibility to a SurfaceNode if not already present.
///
/// If the node already has an `"accessible"` prop, this is a no-op.
/// Otherwise, auto-generates defaults based on component type and existing props.
pub fn ensure_accessible(node: &mut crate::surface::SurfaceNode) {
    if node.props.contains_key("accessible") {
        return;
    }
    let info = auto_accessible(&node.component_type, &node.props);
    node.set_prop("accessible", info.to_prop_value());
}