#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
use anyhow::anyhow;
use std::io;
use std::str::FromStr;
use twiggy_ir as ir;
pub trait Analyze {
type Data: Emit;
fn analyze(items: &mut ir::Items) -> anyhow::Result<Self::Data>;
}
#[derive(Clone, Copy, Debug)]
pub enum ParseMode {
Wasm,
#[cfg(feature = "dwarf")]
Dwarf,
Auto,
}
impl Default for ParseMode {
fn default() -> ParseMode {
ParseMode::Auto
}
}
impl FromStr for ParseMode {
type Err = anyhow::Error;
fn from_str(s: &str) -> anyhow::Result<Self> {
match s {
"wasm" => Ok(ParseMode::Wasm),
#[cfg(feature = "dwarf")]
"dwarf" => Ok(ParseMode::Dwarf),
"auto" => Ok(ParseMode::Auto),
_ => Err(anyhow!("Unknown parse mode: {}", s)),
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum OutputFormat {
#[cfg(feature = "emit_text")]
Text,
#[cfg(feature = "emit_csv")]
Csv,
#[cfg(feature = "emit_json")]
Json,
}
#[cfg(feature = "emit_text")]
#[cfg(feature = "emit_csv")]
#[cfg(feature = "emit_json")]
impl Default for OutputFormat {
fn default() -> OutputFormat {
OutputFormat::Text
}
}
impl FromStr for OutputFormat {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
#[cfg(feature = "emit_text")]
"text" => Ok(OutputFormat::Text),
#[cfg(feature = "emit_json")]
"json" => Ok(OutputFormat::Json),
#[cfg(feature = "emit_csv")]
"csv" => Ok(OutputFormat::Csv),
_ => Err(anyhow!("Unknown output format: {}", s)),
}
}
}
pub trait Emit {
fn emit(
&self,
items: &ir::Items,
destination: &mut dyn io::Write,
format: OutputFormat,
) -> anyhow::Result<()> {
match format {
#[cfg(feature = "emit_text")]
OutputFormat::Text => self.emit_text(items, destination),
#[cfg(feature = "emit_csv")]
OutputFormat::Csv => self.emit_csv(items, destination),
#[cfg(feature = "emit_json")]
OutputFormat::Json => self.emit_json(items, destination),
}
}
#[cfg(feature = "emit_text")]
fn emit_text(&self, items: &ir::Items, destination: &mut dyn io::Write) -> anyhow::Result<()>;
#[cfg(feature = "emit_csv")]
fn emit_csv(&self, items: &ir::Items, destination: &mut dyn io::Write) -> anyhow::Result<()>;
#[cfg(feature = "emit_json")]
fn emit_json(&self, items: &ir::Items, destination: &mut dyn io::Write) -> anyhow::Result<()>;
}