wasm-slim 0.1.1

WASM bundle size optimizer
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
//! Build workflow orchestration
//! Build workflow orchestration module.
//!
//! This module provides the core build workflow logic that orchestrates the complete
//! WASM optimization pipeline. It separates business logic from presentation concerns,
//! allowing the build process to be used programmatically or via CLI.
//!
//! # Architecture
//!
//! The workflow follows a three-phase approach:
//!
//! 1. **Cargo.toml Optimization**: Analyzes and applies optimizations to the Cargo.toml
//!    file, including profile settings, dependency features, and build-std configuration.
//!
//! 2. **WASM Build Pipeline**: Executes the actual WASM build with optimized settings,
//!    runs wasm-opt for further size reduction, and tracks build metrics.
//!
//! 3. **CI/CD Metrics Validation**: Validates build outputs against configured size budgets
//!    and records historical build data for regression detection.
//!
//! # Examples
//!
//! Basic usage:
//!
//! ```no_run
//! use std::path::Path;
//! use wasm_slim::cmd::workflow::BuildWorkflow;
//!
//! let workflow = BuildWorkflow::new(Path::new("."));
//! let result = workflow.execute(false, false, None).expect("Build failed");
//! println!("Build completed: {} -> {} bytes",
//!          result.metrics.before_bytes, result.metrics.after_bytes);
//! ```
//!
//! Dry-run mode (preview changes without applying):
//!
//! ```no_run
//! # use std::path::Path;
//! # use wasm_slim::cmd::workflow::BuildWorkflow;
//! let workflow = BuildWorkflow::new(Path::new("."));
//! let result = workflow.execute(true, false, None).expect("Dry-run failed");
//! for file in &result.dry_run_files {
//!     println!("Would modify: {}", file);
//! }
//! ```
//!
//! With budget checking:
//!
//! ```no_run
//! # use std::path::Path;
//! # use wasm_slim::cmd::workflow::BuildWorkflow;
//! let workflow = BuildWorkflow::new(Path::new("."));
//! let result = workflow.execute(false, true, None).expect("Build failed");
//! if let Some(passed) = result.budget_check_passed {
//!     assert!(passed, "Build exceeded size budget!");
//! }
//! ```

use anyhow::Result;
use std::path::{Path, PathBuf};

use crate::{config, optimizer, pipeline};

/// Result of the complete build workflow
#[derive(Debug)]
pub struct BuildResult {
    /// Changes made during Cargo.toml optimization
    pub cargo_changes: Vec<String>,
    /// Build metrics (sizes, reduction)
    pub metrics: pipeline::SizeMetrics,
    /// Whether size budget check passed (if applicable)
    pub budget_check_passed: Option<bool>,
    /// Size budget threshold (if configured)
    pub budget_threshold: Option<u64>,
    /// Whether this was a dry-run
    pub dry_run: bool,
    /// Files that would be optimized in dry-run mode
    pub dry_run_files: Vec<String>,
}

/// Result type for Cargo.toml optimization with backup information
///
/// Returns: (changes_made, dry_run_files, backup_paths_with_content)
type OptimizationResult = (Vec<String>, Vec<String>, Vec<(PathBuf, String)>);

/// Build workflow orchestrator
///
/// Coordinates the three-phase build process:
/// 1. Cargo.toml optimization
/// 2. WASM build pipeline
/// 3. CI/CD metrics validation
pub struct BuildWorkflow {
    project_root: PathBuf,
}

impl BuildWorkflow {
    /// Create a new build workflow for the given project
    pub fn new(project_root: &Path) -> Self {
        Self {
            project_root: project_root.to_path_buf(),
        }
    }

    /// Execute the complete build workflow
    pub fn execute(
        &self,
        dry_run: bool,
        check_budget: bool,
        _target_dir: Option<&str>,
    ) -> Result<BuildResult> {
        // Phase 1: Optimize Cargo.toml files and save backups
        let (cargo_changes, dry_run_files, backups) =
            self.optimize_cargo_tomls_with_backup(dry_run)?;

        // Phase 2: Run build pipeline
        let metrics = match self.run_build_pipeline() {
            Ok(m) => m,
            Err(e) => {
                // Rollback on build failure
                if !dry_run && !backups.is_empty() {
                    let _ = self.rollback_cargo_tomls(&backups);
                }
                return Err(e);
            }
        };

        // Phase 3: Check CI/CD metrics
        let (budget_check_passed, budget_threshold) = if check_budget {
            self.check_budget(&metrics)?
        } else {
            (None, None)
        };

        Ok(BuildResult {
            cargo_changes,
            metrics,
            budget_check_passed,
            budget_threshold,
            dry_run,
            dry_run_files,
        })
    }

