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
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
//! Enhanced error types with contextual suggestions
//!
//! Provides structured error types that include:
//! - Actionable error messages
//! - Suggested fixes and recovery actions
//! - Documentation links
//! - Proper exit codes for CI/CD
//!
//! # Examples
//!
//! ```no_run
//! use wasm_slim::error::WasmSlimError;
//! use wasm_slim::pipeline::{BuildPipeline, PipelineConfig};
//!
//! let pipeline = BuildPipeline::new(".", PipelineConfig::default());
//!
//! match pipeline.build() {
//!     Ok(metrics) => {
//!         println!("Build successful: {} bytes", metrics.after_bytes);
//!     }
//!     Err(e) => {
//!         eprintln!("Build failed: {}", e);
//!         // Errors contain actionable context
//!         std::process::exit(1);
//!     }
//! }
//! ```

use std::path::PathBuf;
use thiserror::Error;

use crate::pipeline::PipelineError;

/// Enhanced wasm-slim errors with contextual suggestions
#[derive(Error, Debug)]
pub enum WasmSlimError {
    /// Required tool is not installed
    #[error("Tool not installed: {tool}")]
    ToolMissing {
        /// Tool name
        tool: String,
        /// Installation command
        install_cmd: String,
        /// Optional documentation URL
        docs_url: Option<String>,
    },

    /// Configuration file not found
    #[error("Configuration file not found: {path}")]
    ConfigNotFound {
        /// Path to config file
        path: PathBuf,
        #[source]
        /// IO error source
        source: std::io::Error,
    },

    /// Invalid template name
    #[error("Invalid template: '{name}'")]
    InvalidTemplate {
        /// Invalid template name
        name: String,
        /// List of valid template names
        available: Vec<String>,
    },

    /// File not found during operation
    #[error("File not found: {path}")]
    FileNotFound {
        /// Path to missing file
        path: PathBuf,
        /// Operation that required the file
        operation: String,
    },

    /// WASM file required but not provided
    #[error("WASM file required for {operation}")]
    WasmFileRequired {
        /// Operation requiring WASM file
        operation: String,
    },

    /// Size budget exceeded
    #[error("WASM bundle size ({actual} bytes) exceeds maximum ({max_allowed} bytes)")]
    BudgetExceeded {
        /// Actual bundle size
        actual: u64,
        /// Maximum allowed size
        max_allowed: u64,
        /// Percentage over budget
        percentage_over: f64,
    },

    /// Build command failed
    #[error("Build command failed: {command}")]
    BuildFailed {
        /// Command that failed
        command: String,
        /// Error output
        stderr: String,
    },

    /// Invalid analysis mode
    #[error("Unknown analysis mode: '{mode}'")]
    InvalidAnalysisMode {
        /// Invalid mode name
        mode: String,
        /// List of valid modes
        valid_modes: Vec<String>,
    },

    /// Generic I/O error with context
    #[error("I/O error: {context}")]
    Io {
        /// Context about where the error occurred
        context: String,
        #[source]
        /// IO error source
        source: std::io::Error,
    },

