use std::fmt;
use std::str::FromStr;
use crate::error::WasmCheckError;
use crate::size::SizeReport;
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum Metric {
Raw,
Gzip,
Brotli,
}
impl Metric {
pub const ALL: [Metric; 3] = [Metric::Raw, Metric::Gzip, Metric::Brotli];
#[must_use]
pub fn value(self, report: &SizeReport) -> u64 {
match self {
Metric::Raw => report.raw(),
Metric::Gzip => report.gzip(),
Metric::Brotli => report.brotli(),
}
}
}
impl fmt::Display for Metric {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Metric::Raw => "raw",
Metric::Gzip => "gzip",
Metric::Brotli => "brotli",
})
}
}
impl FromStr for Metric {
type Err = WasmCheckError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_ascii_lowercase().as_str() {
"raw" => Ok(Metric::Raw),
"gzip" => Ok(Metric::Gzip),
"brotli" => Ok(Metric::Brotli),
other => Err(WasmCheckError::InvalidMetric(other.to_string())),
}
}
}