    /// Phase 1: Optimize Cargo.toml files with backup support
    fn optimize_cargo_tomls_with_backup(&self, dry_run: bool) -> Result<OptimizationResult> {
        let _config = config::ConfigLoader::load(&self.project_root)
            .unwrap_or_else(|_| config::ConfigFile::default());

        let mut changes = Vec::new();
        let mut dry_run_files = Vec::new();
        let mut backups = Vec::new();

        // Optimize Cargo.toml files
        let (cargo_changes, cargo_dry_run, cargo_backups) = self.optimize_cargo_files(dry_run)?;
        changes.extend(cargo_changes);
        dry_run_files.extend(cargo_dry_run);
        backups.extend(cargo_backups);

        // Apply build-std optimization if on nightly
        let (build_std_changes, build_std_dry_run) = self.optimize_build_std_if_needed(dry_run)?;
        changes.extend(build_std_changes);
        dry_run_files.extend(build_std_dry_run);

        Ok((changes, dry_run_files, backups))
    }

    /// Optimize all Cargo.toml files in the workspace
    fn optimize_cargo_files(&self, dry_run: bool) -> Result<OptimizationResult> {
        let cargo_finder = optimizer::CargoFileFinder::new(&self.project_root);
        let cargo_editor = optimizer::CargoTomlEditor::new();
        let opt_config = optimizer::OptimizationConfig::default();

        let mut changes = Vec::new();
        let mut dry_run_files = Vec::new();
        let mut backups = Vec::new();
        let cargo_tomls = cargo_finder.find_cargo_tomls()?;

        for toml_path in cargo_tomls {
            let relative = toml_path
                .strip_prefix(&self.project_root)
                .unwrap_or(&toml_path)
                .display()
                .to_string();

            if dry_run {
                dry_run_files.push(relative);
            } else {
                // Create backup before modification
                if let Ok(original_content) = std::fs::read_to_string(&toml_path) {
                    backups.push((toml_path.clone(), original_content));
                }

                match cargo_editor.optimize_cargo_toml(&toml_path, &opt_config, None, dry_run) {
                    Ok(change_list) => {
                        changes.extend(change_list);
                    }
                    Err(_) => {
                        // Skip files that fail to optimize
                        continue;
                    }
                }
            }
        }

        Ok((changes, dry_run_files, backups))
    }

    /// Apply build-std optimization if nightly toolchain is available
    fn optimize_build_std_if_needed(&self, dry_run: bool) -> Result<(Vec<String>, Vec<String>)> {
        let mut changes = Vec::new();
        let mut dry_run_files = Vec::new();

        // Check for nightly toolchain and build-std optimization
        if let Ok(true) = crate::toolchain::ToolchainDetector::new().is_nightly_toolchain() {
            let build_std_optimizer = optimizer::BuildStdOptimizer::new(&self.project_root);

            if !build_std_optimizer.is_configured().unwrap_or(false) {
                if dry_run {
                    dry_run_files.push("build-std configuration".to_string());
                } else {
                    let build_std_config = optimizer::BuildStdConfig::minimal();

                    if let Ok(build_std_changes) =
                        build_std_optimizer.apply_build_std(&build_std_config, dry_run)
                    {
                        changes.extend(build_std_changes);
                    }
                }
            }
        }

        Ok((changes, dry_run_files))
    }

    /// Rollback Cargo.toml files from backups
    fn rollback_cargo_tomls(&self, backups: &[(PathBuf, String)]) -> Result<()> {
        for (path, content) in backups {
            std::fs::write(path, content)?;
        }
        Ok(())
    }

    /// Phase 2: Run the build pipeline
    fn run_build_pipeline(&self) -> Result<pipeline::SizeMetrics> {
        use crate::error::WasmSlimError;

        let _config = config::ConfigLoader::load(&self.project_root)
            .unwrap_or_else(|_| config::ConfigFile::default());

        let pipeline_config = pipeline::PipelineConfig {
            opt_level: pipeline::WasmOptLevel::Oz,
            run_wasm_snip: true,
            ..Default::default()
        };

        let build_pipeline = pipeline::BuildPipeline::new(&self.project_root, pipeline_config);
        build_pipeline
            .build()
            .map_err(|e| anyhow::Error::from(WasmSlimError::from(e)))
    }

