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
//! Asset type definitions
use serde::{Deserialize, Serialize};
/// Type of detected asset
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AssetType {
/// Font file (TTF, OTF, WOFF, etc.)
Font,
/// Image file (PNG, JPG, SVG, etc.)
Image,
/// Generic data file
Data,
/// Unknown or unrecognized asset type
Unknown,
}
/// Priority level for externalization recommendation
/// Ordered from lowest to highest priority for correct Ord comparison
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AssetPriority {
/// <2% of bundle or <100KB
Low,
/// 2-5% of bundle or 100-200KB
Medium,
/// 5-10% of bundle or 200-500KB
High,
/// >10% of bundle or >500KB
Critical,
}
impl AssetPriority {
/// Priority thresholds
pub const CRITICAL_PERCENTAGE: f64 = 10.0;
/// Critical size threshold in KB
pub const CRITICAL_SIZE_KB: u64 = 500;
/// High priority percentage threshold
pub const HIGH_PERCENTAGE: f64 = 5.0;
/// High size threshold in KB
pub const HIGH_SIZE_KB: u64 = 200;
/// Medium priority percentage threshold
pub const MEDIUM_PERCENTAGE: f64 = 2.0;
/// Medium size threshold in KB
pub const MEDIUM_SIZE_KB: u64 = 100;
/// Calculate priority based on asset size and bundle size
///
/// Priority is determined by either percentage of bundle or absolute size:
/// - **Critical**: >10% of bundle OR >500KB
/// - **High**: >5% of bundle OR >200KB
/// - **Medium**: >2% of bundle OR >100KB
/// - **Low**: Everything else
///
/// # Arguments
/// * `asset_kb` - Size of the asset in kilobytes
/// * `bundle_kb` - Total bundle size in kilobytes
///
/// # Examples
///
/// Large asset triggers critical priority:
/// ```
/// use wasm_slim::analyzer::asset_types::AssetPriority;
///
/// // 600KB asset is critical regardless of percentage
/// let priority = AssetPriority::from_size(600, 10_000);
/// assert_eq!(priority, AssetPriority::Critical);
/// ```
///
/// Percentage-based priority:
/// ```
/// use wasm_slim::analyzer::asset_types::AssetPriority;
///
/// // 12% of 1MB bundle = 120KB, high percentage triggers critical
/// let priority = AssetPriority::from_size(120, 1_000);
/// assert_eq!(priority, AssetPriority::Critical);
///
/// // 6% of 1MB bundle = 60KB, triggers high priority
/// let priority = AssetPriority::from_size(60, 1_000);
/// assert_eq!(priority, AssetPriority::High);
/// ```
pub fn from_size(asset_kb: u64, bundle_kb: u64) -> Self {
let percentage = if bundle_kb > 0 {
(asset_kb as f64 / bundle_kb as f64) * 100.0
} else {
0.0
};
if percentage > Self::CRITICAL_PERCENTAGE || asset_kb > Self::CRITICAL_SIZE_KB {
AssetPriority::Critical
} else if percentage > Self::HIGH_PERCENTAGE || asset_kb > Self::HIGH_SIZE_KB {
AssetPriority::High
} else if percentage > Self::MEDIUM_PERCENTAGE || asset_kb > Self::MEDIUM_SIZE_KB {
AssetPriority::Medium
} else {
AssetPriority::Low
}
}
}