    /// Pipeline error during build
    #[error("pipeline error: {0}")]
    Pipeline(#[from] PipelineError),
}

impl WasmSlimError {
    /// Get actionable suggestion for resolving this error.
    ///
    /// Returns a user-friendly suggestion for how to fix the error, if available.
    ///
    /// # Examples
    ///
    /// ```
    /// use wasm_slim::error::WasmSlimError;
    ///
    /// let error = WasmSlimError::ToolMissing {
    ///     tool: "wasm-opt".to_string(),
    ///     install_cmd: "cargo install wasm-opt".to_string(),
    ///     docs_url: None,
    /// };
    ///
    /// let suggestion = error.suggestion();
    /// assert!(suggestion.is_some());
    /// assert!(suggestion.unwrap().contains("cargo install"));
    /// ```
    pub fn suggestion(&self) -> Option<String> {
        match self {
            Self::ToolMissing { install_cmd, .. } => Some(format!("Install with: {}", install_cmd)),
            Self::ConfigNotFound { .. } => {
                Some("Run 'wasm-slim init' to create a configuration file".to_string())
            }
            Self::InvalidTemplate { available, .. } => Some(format!(
                "Available templates: {}\nRun 'wasm-slim init --list' to see all templates",
                available.join(", ")
            )),
            Self::FileNotFound { path, operation } => Some(format!(
                "Ensure {} exists before running {}",
                path.display(),
                operation
            )),
            Self::WasmFileRequired { operation } => Some(format!(
                "Provide WASM file path: wasm-slim analyze {} <path/to/file.wasm>",
                operation
            )),
            Self::BudgetExceeded {
                percentage_over, ..
            } => Some(format!(
                "Bundle is {:.1}% over budget. Consider:\n  \
                     - Using 'aggressive' template\n  \
                     - Running 'wasm-slim analyze assets' to find large embedded resources\n  \
                     - Running 'wasm-slim analyze deps' to identify heavy dependencies",
                percentage_over
            )),
            Self::BuildFailed { stderr, .. } => {
                if stderr.contains("wasm32-unknown-unknown") {
                    Some(
                        "Install WASM target: rustup target add wasm32-unknown-unknown".to_string(),
                    )
                } else {
                    Some("Check the build errors above and fix compilation issues".to_string())
                }
            }
            Self::InvalidAnalysisMode { valid_modes, .. } => {
                Some(format!("Valid modes: {}", valid_modes.join(", ")))
            }
            Self::Io { context, .. } => Some(format!(
                "Check file permissions and that {} is accessible",
                context
            )),
            Self::Pipeline(e) => {
                let msg = e.to_string();
                if msg.contains("wasm32-unknown-unknown") {
                    Some(
                        "Install WASM target: rustup target add wasm32-unknown-unknown".to_string(),
                    )
                } else if msg.contains("wasm-opt") || msg.contains("wasm-bindgen") {
                    Some("Ensure required tools are installed. Run: cargo install wasm-bindgen-cli && cargo install wasm-opt".to_string())
                } else {
                    Some("Check the build errors above and fix compilation issues".to_string())
                }
            }
        }
    }

    /// Get documentation URL for this error.
    ///
    /// Returns a URL to relevant documentation for resolving this error type.
    ///
    /// # Examples
    ///
    /// ```
    /// use wasm_slim::error::WasmSlimError;
    ///
    /// let error = WasmSlimError::ToolMissing {
    ///     tool: "wasm-bindgen".to_string(),
    ///     install_cmd: "cargo install wasm-bindgen-cli".to_string(),
    ///     docs_url: Some("https://rustwasm.github.io/wasm-bindgen/".to_string()),
    /// };
    ///
    /// assert!(error.docs_url().is_some());
    /// assert_eq!(error.docs_url().unwrap(), "https://rustwasm.github.io/wasm-bindgen/");
    /// ```
    pub fn docs_url(&self) -> Option<&str> {
        match self {
            Self::ToolMissing { docs_url, .. } => docs_url.as_deref(),
            Self::ConfigNotFound { .. } => {
                Some("https://github.com/vitalratel/wasm-slim#configuration")
            }
            Self::BudgetExceeded { .. } => {
                Some("https://github.com/vitalratel/wasm-slim#ci-cd-integration")
            }
            Self::Pipeline(_) => Some("https://github.com/vitalratel/wasm-slim#build-pipeline"),
            _ => None,
        }
    }