    /// Phase 3: Check CI/CD budget
    fn check_budget(&self, metrics: &pipeline::SizeMetrics) -> Result<(Option<bool>, Option<u64>)> {
        let config = config::ConfigLoader::load(&self.project_root)
            .unwrap_or_else(|_| config::ConfigFile::default());

        if let Some(budget) = &config.size_budget {
            if let Some(max_size_kb) = budget.max_size_kb {
                let max_size = max_size_kb * 1024;
                let passed = metrics.after_bytes <= max_size;

                if !passed {
                    anyhow::bail!(
                        "WASM bundle size ({} bytes) exceeds maximum ({} bytes)",
                        metrics.after_bytes,
                        max_size
                    );
                }

                return Ok((Some(passed), Some(max_size)));
            }
        }

        Ok((None, None))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::{Path, PathBuf};

    #[test]
    fn test_build_workflow_new_with_different_paths() {
        let paths = vec![
            Path::new("/tmp"),
            Path::new("/home/user/project"),
            Path::new("."),
            Path::new("./relative/path"),
        ];

        for path in paths {
            let workflow = BuildWorkflow::new(path);
            assert_eq!(workflow.project_root, PathBuf::from(path));
        }
    }

    #[test]
    fn test_build_workflow_stores_project_root() {
        let root = Path::new("/test/project");
        let workflow = BuildWorkflow::new(root);
        assert_eq!(workflow.project_root, root);
    }

    #[test]
    fn test_build_result_can_be_created() {
        use crate::pipeline::SizeMetrics;

        let result = BuildResult {
            cargo_changes: vec!["test".to_string()],
            metrics: SizeMetrics {
                before_bytes: 1000,
                after_bytes: 800,
            },
            budget_check_passed: None,
            budget_threshold: None,
            dry_run: false,
            dry_run_files: vec![],
        };
        assert_eq!(result.cargo_changes.len(), 1);
        assert_eq!(result.metrics.before_bytes, 1000);
        assert_eq!(result.metrics.after_bytes, 800);
    }

    #[test]
    fn test_build_result_with_budget_check() {
        use crate::pipeline::SizeMetrics;

        let result = BuildResult {
            cargo_changes: vec![],
            metrics: SizeMetrics {
                before_bytes: 2000,
                after_bytes: 1500,
            },
            budget_check_passed: Some(true),
            budget_threshold: Some(2000),
            dry_run: false,
            dry_run_files: vec![],
        };
        assert_eq!(result.budget_check_passed, Some(true));
        assert_eq!(result.budget_threshold, Some(2000));
    }

    #[test]
    fn test_build_result_with_failed_budget() {
        use crate::pipeline::SizeMetrics;

        let result = BuildResult {
            cargo_changes: vec![],
            metrics: SizeMetrics {
                before_bytes: 1000,
                after_bytes: 2500,
            },
            budget_check_passed: Some(false),
            budget_threshold: Some(2000),
            dry_run: false,
            dry_run_files: vec![],
        };
        assert_eq!(result.budget_check_passed, Some(false));
        assert!(result.metrics.after_bytes > result.budget_threshold.unwrap());
    }

    #[test]
    fn test_build_result_dry_run_mode() {
        use crate::pipeline::SizeMetrics;

        let result = BuildResult {
            cargo_changes: vec![],
            metrics: SizeMetrics {
                before_bytes: 1000,
                after_bytes: 1000,
            },
            budget_check_passed: None,
            budget_threshold: None,
            dry_run: true,
            dry_run_files: vec!["file1.toml".to_string(), "file2.toml".to_string()],
        };
        assert!(result.dry_run);
        assert_eq!(result.dry_run_files.len(), 2);
    }

    #[test]
    fn test_build_result_with_multiple_cargo_changes() {
        use crate::pipeline::SizeMetrics;

        let result = BuildResult {
            cargo_changes: vec![
                "change1".to_string(),
                "change2".to_string(),
                "change3".to_string(),
            ],
            metrics: SizeMetrics {
                before_bytes: 2000,
                after_bytes: 1000,
            },
            budget_check_passed: None,
            budget_threshold: None,
            dry_run: false,
            dry_run_files: vec![],
        };
        assert_eq!(result.cargo_changes.len(), 3);
        assert!(result.metrics.before_bytes > result.metrics.after_bytes);
    }

    #[test]
    fn test_optimization_result_empty_tuple() {
        let result: OptimizationResult = (vec![], vec![], vec![]);
        assert!(result.0.is_empty());
        assert!(result.1.is_empty());
        assert!(result.2.is_empty());
    }

    #[test]
    fn test_optimization_result_with_changes() {
        let result: OptimizationResult = (
            vec!["change1".to_string(), "change2".to_string()],
            vec![],
            vec![],
        );
        assert_eq!(result.0.len(), 2);
        assert!(result.1.is_empty());
        assert!(result.2.is_empty());
    }

    #[test]
    fn test_optimization_result_with_dry_run_files() {
        let result: OptimizationResult = (
            vec![],
            vec!["file1.toml".to_string(), "file2.toml".to_string()],
            vec![],
        );
        assert!(result.0.is_empty());
        assert_eq!(result.1.len(), 2);
        assert!(result.2.is_empty());
    }

    #[test]
    fn test_optimization_result_with_backups() {
        let result: OptimizationResult = (
            vec![],
            vec![],
            vec![
                (PathBuf::from("/path/backup1"), "content1".to_string()),
                (PathBuf::from("/path/backup2"), "content2".to_string()),
            ],
        );
        assert!(result.0.is_empty());
        assert!(result.1.is_empty());
        assert_eq!(result.2.len(), 2);
    }

    #[test]
    fn test_optimization_result_complete() {
        let result: OptimizationResult = (
            vec!["change".to_string()],
            vec!["file.toml".to_string()],
            vec![(PathBuf::from("/backup"), "content".to_string())],
        );
        assert_eq!(result.0.len(), 1);
        assert_eq!(result.1.len(), 1);
        assert_eq!(result.2.len(), 1);
    }

    #[test]
    fn test_build_workflow_can_be_created() {
        let workflow = BuildWorkflow::new(Path::new("/tmp"));
        assert_eq!(workflow.project_root, PathBuf::from("/tmp"));
    }

    #[test]
    fn test_rollback_cargo_tomls_restores_content() {
        use std::fs;
        use tempfile::NamedTempFile;

        let workflow = BuildWorkflow::new(Path::new("/tmp"));

        // Create temporary files with original content
        let temp_file1 = NamedTempFile::new().unwrap();
        let temp_file2 = NamedTempFile::new().unwrap();

        let original_content1 = "original content 1";
        let original_content2 = "original content 2";

        fs::write(temp_file1.path(), "modified").unwrap();
        fs::write(temp_file2.path(), "modified").unwrap();

        let backups = vec![
            (
                temp_file1.path().to_path_buf(),
                original_content1.to_string(),
            ),
            (
                temp_file2.path().to_path_buf(),
                original_content2.to_string(),
            ),
        ];

        // Test rollback
        let result = workflow.rollback_cargo_tomls(&backups);
        assert!(result.is_ok());

        // Verify content restored
        assert_eq!(
            fs::read_to_string(temp_file1.path()).unwrap(),
            original_content1
        );
        assert_eq!(
            fs::read_to_string(temp_file2.path()).unwrap(),
            original_content2
        );
    }

    #[test]
    fn test_rollback_cargo_tomls_with_empty_backups() {
        let workflow = BuildWorkflow::new(Path::new("/tmp"));
        let backups = [];

        let result = workflow.rollback_cargo_tomls(&backups);
        assert!(result.is_ok());
    }

    #[test]
    fn test_build_result_debug_format() {
        use crate::pipeline::SizeMetrics;

        let result = BuildResult {
            cargo_changes: vec!["test".to_string()],
            metrics: SizeMetrics {
                before_bytes: 1000,
                after_bytes: 800,
            },
            budget_check_passed: None,
            budget_threshold: None,
            dry_run: false,
            dry_run_files: vec![],
        };

        // Verify Debug trait is implemented
        let debug_str = format!("{:?}", result);
        assert!(debug_str.contains("BuildResult"));
    }

    #[test]
    fn test_build_result_all_fields() {
        use crate::pipeline::SizeMetrics;

        let result = BuildResult {
            cargo_changes: vec!["opt-level=z".to_string(), "lto=true".to_string()],
            metrics: SizeMetrics {
                before_bytes: 5000,
                after_bytes: 3000,
            },
            budget_check_passed: Some(true),
            budget_threshold: Some(4000),
            dry_run: true,
            dry_run_files: vec!["Cargo.toml".to_string()],
        };

        assert_eq!(result.cargo_changes.len(), 2);
        assert_eq!(result.metrics.before_bytes, 5000);
        assert_eq!(result.metrics.after_bytes, 3000);
        assert_eq!(result.budget_check_passed, Some(true));
        assert_eq!(result.budget_threshold, Some(4000));
        assert!(result.dry_run);
        assert_eq!(result.dry_run_files.len(), 1);
    }

    #[test]
    fn test_optimization_result_type_alias() {
        // Test that OptimizationResult type is correctly defined
        let _result: OptimizationResult = (
            vec!["change".to_string()],
            vec!["file.toml".to_string()],
            vec![(PathBuf::from("/backup"), "content".to_string())],
        );

        // Type checking passes means the alias is correct
    }
}