leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly support
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Custom Components System
//!
//! This module provides a framework for creating custom chart components
//! that can be registered and used within the Helios theming system.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use thiserror::Error;

use crate::theme_engine::{ComponentType, ThemeError, ThemeValue};

/// Custom component errors
#[derive(Debug, Error)]
pub enum ComponentError {
    #[error("Component not found: {id}")]
    ComponentNotFound { id: String },

    #[error("Invalid component definition: {message}")]
    InvalidComponentDefinition { message: String },

    #[error("Component validation failed: {message}")]
    ComponentValidationFailed { message: String },

    #[error("Component rendering failed: {message}")]
    ComponentRenderingFailed { message: String },

    #[error("Component registration failed: {message}")]
    ComponentRegistrationFailed { message: String },
}

/// Component identifier
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ComponentId(pub String);

/// Component property definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentProperty {
    pub name: String,
    pub property_type: PropertyType,
    pub required: bool,
    pub default_value: Option<ThemeValue>,
    pub description: Option<String>,
    pub validation: Option<ValidationRule>,
}

/// Property types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PropertyType {
    String,
    Number,
    Boolean,
    Color,
    Array,
    Object,
    Function,
}

/// Validation rules for component properties
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationRule {
    pub min_value: Option<f64>,
    pub max_value: Option<f64>,
    pub allowed_values: Option<Vec<String>>,
    pub pattern: Option<String>,
    pub custom_validator: Option<String>,
}

/// Component event definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentEvent {
    pub name: String,
    pub event_type: EventType,
    pub description: Option<String>,
    pub parameters: Vec<ComponentProperty>,
}

/// Event types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EventType {
    Click,
    Hover,
    Focus,
    Blur,
    Change,
    Custom(String),
}

/// Component lifecycle hooks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentLifecycle {
    pub on_mount: Option<String>,
    pub on_unmount: Option<String>,
    pub on_update: Option<String>,
    pub on_render: Option<String>,
}

/// Custom component definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomComponent {
    pub id: ComponentId,
    pub name: String,
    pub description: Option<String>,
    pub version: String,
    pub component_type: ComponentType,
    pub properties: Vec<ComponentProperty>,
    pub events: Vec<ComponentEvent>,
    pub lifecycle: ComponentLifecycle,
    pub template: String,
    pub styles: HashMap<String, ThemeValue>,
    pub dependencies: Vec<String>,
    pub metadata: HashMap<String, ThemeValue>,
}

/// Component renderer trait
pub trait ComponentRenderer {
    fn render(
        &self,
        component: &CustomComponent,
        props: &HashMap<String, ThemeValue>,
    ) -> Result<String, ComponentError>;
    fn validate_props(
        &self,
        component: &CustomComponent,
        props: &HashMap<String, ThemeValue>,
    ) -> Result<(), ComponentError>;
    fn get_required_props(&self, component: &CustomComponent) -> Vec<String>;
}

/// Default component renderer
#[derive(Debug)]
pub struct DefaultComponentRenderer {
    template_engine: TemplateEngine,
    validator: ComponentValidator,
}

impl DefaultComponentRenderer {
    pub fn new() -> Self {
        Self {
            template_engine: TemplateEngine::new(),
            validator: ComponentValidator::new(),
        }
    }
}

impl ComponentRenderer for DefaultComponentRenderer {
    fn render(
        &self,
        component: &CustomComponent,
        props: &HashMap<String, ThemeValue>,
    ) -> Result<String, ComponentError> {
        // Validate props first
        self.validate_props(component, props)?;

        // Render the component
        self.template_engine
            .render(&component.template, props)
            .map_err(|e| ComponentError::ComponentRenderingFailed {
                message: format!("Template rendering failed: {}", e),
            })
    }

    fn validate_props(
        &self,
        component: &CustomComponent,
        props: &HashMap<String, ThemeValue>,
    ) -> Result<(), ComponentError> {
        self.validator.validate_component_props(component, props)
    }

