1#![deny(missing_docs)]
3#![deny(missing_debug_implementations)]
4
5use anyhow::anyhow;
6use std::io;
7use std::str::FromStr;
8use twiggy_ir as ir;
9
10pub trait Analyze {
13 type Data: Emit;
15
16 fn analyze(items: &mut ir::Items) -> anyhow::Result<Self::Data>;
18}
19
20#[derive(Clone, Copy, Debug)]
22pub enum ParseMode {
23 Wasm,
25 #[cfg(feature = "dwarf")]
27 Dwarf,
28 Auto,
30}
31
32impl Default for ParseMode {
33 fn default() -> ParseMode {
34 ParseMode::Auto
35 }
36}
37
38impl FromStr for ParseMode {
39 type Err = anyhow::Error;
40
41 fn from_str(s: &str) -> anyhow::Result<Self> {
42 match s {
43 "wasm" => Ok(ParseMode::Wasm),
44 #[cfg(feature = "dwarf")]
45 "dwarf" => Ok(ParseMode::Dwarf),
46 "auto" => Ok(ParseMode::Auto),
47 _ => Err(anyhow!("Unknown parse mode: {}", s)),
48 }
49 }
50}
51
52#[derive(Clone, Copy, Debug)]
54pub enum OutputFormat {
55 #[cfg(feature = "emit_text")]
57 Text,
58
59 #[cfg(feature = "emit_csv")]
65 Csv,
66
67 #[cfg(feature = "emit_json")]
69 Json,
70}
71
72#[cfg(feature = "emit_text")]
73#[cfg(feature = "emit_csv")]
74#[cfg(feature = "emit_json")]
75impl Default for OutputFormat {
76 fn default() -> OutputFormat {
77 OutputFormat::Text
78 }
79}
80
81impl FromStr for OutputFormat {
82 type Err = anyhow::Error;
83
84 fn from_str(s: &str) -> Result<Self, Self::Err> {
85 match s {
86 #[cfg(feature = "emit_text")]
87 "text" => Ok(OutputFormat::Text),
88 #[cfg(feature = "emit_json")]
89 "json" => Ok(OutputFormat::Json),
90 #[cfg(feature = "emit_csv")]
91 "csv" => Ok(OutputFormat::Csv),
92 _ => Err(anyhow!("Unknown output format: {}", s)),
93 }
94 }
95}
96
97pub trait Emit {
100 fn emit(
102 &self,
103 items: &ir::Items,
104 destination: &mut dyn io::Write,
105 format: OutputFormat,
106 ) -> anyhow::Result<()> {
107 match format {
108 #[cfg(feature = "emit_text")]
109 OutputFormat::Text => self.emit_text(items, destination),
110 #[cfg(feature = "emit_csv")]
113 OutputFormat::Csv => self.emit_csv(items, destination),
114 #[cfg(feature = "emit_json")]
115 OutputFormat::Json => self.emit_json(items, destination),
116 }
117 }
118
119 #[cfg(feature = "emit_text")]
121 fn emit_text(&self, items: &ir::Items, destination: &mut dyn io::Write) -> anyhow::Result<()>;
122
123 #[cfg(feature = "emit_csv")]
131 fn emit_csv(&self, items: &ir::Items, destination: &mut dyn io::Write) -> anyhow::Result<()>;
132
133 #[cfg(feature = "emit_json")]
135 fn emit_json(&self, items: &ir::Items, destination: &mut dyn io::Write) -> anyhow::Result<()>;
136}