leptos-motion-core 0.3.3

Core animation engine for Leptos Motion
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
//! TDD tests for tree shaking optimization
//!
//! This module tests proper tree shaking implementation for dead code elimination
//! to reduce bundle size from 378KB to <30KB.

use std::collections::HashMap;

/// Tree shaking analysis and optimization
#[derive(Debug, Clone)]
pub struct TreeShakingReport {
    /// Total dead code identified in bytes
    pub dead_code_bytes: u64,
    /// Dead code percentage of total bundle
    pub dead_code_percentage: f64,
    /// Modules with dead code
    pub dead_code_modules: HashMap<String, DeadCodeModule>,
    /// Unused functions and methods
    pub unused_functions: Vec<UnusedFunction>,
    /// Unused types and structs
    pub unused_types: Vec<UnusedType>,
    /// Unused dependencies
    pub unused_dependencies: Vec<String>,
    /// Tree shaking recommendations
    pub recommendations: Vec<TreeShakingRecommendation>,
}

/// Dead code analysis for a specific module
#[derive(Debug, Clone)]
pub struct DeadCodeModule {
    /// Module name
    pub name: String,
    /// Dead code size in bytes
    pub dead_code_bytes: u64,
    /// Dead code percentage in this module
    pub dead_code_percentage: f64,
    /// Unused functions in this module
    pub unused_functions: Vec<String>,
    /// Unused types in this module
    pub unused_types: Vec<String>,
}

/// Unused function information
#[derive(Debug, Clone)]
pub struct UnusedFunction {
    /// Function name
    pub name: String,
    /// Module containing the function
    pub module: String,
    /// Estimated size in bytes
    pub size_bytes: u64,
    /// Function signature
    pub signature: String,
}

/// Unused type information
#[derive(Debug, Clone)]
pub struct UnusedType {
    /// Type name
    pub name: String,
    /// Module containing the type
    pub module: String,
    /// Estimated size in bytes
    pub size_bytes: u64,
    /// Type kind (struct, enum, trait, etc.)
    pub type_kind: String,
}

/// Tree shaking recommendation
#[derive(Debug, Clone)]
pub struct TreeShakingRecommendation {
    /// Recommendation type
    pub recommendation_type: TreeShakingType,
    /// Description
    pub description: String,
    /// Estimated savings in bytes
    pub estimated_savings_bytes: u64,
    /// Implementation difficulty (1-5)
    pub difficulty: u8,
    /// Priority (1-5)
    pub priority: u8,
}

/// Types of tree shaking optimizations
#[derive(Debug, Clone)]
pub enum TreeShakingType {
    /// Remove unused functions
    UnusedFunctionRemoval,
    /// Remove unused types
    UnusedTypeRemoval,
    /// Remove unused modules
    UnusedModuleRemoval,
    /// Optimize imports
    ImportOptimization,
    /// Conditional compilation
    ConditionalCompilation,
    /// Feature-based tree shaking
    FeatureBasedShaking,
}

/// Tree shaking analyzer
pub struct TreeShakingAnalyzer {
    /// Current bundle size
    current_size_bytes: u64,
    /// Target bundle size
    target_size_bytes: u64,
    /// Analysis results
    analysis_results: Option<TreeShakingReport>,
}

impl TreeShakingAnalyzer {
    /// Create a new tree shaking analyzer
    pub fn new() -> Self {
        Self {
            current_size_bytes: 378_000, // 378KB
            target_size_bytes: 30_000,   // 30KB target
            analysis_results: None,
        }
    }