    fn get_required_props(&self, component: &CustomComponent) -> Vec<String> {
        component
            .properties
            .iter()
            .filter(|prop| prop.required)
            .map(|prop| prop.name.clone())
            .collect()
    }
}

/// Template engine for component rendering
#[derive(Debug)]
pub struct TemplateEngine {
    // Simple template engine - can be enhanced with more sophisticated templating
}

impl TemplateEngine {
    pub fn new() -> Self {
        Self {}
    }

    pub fn render(
        &self,
        template: &str,
        props: &HashMap<String, ThemeValue>,
    ) -> Result<String, TemplateError> {
        let mut result = template.to_string();

        // Simple variable substitution
        for (key, value) in props {
            let placeholder = format!("{{{{{}}}}}", key);
            let value_str = self.theme_value_to_string(value);
            result = result.replace(&placeholder, &value_str);
        }

        Ok(result)
    }

    fn theme_value_to_string(&self, value: &ThemeValue) -> String {
        match value {
            ThemeValue::String(s) => s.clone(),
            ThemeValue::Number(n) => n.to_string(),
            ThemeValue::Boolean(b) => b.to_string(),
            ThemeValue::Color(c) => c.clone(),
            ThemeValue::Array(arr) => {
                let values: Vec<String> =
                    arr.iter().map(|v| self.theme_value_to_string(v)).collect();
                format!("[{}]", values.join(", "))
            }
            ThemeValue::Object(obj) => {
                let pairs: Vec<String> = obj
                    .iter()
                    .map(|(k, v)| format!("{}: {}", k, self.theme_value_to_string(v)))
                    .collect();
                format!("{{{}}}", pairs.join(", "))
            }
        }
    }
}

/// Template engine errors
#[derive(Debug, Error)]
pub enum TemplateError {
    #[error("Template rendering failed: {message}")]
    RenderingFailed { message: String },

    #[error("Invalid template syntax: {message}")]
    InvalidSyntax { message: String },

    #[error("Missing required variable: {variable}")]
    MissingVariable { variable: String },
}

/// Component validator
#[derive(Debug)]
pub struct ComponentValidator {
    // Component validation logic
}

impl ComponentValidator {
    pub fn new() -> Self {
        Self {}
    }

    pub fn validate_component_props(
        &self,
        component: &CustomComponent,
        props: &HashMap<String, ThemeValue>,
    ) -> Result<(), ComponentError> {
        // Check required properties
        for prop in &component.properties {
            if prop.required && !props.contains_key(&prop.name) {
                return Err(ComponentError::ComponentValidationFailed {
                    message: format!("Required property '{}' is missing", prop.name),
                });
            }
        }

        // Validate property types and values
        for (prop_name, prop_value) in props {
            if let Some(prop_def) = component.properties.iter().find(|p| &p.name == prop_name) {
                self.validate_property_value(prop_def, prop_value)?;
            }
        }

        Ok(())
    }

    fn validate_property_value(
        &self,
        prop_def: &ComponentProperty,
        value: &ThemeValue,
    ) -> Result<(), ComponentError> {
        // Type validation
        match (&prop_def.property_type, value) {
            (PropertyType::String, ThemeValue::String(_)) => {}
            (PropertyType::Number, ThemeValue::Number(_)) => {}
            (PropertyType::Boolean, ThemeValue::Boolean(_)) => {}
            (PropertyType::Color, ThemeValue::Color(_)) => {}
            (PropertyType::Array, ThemeValue::Array(_)) => {}
            (PropertyType::Object, ThemeValue::Object(_)) => {}
            _ => {
                return Err(ComponentError::ComponentValidationFailed {
                    message: format!("Property '{}' has invalid type", prop_def.name),
                });
            }
        }

        // Custom validation rules
        if let Some(rule) = &prop_def.validation {
            self.validate_with_rule(prop_def, value, rule)?;
        }

        Ok(())
    }

