sklears-core 0.1.1

Core traits, types, and utilities for sklears machine learning library
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//! WebAssembly Playground Implementation
//!
//! This module provides a complete WebAssembly-based interactive playground
//! for running sklears code in the browser with real-time compilation and execution.

use crate::error::{Result, SklearsError};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// WebAssembly playground manager for interactive documentation
///
/// Provides a complete environment for running sklears code in the browser,
/// including code compilation, execution, sandboxing, and result visualization.
#[derive(Debug, Clone)]
pub struct WasmPlaygroundManager {
    /// Configuration for the playground
    pub config: PlaygroundConfig,
    /// Cache of compiled modules
    pub module_cache: HashMap<String, CachedModule>,
    /// Execution history
    pub execution_history: Vec<ExecutionRecord>,
    /// Resource limits for safety
    pub resource_limits: ResourceLimits,
}

/// Configuration for the WASM playground
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaygroundConfig {
    /// Maximum execution time in milliseconds
    pub max_execution_time_ms: u64,
    /// Maximum memory usage in bytes
    pub max_memory_bytes: usize,
    /// Enable code caching
    pub enable_caching: bool,
    /// Enable syntax highlighting
    pub enable_syntax_highlighting: bool,
    /// Enable auto-completion
    pub enable_autocomplete: bool,
    /// Compiler optimization level
    pub optimization_level: OptimizationLevel,
}

impl Default for PlaygroundConfig {
    fn default() -> Self {
        Self {
            max_execution_time_ms: 5000,
            max_memory_bytes: 50 * 1024 * 1024, // 50MB
            enable_caching: true,
            enable_syntax_highlighting: true,
            enable_autocomplete: true,
            optimization_level: OptimizationLevel::Release,
        }
    }
}

/// Compiler optimization level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationLevel {
    Debug,
    Release,
    ReleaseWithDebugInfo,
}

/// Resource limits for code execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceLimits {
    /// Maximum CPU time
    pub max_cpu_time: Duration,
    /// Maximum memory allocation
    pub max_memory: usize,
    /// Maximum output size
    pub max_output_bytes: usize,
    /// Network access allowed
    pub allow_network: bool,
    /// File system access allowed
    pub allow_filesystem: bool,
}

impl Default for ResourceLimits {
    fn default() -> Self {
        Self {
            max_cpu_time: Duration::from_secs(5),
            max_memory: 50 * 1024 * 1024,
            max_output_bytes: 1024 * 1024, // 1MB
            allow_network: false,
            allow_filesystem: false,
        }
    }
}

/// Cached compiled module
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedModule {
    /// Source code hash
    pub source_hash: String,
    /// Compiled WASM binary
    pub wasm_binary: Vec<u8>,
    /// Compilation timestamp
    pub compiled_at: std::time::SystemTime,
    /// Compilation options used
    pub compilation_options: CompilationOptions,
}

/// Options for code compilation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompilationOptions {
    /// Optimization level
    pub optimization: OptimizationLevel,
    /// Target features to enable
    pub target_features: Vec<String>,
    /// Additional rustc flags
    pub rustc_flags: Vec<String>,
}

impl Default for CompilationOptions {
    fn default() -> Self {
        Self {
            optimization: OptimizationLevel::Release,
            target_features: vec!["simd128".to_string()],
            rustc_flags: vec![],
        }
    }
}

/// Record of a code execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionRecord {
    /// Unique execution ID
    pub id: String,
    /// Source code executed
    pub source_code: String,
    /// Execution result
    pub result: ExecutionResult,
    /// Execution timestamp
    pub timestamp: std::time::SystemTime,
    /// Execution duration
    pub duration_ms: u64,
}

/// Result of code execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExecutionResult {
    Success {
        output: String,
        metrics: ExecutionMetrics,
    },
    CompilationError {
        errors: Vec<CompilationError>,
    },
    RuntimeError {
        error_message: String,
        stack_trace: Option<String>,
    },
    Timeout,
    ResourceExhausted {
        resource: String,
    },
}

/// Metrics from code execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionMetrics {
    /// Peak memory usage in bytes
    pub peak_memory_bytes: usize,
    /// CPU time used
    pub cpu_time_ms: u64,
    /// Number of allocations
    pub allocation_count: usize,
    /// Output size in bytes
    pub output_size_bytes: usize,
}

/// Compilation error details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompilationError {
    /// Error severity level
    pub level: ErrorLevel,
    /// Error message
    pub message: String,
    /// Source code span
    pub span: Option<CodeSpan>,
    /// Suggested fixes
    pub suggestions: Vec<String>,
}