    /// Get appropriate exit code for this error.
    ///
    /// Returns Unix-style exit codes based on the error type, following sysexits.h conventions.
    ///
    /// # Examples
    ///
    /// ```
    /// use wasm_slim::error::WasmSlimError;
    ///
    /// let error = WasmSlimError::ToolMissing {
    ///     tool: "wasm-opt".to_string(),
    ///     install_cmd: "cargo install wasm-opt".to_string(),
    ///     docs_url: None,
    /// };
    ///
    /// assert_eq!(error.exit_code(), 127); // Command not found
    ///
    /// let budget_error = WasmSlimError::BudgetExceeded {
    ///     actual: 1500000, // 1500 KB in bytes
    ///     max_allowed: 1000000, // 1000 KB in bytes
    ///     percentage_over: 50.0,
    /// };
    ///
    /// assert_eq!(budget_error.exit_code(), 1); // Generic error for CI failure
    /// ```
    pub fn exit_code(&self) -> i32 {
        match self {
            Self::ToolMissing { .. } => 127, // Command not found (Unix convention)
            Self::ConfigNotFound { .. } => 66, // EX_NOINPUT (sysexits.h)
            Self::InvalidTemplate { .. } => 65, // EX_DATAERR
            Self::FileNotFound { .. } => 66, // EX_NOINPUT
            Self::WasmFileRequired { .. } => 64, // EX_USAGE
            Self::BudgetExceeded { .. } => 1, // Generic error (CI should fail)
            Self::BuildFailed { .. } => 1,   // Generic error
            Self::InvalidAnalysisMode { .. } => 64, // EX_USAGE
            Self::Io { .. } => 74,           // EX_IOERR
            Self::Pipeline(_) => 1,          // Generic error (build failed)
        }
    }
}

impl WasmSlimError {
    /// Returns the pipeline error if this is a `Pipeline` variant.
    pub fn as_pipeline_error(&self) -> Option<&PipelineError> {
        match self {
            Self::Pipeline(e) => Some(e),
            _ => None,
        }
    }
}

/// Error formatter with colors and structured output
pub struct ErrorFormatter;

impl ErrorFormatter {
    /// Format error with suggestions and documentation links
    pub fn format(error: &anyhow::Error) -> String {
        use console::style;

        let mut output = String::new();

        // Main error message
        output.push_str(&format!("{} {}\n", style("error:").red().bold(), error));

        // Error chain (caused by)
        let mut source = error.source();
        let mut indent = 1;
        while let Some(err) = source {
            output.push_str(&format!(
                "{}{} {}\n",
                "  ".repeat(indent),
                style("caused by:").yellow(),
                err
            ));
            source = err.source();
            indent += 1;
        }

        // Try to downcast to WasmSlimError for suggestions
        if let Some(ws_error) = error.downcast_ref::<WasmSlimError>() {
            // Suggestions
            if let Some(suggestion) = ws_error.suggestion() {
                output.push_str(&format!(
                    "\n{} {}\n",
                    style("help:").cyan().bold(),
                    suggestion
                ));
            }

            // Documentation link
            if let Some(docs) = ws_error.docs_url() {
                output.push_str(&format!("{} {}\n", style("docs:").blue(), docs));
            }
        }

        output
    }

    /// Get exit code from error
    pub fn exit_code(error: &anyhow::Error) -> i32 {
        if let Some(ws_error) = error.downcast_ref::<WasmSlimError>() {
            ws_error.exit_code()
        } else {
            1 // Generic error
        }
    }
}

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

    #[test]
    fn test_tool_missing_has_suggestion() {
        let err = WasmSlimError::ToolMissing {
            tool: "twiggy".to_string(),
            install_cmd: "cargo install twiggy".to_string(),
            docs_url: Some("https://rustwasm.github.io/twiggy/".to_string()),
        };

        let suggestion = err
            .suggestion()
            .expect("ToolMissing should have suggestion");
        assert!(suggestion.contains("cargo install twiggy"));
    }

    #[test]
    fn test_budget_exceeded_has_actionable_suggestion() {
        let err = WasmSlimError::BudgetExceeded {
            actual: 1_500_000,
            max_allowed: 1_000_000,
            percentage_over: 50.0,
        };

        let suggestion = err
            .suggestion()
            .expect("BudgetExceeded should have suggestion");
        assert!(suggestion.contains("aggressive"));
        assert!(suggestion.contains("analyze assets"));
    }

    #[test]
    fn test_invalid_template_lists_alternatives() {
        let err = WasmSlimError::InvalidTemplate {
            name: "foo".to_string(),
            available: vec![
                "minimal".to_string(),
                "balanced".to_string(),
                "aggressive".to_string(),
            ],
        };

        let suggestion = err
            .suggestion()
            .expect("InvalidTemplate should have suggestion");
        assert!(suggestion.contains("minimal"));
        assert!(suggestion.contains("balanced"));
        assert!(suggestion.contains("aggressive"));
    }