    fn validate_with_rule(
        &self,
        prop_def: &ComponentProperty,
        value: &ThemeValue,
        rule: &ValidationRule,
    ) -> Result<(), ComponentError> {
        match value {
            ThemeValue::Number(n) => {
                if let Some(min) = rule.min_value {
                    if *n < min {
                        return Err(ComponentError::ComponentValidationFailed {
                            message: format!(
                                "Property '{}' value {} is below minimum {}",
                                prop_def.name, n, min
                            ),
                        });
                    }
                }
                if let Some(max) = rule.max_value {
                    if *n > max {
                        return Err(ComponentError::ComponentValidationFailed {
                            message: format!(
                                "Property '{}' value {} is above maximum {}",
                                prop_def.name, n, max
                            ),
                        });
                    }
                }
            }
            ThemeValue::String(s) => {
                if let Some(allowed) = &rule.allowed_values {
                    if !allowed.contains(s) {
                        return Err(ComponentError::ComponentValidationFailed {
                            message: format!(
                                "Property '{}' value '{}' is not in allowed values",
                                prop_def.name, s
                            ),
                        });
                    }
                }
                if let Some(pattern) = &rule.pattern {
                    // Simple pattern matching - can be enhanced with regex
                    if !s.contains(pattern) {
                        return Err(ComponentError::ComponentValidationFailed {
                            message: format!(
                                "Property '{}' value '{}' does not match pattern '{}'",
                                prop_def.name, s, pattern
                            ),
                        });
                    }
                }
            }
            _ => {} // Other types don't have specific validation rules yet
        }

        Ok(())
    }
}

/// Component factory for creating and managing custom components
#[derive(Debug)]
pub struct ComponentFactory {
    components: HashMap<ComponentId, CustomComponent>,
    renderer: Box<dyn ComponentRenderer>,
    validator: ComponentValidator,
}

impl ComponentFactory {
    pub fn new() -> Self {
        Self {
            components: HashMap::new(),
            renderer: Box::new(DefaultComponentRenderer::new()),
            validator: ComponentValidator::new(),
        }
    }

    pub fn with_renderer(renderer: Box<dyn ComponentRenderer>) -> Self {
        Self {
            components: HashMap::new(),
            renderer,
            validator: ComponentValidator::new(),
        }
    }

    /// Register a custom component
    pub fn register_component(&mut self, component: CustomComponent) -> Result<(), ComponentError> {
        // Validate component definition
        self.validator.validate_component_definition(&component)?;

        // Check for conflicts
        if self.components.contains_key(&component.id) {
            return Err(ComponentError::ComponentRegistrationFailed {
                message: format!(
                    "Component with ID '{}' is already registered",
                    component.id.0
                ),
            });
        }

        // Register the component
        self.components.insert(component.id.clone(), component);

        Ok(())
    }

    /// Get a component by ID
    pub fn get_component(&self, id: &ComponentId) -> Result<&CustomComponent, ComponentError> {
        self.components
            .get(id)
            .ok_or_else(|| ComponentError::ComponentNotFound { id: id.0.clone() })
    }

    /// Render a component
    pub fn render_component(
        &self,
        id: &ComponentId,
        props: &HashMap<String, ThemeValue>,
    ) -> Result<String, ComponentError> {
        let component = self.get_component(id)?;
        self.renderer.render(component, props)
    }

    /// List all registered components
    pub fn list_components(&self) -> Vec<&CustomComponent> {
        self.components.values().collect()
    }

    /// Get components by type
    pub fn get_components_by_type(&self, component_type: &ComponentType) -> Vec<&CustomComponent> {
        self.components
            .values()
            .filter(|component| &component.component_type == component_type)
            .collect()
    }

    /// Unregister a component
    pub fn unregister_component(&mut self, id: &ComponentId) -> Result<(), ComponentError> {
        self.components
            .remove(id)
            .ok_or_else(|| ComponentError::ComponentNotFound { id: id.0.clone() })?;
        Ok(())
    }

    /// Get component count
    pub fn component_count(&self) -> usize {
        self.components.len()
    }
}