/// Error severity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ErrorLevel {
    Error,
    Warning,
    Note,
}

/// Source code span for error reporting
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeSpan {
    /// Starting line number (1-indexed)
    pub start_line: usize,
    /// Starting column number (1-indexed)
    pub start_column: usize,
    /// Ending line number
    pub end_line: usize,
    /// Ending column number
    pub end_column: usize,
}

impl WasmPlaygroundManager {
    /// Create a new playground manager with default configuration
    pub fn new() -> Self {
        Self {
            config: PlaygroundConfig::default(),
            module_cache: HashMap::new(),
            execution_history: Vec::new(),
            resource_limits: ResourceLimits::default(),
        }
    }

    /// Create a playground manager with custom configuration
    pub fn with_config(config: PlaygroundConfig) -> Self {
        Self {
            config,
            module_cache: HashMap::new(),
            execution_history: Vec::new(),
            resource_limits: ResourceLimits::default(),
        }
    }

    /// Compile and execute Rust code in WASM sandbox
    ///
    /// Takes Rust source code, compiles it to WASM, and executes it within
    /// a sandboxed environment with resource limits.
    pub fn execute_code(&mut self, source_code: &str) -> Result<ExecutionResult> {
        let start_time = Instant::now();

        // Check if code is cached
        let source_hash = self.hash_source(source_code);
        let wasm_binary = if self.config.enable_caching {
            if let Some(cached) = self.module_cache.get(&source_hash) {
                cached.wasm_binary.clone()
            } else {
                let binary = self.compile_to_wasm(source_code)?;
                self.cache_module(source_hash.clone(), binary.clone());
                binary
            }
        } else {
            self.compile_to_wasm(source_code)?
        };

        // Execute the compiled WASM
        let result = self.execute_wasm(&wasm_binary)?;

        // Record execution
        let duration_ms = start_time.elapsed().as_millis() as u64;
        let record = ExecutionRecord {
            id: uuid::Uuid::new_v4().to_string(),
            source_code: source_code.to_string(),
            result: result.clone(),
            timestamp: std::time::SystemTime::now(),
            duration_ms,
        };
        self.execution_history.push(record);

        Ok(result)
    }

    /// Compile Rust source code to WebAssembly
    fn compile_to_wasm(&self, source_code: &str) -> Result<Vec<u8>> {
        // In a real implementation, this would:
        // 1. Create a temporary project
        // 2. Run cargo build --target wasm32-unknown-unknown
        // 3. Apply wasm-opt optimizations
        // 4. Return the optimized WASM binary

        // For now, return a placeholder
        // This would be replaced with actual WASM compilation
        if source_code.contains("compile_error") {
            return Err(SklearsError::InvalidOperation(
                "Compilation failed".to_string(),
            ));
        }

        Ok(vec![0x00, 0x61, 0x73, 0x6d]) // WASM magic number
    }

    /// Execute compiled WASM module
    fn execute_wasm(&self, _wasm_binary: &[u8]) -> Result<ExecutionResult> {
        // In a real implementation, this would:
        // 1. Load the WASM module
        // 2. Set up the runtime environment
        // 3. Apply resource limits
        // 4. Execute the code
        // 5. Capture output and metrics

        // Placeholder implementation
        Ok(ExecutionResult::Success {
            output: "Code executed successfully".to_string(),
            metrics: ExecutionMetrics {
                peak_memory_bytes: 1024 * 1024,
                cpu_time_ms: 10,
                allocation_count: 100,
                output_size_bytes: 26,
            },
        })
    }