    #[test]
    fn test_exit_codes_follow_conventions() {
        let tool_err = WasmSlimError::ToolMissing {
            tool: "test".to_string(),
            install_cmd: "test".to_string(),
            docs_url: None,
        };
        assert_eq!(tool_err.exit_code(), 127); // Command not found

        let config_err = WasmSlimError::ConfigNotFound {
            path: PathBuf::from("test"),
            source: std::io::Error::new(std::io::ErrorKind::NotFound, "test"),
        };
        assert_eq!(config_err.exit_code(), 66); // No input file
    }

    #[test]
    fn test_config_not_found_has_suggestion() {
        let err = WasmSlimError::ConfigNotFound {
            path: PathBuf::from("wasm-slim.toml"),
            source: std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
        };

        let suggestion = err
            .suggestion()
            .expect("ConfigNotFound should have suggestion");
        assert!(suggestion.contains("wasm-slim init"));
    }

    #[test]
    fn test_file_not_found_has_context() {
        let err = WasmSlimError::FileNotFound {
            path: PathBuf::from("output.wasm"),
            operation: "analyze".to_string(),
        };

        let suggestion = err
            .suggestion()
            .expect("FileNotFound should have suggestion");
        assert!(suggestion.contains("output.wasm"));
        assert!(suggestion.contains("analyze"));
    }

    #[test]
    fn test_wasm_file_required_has_usage_hint() {
        let err = WasmSlimError::WasmFileRequired {
            operation: "bloat".to_string(),
        };

        let suggestion = err
            .suggestion()
            .expect("WasmFileRequired should have suggestion");
        assert!(suggestion.contains("bloat"));
        assert!(suggestion.contains(".wasm"));
    }

    #[test]
    fn test_build_failed_with_missing_target() {
        let err = WasmSlimError::BuildFailed {
            command: "cargo build".to_string(),
            stderr: "error: target wasm32-unknown-unknown not found".to_string(),
        };

        let suggestion = err
            .suggestion()
            .expect("BuildFailed should have suggestion for missing target");
        assert!(suggestion.contains("rustup target add"));
        assert!(suggestion.contains("wasm32-unknown-unknown"));
    }

    #[test]
    fn test_build_failed_generic_error() {
        let err = WasmSlimError::BuildFailed {
            command: "cargo build".to_string(),
            stderr: "error: could not compile".to_string(),
        };

        let suggestion = err
            .suggestion()
            .expect("BuildFailed should have suggestion");
        assert!(suggestion.contains("build errors"));
    }

    #[test]
    fn test_invalid_analysis_mode_lists_valid_modes() {
        let err = WasmSlimError::InvalidAnalysisMode {
            mode: "invalid".to_string(),
            valid_modes: vec![
                "bloat".to_string(),
                "assets".to_string(),
                "deps".to_string(),
            ],
        };

        let suggestion = err
            .suggestion()
            .expect("InvalidAnalysisMode should have suggestion");
        assert!(suggestion.contains("bloat"));
        assert!(suggestion.contains("assets"));
        assert!(suggestion.contains("deps"));
    }

    #[test]
    fn test_io_error_has_context() {
        let err = WasmSlimError::Io {
            context: "reading Cargo.toml".to_string(),
            source: std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied"),
        };

        let suggestion = err.suggestion().expect("Io error should have suggestion");
        assert!(suggestion.contains("permissions"));
        assert!(suggestion.contains("reading Cargo.toml"));
    }

