use std::fmt::Display;
use std::str::FromStr;
use allocative::Allocative;
use dupe::Dupe;
#[derive(Debug, PartialEq, Eq, Hash, Clone, Dupe, Allocative)]
#[non_exhaustive]
pub enum ProfileMode {
HeapSummaryAllocated,
HeapSummaryRetained,
HeapFlameAllocated,
HeapFlameRetained,
Statement,
Coverage,
Bytecode,
BytecodePairs,
TimeFlame,
Typecheck,
None,
}
impl Display for ProfileMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name())
}
}
impl ProfileMode {
pub(crate) const ALL: [ProfileMode; 11] = [
ProfileMode::HeapSummaryAllocated,
ProfileMode::HeapSummaryRetained,
ProfileMode::HeapFlameAllocated,
ProfileMode::HeapFlameRetained,
ProfileMode::Statement,
ProfileMode::Coverage,
ProfileMode::Bytecode,
ProfileMode::BytecodePairs,
ProfileMode::TimeFlame,
ProfileMode::Typecheck,
ProfileMode::None,
];
pub(crate) fn name(&self) -> &str {
match self {
ProfileMode::HeapSummaryAllocated => "heap-summary-allocated",
ProfileMode::HeapSummaryRetained => "heap-summary-retained",
ProfileMode::HeapFlameAllocated => "heap-flame-allocated",
ProfileMode::HeapFlameRetained => "heap-flame-retained",
ProfileMode::Statement => "statement",
ProfileMode::Coverage => "coverage",
ProfileMode::Bytecode => "bytecode",
ProfileMode::BytecodePairs => "bytecode-pairs",
ProfileMode::TimeFlame => "time-flame",
ProfileMode::Typecheck => "typecheck",
ProfileMode::None => "none",
}
}
pub fn requires_frozen_module(&self) -> bool {
match self {
ProfileMode::HeapSummaryRetained | ProfileMode::HeapFlameRetained => true,
_ => false,
}
}
}
impl FromStr for ProfileMode {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
for mode in Self::ALL {
if s == mode.name() {
return Ok(mode);
}
}
Err(anyhow::anyhow!("Invalid ProfileMode: `{}`", s))
}
}