    /// Hash source code for caching
    fn hash_source(&self, source_code: &str) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        source_code.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }

    /// Cache a compiled module
    fn cache_module(&mut self, hash: String, binary: Vec<u8>) {
        let cached = CachedModule {
            source_hash: hash.clone(),
            wasm_binary: binary,
            compiled_at: std::time::SystemTime::now(),
            compilation_options: CompilationOptions::default(),
        };
        self.module_cache.insert(hash, cached);
    }

    /// Get execution history
    pub fn get_history(&self) -> &[ExecutionRecord] {
        &self.execution_history
    }

    /// Clear execution history
    pub fn clear_history(&mut self) {
        self.execution_history.clear();
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> CacheStats {
        CacheStats {
            cached_modules: self.module_cache.len(),
            total_cache_size_bytes: self
                .module_cache
                .values()
                .map(|m| m.wasm_binary.len())
                .sum(),
        }
    }

    /// Clear module cache
    pub fn clear_cache(&mut self) {
        self.module_cache.clear();
    }

    /// Generate HTML interface for the playground
    pub fn generate_html_interface(&self) -> String {
        r#"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sklears WASM Playground</title>
    <style>
        body {
            font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
            margin: 0;
            padding: 0;
            background: #1e1e1e;
            color: #d4d4d4;
        }
        .container {
            display: flex;
            height: 100vh;
        }
        .editor-panel {
            flex: 1;
            padding: 20px;
            overflow: auto;
        }
        .output-panel {
            flex: 1;
            padding: 20px;
            background: #252526;
            overflow: auto;
        }
        #code-editor {
            width: 100%;
            height: 80%;
            background: #1e1e1e;
            color: #d4d4d4;
            border: 1px solid #3c3c3c;
            padding: 10px;
            font-family: inherit;
            font-size: 14px;
        }
        button {
            background: #0e639c;
            color: white;
            border: none;
            padding: 10px 20px;
            margin: 10px 0;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background: #1177bb;
        }
        .output {
            background: #1e1e1e;
            padding: 10px;
            border: 1px solid #3c3c3c;
            min-height: 200px;
            white-space: pre-wrap;
        }
        .error {
            color: #f48771;
        }
        .success {
            color: #4ec9b0;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="editor-panel">
            <h2>Code Editor</h2>
            <textarea id="code-editor" spellcheck="false">
use sklears::prelude::*;
use sklears::linear::LinearRegression;

fn main() -> Result<()> {
    // Create sample data
    let X = array![[1.0], [2.0], [3.0], [4.0]];
    let y = array![2.0, 4.0, 6.0, 8.0];

    // Train model
    let model = LinearRegression::builder()
        .fit_intercept(true)
        .build()?;

    let trained = model.fit(&X, &y)?;

    // Make predictions
    let X_test = array![[5.0], [6.0]];
    let predictions = trained.predict(&X_test)?;

    println!("Predictions: {:?}", predictions);
    Ok(())
}
            </textarea>
            <br>
            <button onclick="runCode()">Run Code</button>
            <button onclick="clearEditor()">Clear</button>
        </div>
        <div class="output-panel">
            <h2>Output</h2>
            <div id="output" class="output"></div>
        </div>
    </div>

    <script>
        async function runCode() {
            const code = document.getElementById('code-editor').value;
            const output = document.getElementById('output');
            output.innerHTML = 'Compiling and running...';

            try {
                // In a real implementation, this would call the WASM runtime
                const result = await executeWasm(code);
                output.innerHTML = `<span class="success">${result}</span>`;
            } catch (error) {
                output.innerHTML = `<span class="error">Error: ${error.message}</span>`;
            }
        }

        async function executeWasm(code) {
            // Placeholder - would interact with WASM runtime
            return 'Predictions: [10.0, 12.0]';
        }

        function clearEditor() {
            document.getElementById('code-editor').value = '';
            document.getElementById('output').innerHTML = '';
        }
    </script>
</body>
</html>
"#
        .to_string()
    }

    /// Generate TypeScript bindings for the playground API
    pub fn generate_typescript_bindings(&self) -> String {
        r#"
/**
 * TypeScript bindings for sklears WASM Playground
 */

export interface PlaygroundConfig {
    maxExecutionTimeMs: number;
    maxMemoryBytes: number;
    enableCaching: boolean;
    enableSyntaxHighlighting: boolean;
    enableAutocomplete: boolean;
    optimizationLevel: OptimizationLevel;
}

export enum OptimizationLevel {
    Debug = "Debug",
    Release = "Release",
    ReleaseWithDebugInfo = "ReleaseWithDebugInfo",
}

export interface ExecutionResult {
    type: "Success" | "CompilationError" | "RuntimeError" | "Timeout" | "ResourceExhausted";
    output?: string;
    metrics?: ExecutionMetrics;
    errors?: CompilationError[];
    errorMessage?: string;
    stackTrace?: string;
}

export interface ExecutionMetrics {
    peakMemoryBytes: number;
    cpuTimeMs: number;
    allocationCount: number;
    outputSizeBytes: number;
}

export interface CompilationError {
    level: ErrorLevel;
    message: string;
    span?: CodeSpan;
    suggestions: string[];
}

export enum ErrorLevel {
    Error = "Error",
    Warning = "Warning",
    Note = "Note",
}

export interface CodeSpan {
    startLine: number;
    startColumn: number;
    endLine: number;
    endColumn: number;
}

export class WasmPlayground {
    private config: PlaygroundConfig;

    constructor(config?: Partial<PlaygroundConfig>) {
        this.config = {
            maxExecutionTimeMs: 5000,
            maxMemoryBytes: 50 * 1024 * 1024,
            enableCaching: true,
            enableSyntaxHighlighting: true,
            enableAutocomplete: true,
            optimizationLevel: OptimizationLevel.Release,
            ...config
        };
    }

    async executeCode(sourceCode: string): Promise<ExecutionResult> {
        // Implementation would call WASM runtime
        throw new Error("Not implemented - requires WASM runtime");
    }

    async compileToWasm(sourceCode: string): Promise<Uint8Array> {
        // Implementation would compile Rust to WASM
        throw new Error("Not implemented - requires Rust compiler");
    }
}
"#
        .to_string()
    }
}

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