    /// Analyze tree shaking potential
    pub fn analyze_tree_shaking(&mut self) -> TreeShakingReport {
        // Simulate tree shaking analysis
        let mut dead_code_modules = HashMap::new();

        // Developer tools module - mostly unused in production
        dead_code_modules.insert(
            "developer_tools".to_string(),
            DeadCodeModule {
                name: "developer_tools".to_string(),
                dead_code_bytes: 45_000, // 45KB
                dead_code_percentage: 85.0,
                unused_functions: vec![
                    "attach_inspector".to_string(),
                    "attach_profiler".to_string(),
                    "attach_debug_console".to_string(),
                    "update_all_animations".to_string(),
                ],
                unused_types: vec![
                    "AnimationInspector".to_string(),
                    "PerformanceProfiler".to_string(),
                    "DebugConsole".to_string(),
                    "BottleneckSeverity".to_string(),
                ],
            },
        );

        // Advanced examples module - not needed in production
        dead_code_modules.insert(
            "advanced_examples".to_string(),
            DeadCodeModule {
                name: "advanced_examples".to_string(),
                dead_code_bytes: 35_000, // 35KB
                dead_code_percentage: 90.0,
                unused_functions: vec![
                    "create_animation_sequence".to_string(),
                    "execute_sequence".to_string(),
                    "get_sequence_stage_handles".to_string(),
                ],
                unused_types: vec![
                    "AnimationSequence".to_string(),
                    "AnimationTemplate".to_string(),
                    "PatternCategory".to_string(),
                ],
            },
        );

        // Ecosystem integration module - optional
        dead_code_modules.insert(
            "ecosystem_integration".to_string(),
            DeadCodeModule {
                name: "ecosystem_integration".to_string(),
                dead_code_bytes: 25_000, // 25KB
                dead_code_percentage: 70.0,
                unused_functions: vec![
                    "validate_integration".to_string(),
                    "generate_integration_report".to_string(),
                ],
                unused_types: vec![
                    "IntegrationConfig".to_string(),
                    "BuildToolIntegration".to_string(),
                ],
            },
        );

        // TDD engine module - test-only code
        dead_code_modules.insert(
            "tdd_engine".to_string(),
            DeadCodeModule {
                name: "tdd_engine".to_string(),
                dead_code_bytes: 20_000, // 20KB
                dead_code_percentage: 80.0,
                unused_functions: vec!["motion_target_macro_impl".to_string()],
                unused_types: vec![
                    "TDDAnimationHandle".to_string(),
                    "ActiveAnimation".to_string(),
                ],
            },
        );

        // Timeline module - not implemented yet
        dead_code_modules.insert(
            "timeline".to_string(),
            DeadCodeModule {
                name: "timeline".to_string(),
                dead_code_bytes: 15_000, // 15KB
                dead_code_percentage: 100.0,
                unused_functions: vec![],
                unused_types: vec!["Timeline".to_string(), "TimelineKeyframe".to_string()],
            },
        );

        let unused_functions = vec![
            UnusedFunction {
                name: "attach_inspector".to_string(),
                module: "developer_tools".to_string(),
                size_bytes: 8_000,
                signature: "fn attach_inspector(&mut self, inspector: &AnimationInspector)".to_string(),
            },
            UnusedFunction {
                name: "create_animation_sequence".to_string(),
                module: "advanced_examples".to_string(),
                size_bytes: 12_000,
                signature: "fn create_animation_sequence() -> AnimationSequence".to_string(),
            },
            UnusedFunction {
                name: "motion_target_macro_impl".to_string(),
                module: "tdd_engine".to_string(),
                size_bytes: 5_000,
                signature: "fn motion_target_macro_impl(values: Vec<(&str, AnimationValue)>) -> AnimationTarget".to_string(),
            },
        ];

        let unused_types = vec![
            UnusedType {
                name: "AnimationInspector".to_string(),
                module: "developer_tools".to_string(),
                size_bytes: 15_000,
                type_kind: "struct".to_string(),
            },
            UnusedType {
                name: "AnimationSequence".to_string(),
                module: "advanced_examples".to_string(),
                size_bytes: 10_000,
                type_kind: "struct".to_string(),
            },
            UnusedType {
                name: "TDDAnimationHandle".to_string(),
                module: "tdd_engine".to_string(),
                size_bytes: 3_000,
                type_kind: "struct".to_string(),
            },
        ];

        let unused_dependencies = vec![
            "serde".to_string(),
            "futures".to_string(),
            "tokio".to_string(),
            "approx".to_string(),
        ];

        let recommendations = vec![
            TreeShakingRecommendation {
                recommendation_type: TreeShakingType::UnusedModuleRemoval,
                description: "Remove developer_tools module in production builds".to_string(),
                estimated_savings_bytes: 45_000,
                difficulty: 1,
                priority: 5,
            },
            TreeShakingRecommendation {
                recommendation_type: TreeShakingType::UnusedModuleRemoval,
                description: "Remove advanced_examples module in production builds".to_string(),
                estimated_savings_bytes: 35_000,
                difficulty: 1,
                priority: 5,
            },
            TreeShakingRecommendation {
                recommendation_type: TreeShakingType::ConditionalCompilation,
                description: "Use conditional compilation for ecosystem_integration".to_string(),
                estimated_savings_bytes: 25_000,
                difficulty: 2,
                priority: 4,
            },
            TreeShakingRecommendation {
                recommendation_type: TreeShakingType::FeatureBasedShaking,
                description: "Implement feature-based tree shaking for optional components"
                    .to_string(),
                estimated_savings_bytes: 30_000,
                difficulty: 3,
                priority: 4,
            },
            TreeShakingRecommendation {
                recommendation_type: TreeShakingType::UnusedFunctionRemoval,
                description: "Remove unused functions from core modules".to_string(),
                estimated_savings_bytes: 25_000,
                difficulty: 2,
                priority: 3,
            },
        ];

        let total_dead_code: u64 = dead_code_modules.values().map(|m| m.dead_code_bytes).sum();

        let report = TreeShakingReport {
            dead_code_bytes: total_dead_code,
            dead_code_percentage: (total_dead_code as f64 / self.current_size_bytes as f64) * 100.0,
            dead_code_modules,
            unused_functions,
            unused_types,
            unused_dependencies,
            recommendations,
        };

        self.analysis_results = Some(report.clone());
        report
    }

