leptos_helios_app/
lib.rs

1//! Helios Web Application - Main entry point
2
3use wasm_bindgen::prelude::*;
4
5// Import the console.log function from the browser
6#[wasm_bindgen]
7extern "C" {
8    #[wasm_bindgen(js_namespace = console)]
9    fn log(s: &str);
10}
11
12// Define a macro to make console.log easier to use
13macro_rules! console_log {
14    ($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
15}
16
17// Initialize the panic hook for better error messages
18#[wasm_bindgen(start)]
19pub fn main() {
20    console_error_panic_hook::set_once();
21    console_log!("Helios Web Application initialized successfully!");
22}
23
24// Export a simple function to test the WASM module
25#[wasm_bindgen]
26pub fn greet(name: &str) -> String {
27    format!("Hello, {}! You've been greeted from Helios!", name)
28}
29
30// Export a function to test data processing
31#[wasm_bindgen]
32pub fn process_data(data: &str) -> String {
33    console_log!("Processing data: {}", data);
34    format!("Processed: {}", data.to_uppercase())
35}
36
37// Export a function to test chart creation
38#[wasm_bindgen]
39pub fn create_simple_chart() -> String {
40    console_log!("Creating simple chart");
41    r#"{
42        "type": "line",
43        "data": {
44            "labels": ["A", "B", "C", "D"],
45            "datasets": [{
46                "label": "Sample Data",
47                "data": [1, 2, 3, 4]
48            }]
49        }
50    }"#
51    .to_string()
52}
53
54// Export a function to test WebGPU capabilities
55#[wasm_bindgen]
56pub fn test_webgpu_support() -> String {
57    console_log!("Testing WebGPU support");
58
59    // Check if WebGPU is available in the browser
60    if let Ok(webgpu_available) = check_webgpu_availability() {
61        console_log!("WebGPU support check result: {}", webgpu_available);
62        if webgpu_available {
63            "Supported".to_string()
64        } else {
65            "Not Supported".to_string()
66        }
67    } else {
68        console_log!("WebGPU support check failed");
69        "Error".to_string()
70    }
71}
72
73// Helper function to check WebGPU availability using JavaScript interop
74fn check_webgpu_availability() -> Result<bool, JsValue> {
75    // Use JavaScript to check for WebGPU support
76    let window = web_sys::window().ok_or("No window object")?;
77
78    // Access navigator through JavaScript reflection
79    let navigator = js_sys::Reflect::get(&window, &JsValue::from_str("navigator"))?;
80
81    if navigator.is_undefined() {
82        return Ok(false);
83    }
84
85    // Check if navigator.gpu exists (WebGPU API)
86    let gpu = js_sys::Reflect::get(&navigator, &JsValue::from_str("gpu"))?;
87
88    if gpu.is_undefined() {
89        return Ok(false);
90    }
91
92    // If we get here, WebGPU is available
93    Ok(true)
94}
95
96// Export a function to create chart data
97#[wasm_bindgen]
98pub fn create_chart_data(chart_type: &str, data_points: usize) -> String {
99    console_log!(
100        "Creating chart data for type: {} with {} points",
101        chart_type,
102        data_points
103    );
104
105    let mut data = Vec::new();
106
107    for i in 0..data_points {
108        let value = match chart_type {
109            "line" | "bar" => {
110                // Generate sine wave with some noise
111                let x = i as f64 * 0.5;
112                (x.sin() * 50.0 + 50.0 + (i as f64 * 0.1).sin() * 10.0) as f32
113            }
114            "scatter" => {
115                // Generate random scatter data
116                ((i as f64 * 7.3) % 100.0) as f32
117            }
118            "heatmap" => {
119                // Generate heatmap values
120                ((i as f64 * 11.7) % 100.0) as f32
121            }
122            _ => i as f32,
123        };
124
125        data.push(serde_json::json!({
126            "x": i,
127            "y": value,
128            "value": value
129        }));
130    }
131
132    serde_json::to_string(&data).unwrap_or_else(|_| "[]".to_string())
133}
134
135// Export a function to process chart data
136#[wasm_bindgen]
137pub fn process_chart_data(data: &str) -> String {
138    console_log!("Processing chart data");
139
140    // Process chart data (e.g., normalize, filter, etc.)
141    match serde_json::from_str::<Vec<serde_json::Value>>(data) {
142        Ok(mut parsed_data) => {
143            // Normalize data to 0-100 range
144            if let Some(max_val) = parsed_data
145                .iter()
146                .filter_map(|v| v.get("y").and_then(|y| y.as_f64()))
147                .fold(None, |acc, val| Some(acc.map_or(val, |a: f64| a.max(val))))
148            {
149                if max_val > 0.0 {
150                    for item in &mut parsed_data {
151                        if let Some(y) = item.get_mut("y") {
152                            if let Some(val) = y.as_f64() {
153                                *y = serde_json::Value::Number(
154                                    serde_json::Number::from_f64((val / max_val) * 100.0)
155                                        .unwrap_or(serde_json::Number::from(0)),
156                                );
157                            }
158                        }
159                    }
160                }
161            }
162
163            serde_json::to_string(&parsed_data).unwrap_or_else(|_| "[]".to_string())
164        }
165        Err(_) => "[]".to_string(),
166    }
167}