1use clap::{ArgAction, Args};
2
3use super::CliArgsToRaw;
4
5#[derive(Clone, PartialEq, Eq, Debug, Args)]
6pub struct BuildArgs {
7    #[arg(long = "locked", verbatim_doc_comment)]
9    pub locked: bool,
10
11    #[arg(long = "wasm-symbols", verbatim_doc_comment)]
13    pub wasm_symbols: bool,
14
15    #[arg(long = "wasm-name", verbatim_doc_comment)]
17    pub wasm_name_override: Option<String>,
18
19    #[arg(long = "wasm-suffix", verbatim_doc_comment)]
21    pub wasm_name_suffix: Option<String>,
22
23    #[arg(
25        long = "no-wasm-opt",
26        help = "Skips applying the wasm-opt tool after building the contracts.",
27        action = ArgAction::SetFalse,
28    )]
29    pub wasm_opt: bool,
30
31    #[arg(long = "wat", verbatim_doc_comment)]
33    pub wat: bool,
34
35    #[arg(long = "mir", verbatim_doc_comment)]
37    pub emit_mir: bool,
38
39    #[arg(long = "llvm-ir", verbatim_doc_comment)]
41    pub emit_llvm_ir: bool,
42
43    #[arg(
44        long = "no-imports",
45        help = "Skips extracting the EI imports after building the contracts.",
46        action = ArgAction::SetFalse,
47    )]
48    pub extract_imports: bool,
49
50    #[arg(long = "target-dir-wasm", alias = "target-dir", verbatim_doc_comment)]
54    pub target_dir_wasm: Option<String>,
55
56    #[arg(long = "twiggy-top", verbatim_doc_comment)]
58    pub twiggy_top: bool,
59
60    #[arg(long = "twiggy-paths", verbatim_doc_comment)]
62    pub twiggy_paths: bool,
63
64    #[arg(long = "twiggy-monos", verbatim_doc_comment)]
66    pub twiggy_monos: bool,
67
68    #[arg(long = "twiggy-dominators", verbatim_doc_comment)]
70    pub twiggy_dominators: bool,
71
72    #[deprecated]
74    #[arg(long = "target", verbatim_doc_comment)]
75    pub target: Option<String>,
76
77    #[deprecated]
79    #[arg(long, verbatim_doc_comment)]
80    pub release: bool,
81
82    #[deprecated]
84    #[arg(long = "out-dir", verbatim_doc_comment)]
85    pub out_dir: Option<String>,
86}
87
88impl Default for BuildArgs {
89    #[allow(deprecated)]
90    fn default() -> Self {
91        BuildArgs {
92            locked: false,
93            wasm_symbols: false,
94            wasm_name_override: None,
95            wasm_name_suffix: None,
96            wasm_opt: true,
97            wat: false,
98            emit_mir: false,
99            emit_llvm_ir: false,
100            extract_imports: true,
101            target_dir_wasm: None,
102            twiggy_top: false,
103            twiggy_paths: false,
104            twiggy_monos: false,
105            twiggy_dominators: false,
106            target: None,
107            release: false,
108            out_dir: None,
109        }
110    }
111}
112
113impl BuildArgs {
114    pub fn has_twiggy_call(&self) -> bool {
115        self.twiggy_top || self.twiggy_paths || self.twiggy_monos || self.twiggy_dominators
116    }
117}
118
119impl CliArgsToRaw for BuildArgs {
120    fn to_raw(&self) -> Vec<String> {
121        let mut raw = Vec::new();
122        if self.locked {
123            raw.push("--locked".to_string());
124        }
125        if self.wasm_symbols {
126            raw.push("--wasm-symbols".to_string());
127        }
128        if let Some(wasm_name_override) = &self.wasm_name_override {
129            raw.push("--wasm-name".to_string());
130            raw.push(wasm_name_override.clone())
131        }
132        if let Some(wasm_name_suffix) = &self.wasm_name_suffix {
133            raw.push("--wasm-suffix".to_string());
134            raw.push(wasm_name_suffix.clone())
135        }
136        if !self.wasm_opt {
137            raw.push("--no-wasm-opt".to_string());
138        }
139        if self.wat {
140            raw.push("--wat".to_string());
141        }
142        if self.emit_mir {
143            raw.push("--mir".to_string());
144        }
145        if self.emit_llvm_ir {
146            raw.push("--llvm-ir".to_string());
147        }
148        if !self.extract_imports {
149            raw.push("--no-imports".to_string());
150        }
151        if let Some(target_dir_wasm) = &self.target_dir_wasm {
152            raw.push("--target-dir".to_string());
154            raw.push(target_dir_wasm.clone());
155        }
156        if self.twiggy_top {
157            raw.push("--twiggy-top".to_string());
158        }
159        if self.twiggy_paths {
160            raw.push("--twiggy-paths".to_string());
161        }
162        if self.twiggy_monos {
163            raw.push("--twiggy-monos".to_string());
164        }
165        if self.twiggy_dominators {
166            raw.push("--twiggy-dominators".to_string());
167        }
168        raw
169    }
170}
171
172#[derive(Clone, PartialEq, Eq, Debug, Args)]
173pub struct BuildDbgArgs {
174    #[arg(long = "target-dir-wasm", alias = "target-dir", verbatim_doc_comment)]
178    pub target_dir_wasm: Option<String>,
179
180    #[arg(long = "twiggy-top", verbatim_doc_comment)]
182    pub twiggy_top: bool,
183
184    #[arg(long = "twiggy-paths", verbatim_doc_comment)]
186    pub twiggy_paths: bool,
187
188    #[arg(long = "twiggy-monos", verbatim_doc_comment)]
190    pub twiggy_monos: bool,
191
192    #[arg(long = "twiggy-dominators", verbatim_doc_comment)]
194    pub twiggy_dominators: bool,
195}
196
197impl BuildDbgArgs {
198    pub fn into_build_args(self) -> BuildArgs {
200        BuildArgs {
201            wasm_symbols: true,
202            wasm_name_override: None,
203            wasm_name_suffix: Some("dbg".to_string()),
204            wasm_opt: false,
205            wat: true,
206            extract_imports: false,
207            target_dir_wasm: self.target_dir_wasm,
208            twiggy_top: self.twiggy_top,
209            twiggy_paths: self.twiggy_paths,
210            twiggy_monos: self.twiggy_monos,
211            twiggy_dominators: self.twiggy_dominators,
212            ..BuildArgs::default()
213        }
214    }
215}
216
217impl CliArgsToRaw for BuildDbgArgs {
218    fn to_raw(&self) -> Vec<String> {
219        let mut raw = Vec::new();
220        if let Some(target_dir_wasm) = &self.target_dir_wasm {
221            raw.push("--target-dir".to_string());
223            raw.push(target_dir_wasm.clone());
224        }
225        if self.twiggy_top {
226            raw.push("--twiggy-top".to_string());
227        }
228        if self.twiggy_paths {
229            raw.push("--twiggy-paths".to_string());
230        }
231        if self.twiggy_monos {
232            raw.push("--twiggy-monos".to_string());
233        }
234        if self.twiggy_dominators {
235            raw.push("--twiggy-dominators".to_string());
236        }
237        raw
238    }
239}
240
241#[derive(Clone, PartialEq, Eq, Debug, Args)]
242pub struct TwiggyArgs {
243    #[arg(long = "target-dir-wasm", alias = "target-dir", verbatim_doc_comment)]
247    pub target_dir_wasm: Option<String>,
248}
249
250impl TwiggyArgs {
251    pub fn into_build_args(self) -> BuildArgs {
252        BuildDbgArgs {
253            target_dir_wasm: self.target_dir_wasm,
254            twiggy_top: true,
255            twiggy_paths: true,
256            twiggy_monos: true,
257            twiggy_dominators: true,
258        }
259        .into_build_args()
260    }
261}
262
263impl CliArgsToRaw for TwiggyArgs {
264    fn to_raw(&self) -> Vec<String> {
265        let mut raw = Vec::new();
266        if let Some(target_dir) = &self.target_dir_wasm {
267            raw.push("--target-dir".to_string());
269            raw.push(target_dir.clone());
270        }
271        raw
272    }
273}