|
| 1 | +use serde_json; |
1 | 2 | use std::{io::Write as _, path::PathBuf}; |
2 | 3 |
|
3 | 4 | use hemtt_config::rapify::Derapify; |
4 | 5 |
|
5 | 6 | use crate::Error; |
6 | 7 |
|
| 8 | +#[derive(Debug, Copy, Clone, clap::ValueEnum)] |
| 9 | +pub enum OutputFormat { |
| 10 | + Debin, |
| 11 | + Json, |
| 12 | + JsonPretty, |
| 13 | +} |
| 14 | + |
| 15 | +impl OutputFormat { |
| 16 | + fn default_extension(&self) -> &str { |
| 17 | + match self { |
| 18 | + Self::Debin => "cpp", |
| 19 | + Self::JsonPretty | Self::Json => "json", |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
7 | 24 | #[derive(clap::Args)] |
8 | 25 | #[allow(clippy::module_name_repetitions)] |
9 | 26 | pub struct DerapifyArgs { |
10 | 27 | /// file to derapify |
11 | 28 | pub(crate) file: String, |
| 29 | + /// output format |
| 30 | + #[arg(short = 'f', long = "format", default_value = "debin")] |
| 31 | + pub(crate) output_format: OutputFormat, |
12 | 32 | /// output file |
13 | 33 | pub(crate) output: Option<String>, |
14 | 34 | } |
15 | 35 |
|
16 | 36 | /// Derapify a config file |
17 | | -pub fn derapify(path: &PathBuf, output: Option<&str>) -> Result<(), Error> { |
| 37 | +pub fn derapify(path: &PathBuf, output: Option<&str>, format: OutputFormat) -> Result<(), Error> { |
18 | 38 | let mut file = std::fs::File::open(path)?; |
19 | 39 | let config = hemtt_config::Config::derapify(&mut file)?; |
20 | 40 | let output = output.map_or_else( |
21 | 41 | || { |
22 | 42 | let mut path = path.clone(); |
23 | | - path.set_extension("cpp"); |
| 43 | + path.set_extension(format.default_extension()); |
24 | 44 | path |
25 | 45 | }, |
26 | 46 | PathBuf::from, |
27 | 47 | ); |
28 | 48 | let mut output = std::fs::File::create(output)?; |
29 | | - output.write_all(config.to_string().as_bytes())?; |
| 49 | + match format { |
| 50 | + OutputFormat::Debin => output.write_all(config.to_string().as_bytes())?, |
| 51 | + OutputFormat::Json => { |
| 52 | + output.write_all(serde_json::to_string(&config)?.as_bytes())?; |
| 53 | + } |
| 54 | + OutputFormat::JsonPretty => { |
| 55 | + output.write_all(serde_json::to_string_pretty(&config)?.as_bytes())?; |
| 56 | + } |
| 57 | + } |
30 | 58 | output.flush()?; |
31 | 59 | Ok(()) |
32 | 60 | } |
0 commit comments