impl ComponentValidator {
    fn validate_component_definition(
        &self,
        component: &CustomComponent,
    ) -> Result<(), ComponentError> {
        // Validate component ID
        if component.id.0.is_empty() {
            return Err(ComponentError::InvalidComponentDefinition {
                message: "Component ID cannot be empty".to_string(),
            });
        }

        // Validate component name
        if component.name.is_empty() {
            return Err(ComponentError::InvalidComponentDefinition {
                message: "Component name cannot be empty".to_string(),
            });
        }

        // Validate version
        if component.version.is_empty() {
            return Err(ComponentError::InvalidComponentDefinition {
                message: "Component version cannot be empty".to_string(),
            });
        }

        // Validate template
        if component.template.is_empty() {
            return Err(ComponentError::InvalidComponentDefinition {
                message: "Component template cannot be empty".to_string(),
            });
        }

        // Validate properties
        for prop in &component.properties {
            if prop.name.is_empty() {
                return Err(ComponentError::InvalidComponentDefinition {
                    message: "Component property name cannot be empty".to_string(),
                });
            }
        }

        // Validate events
        for event in &component.events {
            if event.name.is_empty() {
                return Err(ComponentError::InvalidComponentDefinition {
                    message: "Component event name cannot be empty".to_string(),
                });
            }
        }

        Ok(())
    }
}

impl Default for ComponentFactory {
    fn default() -> Self {
        Self::new()
    }
}

/// Create a sample custom component
pub fn create_sample_component() -> CustomComponent {
    CustomComponent {
        id: ComponentId("sample-chart".to_string()),
        name: "Sample Chart Component".to_string(),
        description: Some("A sample custom chart component for demonstration".to_string()),
        version: "1.0.0".to_string(),
        component_type: ComponentType::Custom("sample-chart".to_string()),
        properties: vec![
            ComponentProperty {
                name: "title".to_string(),
                property_type: PropertyType::String,
                required: true,
                default_value: None,
                description: Some("Chart title".to_string()),
                validation: None,
            },
            ComponentProperty {
                name: "width".to_string(),
                property_type: PropertyType::Number,
                required: false,
                default_value: Some(ThemeValue::Number(400.0)),
                description: Some("Chart width in pixels".to_string()),
                validation: Some(ValidationRule {
                    min_value: Some(100.0),
                    max_value: Some(2000.0),
                    allowed_values: None,
                    pattern: None,
                    custom_validator: None,
                }),
            },
            ComponentProperty {
                name: "height".to_string(),
                property_type: PropertyType::Number,
                required: false,
                default_value: Some(ThemeValue::Number(300.0)),
                description: Some("Chart height in pixels".to_string()),
                validation: Some(ValidationRule {
                    min_value: Some(100.0),
                    max_value: Some(1500.0),
                    allowed_values: None,
                    pattern: None,
                    custom_validator: None,
                }),
            },
            ComponentProperty {
                name: "color".to_string(),
                property_type: PropertyType::Color,
                required: false,
                default_value: Some(ThemeValue::Color("#3b82f6".to_string())),
                description: Some("Chart color".to_string()),
                validation: None,
            },
        ],
        events: vec![
            ComponentEvent {
                name: "click".to_string(),
                event_type: EventType::Click,
                description: Some("Fired when the chart is clicked".to_string()),
                parameters: vec![],
            },
            ComponentEvent {
                name: "hover".to_string(),
                event_type: EventType::Hover,
                description: Some("Fired when hovering over the chart".to_string()),
                parameters: vec![],
            },
        ],
        lifecycle: ComponentLifecycle {
            on_mount: Some("console.log('Component mounted')".to_string()),
            on_unmount: Some("console.log('Component unmounted')".to_string()),
            on_update: Some("console.log('Component updated')".to_string()),
            on_render: Some("console.log('Component rendered')".to_string()),
        },
        template: r#"
<div class="sample-chart" style="width: {{width}}px; height: {{height}}px; background-color: {{color}};">
    <h3>{{title}}</h3>
    <div class="chart-content">
        <!-- Chart content will be rendered here -->
    </div>
</div>
        "#.to_string(),
        styles: HashMap::new(),
        dependencies: vec!["chart.js".to_string()],
        metadata: HashMap::new(),
    }
}