    #[test]
    fn test_all_error_variants_have_exit_codes() {
        let errors = vec![
            WasmSlimError::ToolMissing {
                tool: "test".to_string(),
                install_cmd: "test".to_string(),
                docs_url: None,
            },
            WasmSlimError::ConfigNotFound {
                path: PathBuf::from("test"),
                source: std::io::Error::new(std::io::ErrorKind::NotFound, "test"),
            },
            WasmSlimError::InvalidTemplate {
                name: "test".to_string(),
                available: vec![],
            },
            WasmSlimError::FileNotFound {
                path: PathBuf::from("test"),
                operation: "test".to_string(),
            },
            WasmSlimError::WasmFileRequired {
                operation: "test".to_string(),
            },
            WasmSlimError::BudgetExceeded {
                actual: 100,
                max_allowed: 50,
                percentage_over: 100.0,
            },
            WasmSlimError::BuildFailed {
                command: "test".to_string(),
                stderr: "test".to_string(),
            },
            WasmSlimError::InvalidAnalysisMode {
                mode: "test".to_string(),
                valid_modes: vec![],
            },
            WasmSlimError::Io {
                context: "test".to_string(),
                source: std::io::Error::other("test"),
            },
            WasmSlimError::Pipeline(crate::pipeline::PipelineError::BuildFailed(
                "test".to_string(),
            )),
        ];

        for err in errors {
            let exit_code = err.exit_code();
            assert!(
                exit_code > 0,
                "Error {:?} should have non-zero exit code",
                err
            );
            assert!(exit_code < 256, "Exit code should fit in a byte");
        }
    }

    #[test]
    fn test_all_error_variants_have_suggestions() {
        let errors = vec![
            WasmSlimError::ToolMissing {
                tool: "test".to_string(),
                install_cmd: "cargo install test".to_string(),
                docs_url: None,
            },
            WasmSlimError::ConfigNotFound {
                path: PathBuf::from("test"),
                source: std::io::Error::new(std::io::ErrorKind::NotFound, "test"),
            },
            WasmSlimError::InvalidTemplate {
                name: "test".to_string(),
                available: vec!["minimal".to_string()],
            },
            WasmSlimError::FileNotFound {
                path: PathBuf::from("test.wasm"),
                operation: "analyze".to_string(),
            },
            WasmSlimError::WasmFileRequired {
                operation: "bloat".to_string(),
            },
            WasmSlimError::BudgetExceeded {
                actual: 100,
                max_allowed: 50,
                percentage_over: 100.0,
            },
            WasmSlimError::BuildFailed {
                command: "cargo build".to_string(),
                stderr: "compilation failed".to_string(),
            },
            WasmSlimError::InvalidAnalysisMode {
                mode: "invalid".to_string(),
                valid_modes: vec!["bloat".to_string()],
            },
            WasmSlimError::Io {
                context: "reading file".to_string(),
                source: std::io::Error::other("test"),
            },
            WasmSlimError::Pipeline(crate::pipeline::PipelineError::BuildFailed(
                "build failed".to_string(),
            )),
        ];

        for err in &errors {
            let suggestion = err.suggestion();
            assert!(
                suggestion.is_some(),
                "Error {:?} should have a suggestion",
                err
            );
            assert!(
                !suggestion.unwrap().is_empty(),
                "Suggestion should not be empty"
            );
        }
    }

    #[test]
    fn test_pipeline_error_has_suggestion() {
        let err = WasmSlimError::Pipeline(crate::pipeline::PipelineError::BuildFailed(
            "cargo build failed".to_string(),
        ));

        let suggestion = err
            .suggestion()
            .expect("Pipeline error should have suggestion");
        assert!(!suggestion.is_empty());
    }

    #[test]
    fn test_pipeline_error_with_wasm_target_has_install_suggestion() {
        let err = WasmSlimError::Pipeline(crate::pipeline::PipelineError::BuildFailed(
            "target wasm32-unknown-unknown not found".to_string(),
        ));

        let suggestion = err
            .suggestion()
            .expect("Pipeline error should have suggestion");
        assert!(suggestion.contains("rustup target add"));
        assert!(suggestion.contains("wasm32-unknown-unknown"));
    }

    #[test]
    fn test_pipeline_error_accessor() {
        let pipeline_err = crate::pipeline::PipelineError::BuildFailed("test".to_string());
        let err = WasmSlimError::Pipeline(pipeline_err);

        assert!(err.as_pipeline_error().is_some());

        let other_err = WasmSlimError::Io {
            context: "test".to_string(),
            source: std::io::Error::other("test"),
        };
        assert!(other_err.as_pipeline_error().is_none());
    }
}