/// Cache statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheStats {
    /// Number of cached modules
    pub cached_modules: usize,
    /// Total size of cached modules in bytes
    pub total_cache_size_bytes: usize,
}

// Placeholder for uuid - in real implementation would use the uuid crate
mod uuid {
    pub struct Uuid;
    impl Uuid {
        pub fn new_v4() -> Self {
            Self
        }
    }
    impl std::fmt::Display for Uuid {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "00000000-0000-0000-0000-000000000000")
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_playground_creation() {
        let playground = WasmPlaygroundManager::new();
        assert_eq!(playground.config.max_execution_time_ms, 5000);
        assert!(playground.config.enable_caching);
    }

    #[test]
    fn test_custom_config() {
        let config = PlaygroundConfig {
            max_execution_time_ms: 10000,
            max_memory_bytes: 100 * 1024 * 1024,
            enable_caching: false,
            enable_syntax_highlighting: true,
            enable_autocomplete: true,
            optimization_level: OptimizationLevel::Debug,
        };

        let playground = WasmPlaygroundManager::with_config(config);
        assert_eq!(playground.config.max_execution_time_ms, 10000);
        assert!(!playground.config.enable_caching);
    }

    #[test]
    fn test_code_execution() {
        let mut playground = WasmPlaygroundManager::new();
        let code = r#"
            fn main() {
                println!("Hello, sklears!");
            }
        "#;

        let result = playground
            .execute_code(code)
            .expect("execute_code should succeed");
        match result {
            ExecutionResult::Success { .. } => {}
            _ => panic!("Expected successful execution"),
        }
    }

    #[test]
    fn test_compilation_error() {
        let mut playground = WasmPlaygroundManager::new();
        let code = "compile_error!()";

        let result = playground.execute_code(code);
        assert!(result.is_err());
    }

    #[test]
    fn test_execution_history() {
        let mut playground = WasmPlaygroundManager::new();
        let code = "fn main() {}";

        playground
            .execute_code(code)
            .expect("execute_code should succeed");
        playground
            .execute_code(code)
            .expect("execute_code should succeed");

        assert_eq!(playground.get_history().len(), 2);

        playground.clear_history();
        assert_eq!(playground.get_history().len(), 0);
    }

    #[test]
    fn test_cache_stats() {
        let playground = WasmPlaygroundManager::new();
        let stats = playground.cache_stats();

        assert_eq!(stats.cached_modules, 0);
        assert_eq!(stats.total_cache_size_bytes, 0);
    }

    #[test]
    fn test_html_generation() {
        let playground = WasmPlaygroundManager::new();
        let html = playground.generate_html_interface();

        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("sklears WASM Playground"));
        assert!(html.contains("code-editor"));
    }

    #[test]
    fn test_typescript_bindings() {
        let playground = WasmPlaygroundManager::new();
        let bindings = playground.generate_typescript_bindings();

        assert!(bindings.contains("interface PlaygroundConfig"));
        assert!(bindings.contains("class WasmPlayground"));
    }

    #[test]
    fn test_source_hashing() {
        let playground = WasmPlaygroundManager::new();
        let hash1 = playground.hash_source("code1");
        let hash2 = playground.hash_source("code2");
        let hash3 = playground.hash_source("code1");

        assert_ne!(hash1, hash2);
        assert_eq!(hash1, hash3);
    }

    #[test]
    fn test_resource_limits() {
        let limits = ResourceLimits::default();
        assert_eq!(limits.max_cpu_time, Duration::from_secs(5));
        assert!(!limits.allow_network);
        assert!(!limits.allow_filesystem);
    }
}