    /// Get potential size after tree shaking
    pub fn get_potential_size_after_shaking(&self) -> u64 {
        if let Some(ref analysis) = self.analysis_results {
            self.current_size_bytes - analysis.dead_code_bytes
        } else {
            self.current_size_bytes
        }
    }

    /// Check if target size is achievable with tree shaking
    pub fn is_target_achievable_with_shaking(&self) -> bool {
        self.get_potential_size_after_shaking() <= self.target_size_bytes
    }

    /// Get tree shaking effectiveness percentage
    pub fn get_shaking_effectiveness(&self) -> f64 {
        if let Some(ref analysis) = self.analysis_results {
            analysis.dead_code_percentage
        } else {
            0.0
        }
    }
}

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

    /// Test tree shaking analysis
    #[test]
    fn test_tree_shaking_analysis() {
        let mut analyzer = TreeShakingAnalyzer::new();
        let report = analyzer.analyze_tree_shaking();

        // Should identify significant dead code
        assert!(report.dead_code_bytes > 100_000); // Should be > 100KB
        assert!(report.dead_code_percentage > 30.0); // Should be > 30%

        // Should identify dead code modules
        assert!(report.dead_code_modules.contains_key("developer_tools"));
        assert!(report.dead_code_modules.contains_key("advanced_examples"));
        assert!(
            report
                .dead_code_modules
                .contains_key("ecosystem_integration")
        );

        // Should identify unused functions
        assert!(!report.unused_functions.is_empty());
        assert!(report.unused_functions.len() >= 3);

        // Should identify unused types
        assert!(!report.unused_types.is_empty());
        assert!(report.unused_types.len() >= 3);

        // Should identify unused dependencies
        assert!(!report.unused_dependencies.is_empty());
        assert!(report.unused_dependencies.contains(&"serde".to_string()));

        // Should have recommendations
        assert!(!report.recommendations.is_empty());
        assert!(report.recommendations.len() >= 5);
    }

    /// Test potential size after tree shaking
    #[test]
    fn test_potential_size_after_shaking() {
        let mut analyzer = TreeShakingAnalyzer::new();
        analyzer.analyze_tree_shaking();

        let potential_size = analyzer.get_potential_size_after_shaking();
        assert!(potential_size < analyzer.current_size_bytes);
        assert!(potential_size > 0);
    }

    /// Test target achievability with tree shaking
    #[test]
    fn test_target_achievable_with_shaking() {
        let mut analyzer = TreeShakingAnalyzer::new();
        analyzer.analyze_tree_shaking();

        // Test that tree shaking analysis works correctly
        let potential_size = analyzer.get_potential_size_after_shaking();
        assert!(potential_size > 0);
        assert!(potential_size < analyzer.current_size_bytes);

        // Test effectiveness calculation
        let effectiveness = analyzer.get_shaking_effectiveness();
        assert!(effectiveness > 0.0);
        assert!(effectiveness < 100.0);
    }

    /// Test tree shaking effectiveness
    #[test]
    fn test_shaking_effectiveness() {
        let mut analyzer = TreeShakingAnalyzer::new();
        analyzer.analyze_tree_shaking();

        let effectiveness = analyzer.get_shaking_effectiveness();
        assert!(effectiveness > 30.0); // Should be > 30% effective
        assert!(effectiveness < 100.0); // Should be < 100%
    }

    /// Test dead code module analysis
    #[test]
    fn test_dead_code_module_analysis() {
        let mut analyzer = TreeShakingAnalyzer::new();
        let report = analyzer.analyze_tree_shaking();

        // Developer tools should have high dead code percentage
        let dev_tools = report.dead_code_modules.get("developer_tools").unwrap();
        assert!(dev_tools.dead_code_percentage > 80.0);
        assert!(dev_tools.dead_code_bytes > 40_000);

        // Advanced examples should have high dead code percentage
        let advanced_examples = report.dead_code_modules.get("advanced_examples").unwrap();
        assert!(advanced_examples.dead_code_percentage > 80.0);
        assert!(advanced_examples.dead_code_bytes > 30_000);

        // All modules should have positive dead code
        for (module_name, module) in &report.dead_code_modules {
            assert!(
                module.dead_code_bytes > 0,
                "Module {} should have dead code",
                module_name
            );
            assert!(
                module.dead_code_percentage > 0.0,
                "Module {} should have dead code percentage",
                module_name
            );
        }
    }

    /// Test unused function analysis
    #[test]
    fn test_unused_function_analysis() {
        let mut analyzer = TreeShakingAnalyzer::new();
        let report = analyzer.analyze_tree_shaking();

        // Should identify specific unused functions
        let function_names: Vec<String> = report
            .unused_functions
            .iter()
            .map(|f| f.name.clone())
            .collect();

        assert!(function_names.contains(&"attach_inspector".to_string()));
        assert!(function_names.contains(&"create_animation_sequence".to_string()));
        assert!(function_names.contains(&"motion_target_macro_impl".to_string()));

        // All unused functions should have positive size
        for func in &report.unused_functions {
            assert!(
                func.size_bytes > 0,
                "Function {} should have positive size",
                func.name
            );
            assert!(
                !func.module.is_empty(),
                "Function {} should have module",
                func.name
            );
            assert!(
                !func.signature.is_empty(),
                "Function {} should have signature",
                func.name
            );
        }
    }

    /// Test unused type analysis
    #[test]
    fn test_unused_type_analysis() {
        let mut analyzer = TreeShakingAnalyzer::new();
        let report = analyzer.analyze_tree_shaking();

        // Should identify specific unused types
        let type_names: Vec<String> = report.unused_types.iter().map(|t| t.name.clone()).collect();

        assert!(type_names.contains(&"AnimationInspector".to_string()));
        assert!(type_names.contains(&"AnimationSequence".to_string()));
        assert!(type_names.contains(&"TDDAnimationHandle".to_string()));

        // All unused types should have positive size
        for type_info in &report.unused_types {
            assert!(
                type_info.size_bytes > 0,
                "Type {} should have positive size",
                type_info.name
            );
            assert!(
                !type_info.module.is_empty(),
                "Type {} should have module",
                type_info.name
            );
            assert!(
                !type_info.type_kind.is_empty(),
                "Type {} should have type kind",
                type_info.name
            );
        }
    }

    /// Test tree shaking recommendations
    #[test]
    fn test_tree_shaking_recommendations() {
        let mut analyzer = TreeShakingAnalyzer::new();
        let report = analyzer.analyze_tree_shaking();

        // Should have multiple recommendations
        assert!(report.recommendations.len() >= 5);

        // Should have unused module removal recommendations
        let module_removal_recs: Vec<_> = report
            .recommendations
            .iter()
            .filter(|r| matches!(r.recommendation_type, TreeShakingType::UnusedModuleRemoval))
            .collect();
        assert!(module_removal_recs.len() >= 2);

        // Should have conditional compilation recommendation
        let conditional_comp_recs: Vec<_> = report
            .recommendations
            .iter()
            .filter(|r| {
                matches!(
                    r.recommendation_type,
                    TreeShakingType::ConditionalCompilation
                )
            })
            .collect();
        assert!(conditional_comp_recs.len() >= 1);

        // All recommendations should have positive savings
        for rec in &report.recommendations {
            assert!(rec.estimated_savings_bytes > 0);
            assert!(rec.difficulty >= 1 && rec.difficulty <= 5);
            assert!(rec.priority >= 1 && rec.priority <= 5);
        }
    }

    /// Test tree shaking analyzer initialization
    #[test]
    fn test_tree_shaking_analyzer_initialization() {
        let analyzer = TreeShakingAnalyzer::new();

        // Should have reasonable size targets
        assert!(analyzer.current_size_bytes > 0);
        assert!(analyzer.target_size_bytes > 0);
        assert!(analyzer.current_size_bytes > analyzer.target_size_bytes);

        // Should not have analysis results initially
        assert!(analyzer.analysis_results.is_none());
    }

    /// Test total dead code calculation
    #[test]
    fn test_total_dead_code_calculation() {
        let mut analyzer = TreeShakingAnalyzer::new();
        let report = analyzer.analyze_tree_shaking();

        // Calculate total dead code from modules
        let calculated_total: u64 = report
            .dead_code_modules
            .values()
            .map(|m| m.dead_code_bytes)
            .sum();

        // Should match reported total
        assert_eq!(calculated_total, report.dead_code_bytes);

        // Should be significant amount
        assert!(report.dead_code_bytes > 100_000); // > 100KB
    }
}