procon/
args.rs

1use std::fmt::Display;
2use std::path::PathBuf;
3
4use clap::ArgGroup;
5use clap::{Parser, Subcommand};
6use clap_verbosity_flag::Verbosity;
7
8use crate::property_file_reader::Delimiter;
9
10#[derive(Parser, Debug)]
11#[command(
12    author,
13    version,
14    about,
15    long_about = "Procon (Pro)perty (Con)verter \
16    \nA program to convert between different property formats.
17    \nExamples:
18    \nProperty -> Json
19    \n\tprocon json example.properties
20    \nProperty -> Yaml
21    \n\tprocon yaml example.properties
22    \nJson -> Properties
23    \n\tprocon properties example.json
24    "
25)]
26#[command(propagate_version = true)]
27#[command(group(ArgGroup::new("from")
28.multiple(false)
29.args(["from_property_file", "from_yaml_file", "from_json_file"]),
30))]
31#[command(group(ArgGroup::new("dry-run")
32.multiple(false)
33.args(["dry_run", "output_filename"]),
34))]
35pub struct Args {
36    #[command(subcommand)]
37    pub target_format: TargetFormat,
38
39    /// Dry run
40    ///
41    /// Only prints the converted format to the console
42    ///
43    /// This option is mutual exclusive with the -o --output-filename option
44    #[arg(short, long, default_value_t = false)]
45    pub dry_run: bool,
46
47    /// Flag to specifying stdin bytes to be processed as properties
48    ///
49    /// Format of stdin bytes
50    #[arg(short = 'p', long)]
51    pub from_property_file: bool,
52
53    /// Flag to specifying stdin bytes to be processed as yaml
54    ///
55    /// Format of stdin bytes
56    #[arg(short = 'y', long)]
57    pub from_yaml_file: bool,
58
59    /// Flag to specifying stdin bytes to be processed as json
60    ///
61    /// Format of stdin bytes
62    #[arg(short = 'j', long)]
63    pub from_json_file: bool,
64
65    /// File to write the converted format to
66    ///
67    /// This option is mutual exclusive with the -d --dry-run option.
68    #[arg(short, long)]
69    pub output_filename: Option<String>,
70
71    #[clap(flatten)]
72    pub verbose: Verbosity,
73}
74
75#[derive(Subcommand, Debug)]
76pub enum TargetFormat {
77    /// Target format properties
78    Properties {
79        /// Property delimiter
80        ///
81        /// only used in combination with properties command
82        #[arg(short, long, default_value_t = Delimiter::Equals)]
83        property_delimiter: Delimiter,
84        /// Input file
85        file: PathBuf,
86    },
87
88    /// Target format yaml
89    Yaml {
90        /// Property delimiter
91        ///
92        /// only used in combination with properties command
93        #[arg(short, long, default_value_t = Delimiter::Equals)]
94        property_delimiter: Delimiter,
95
96        /// Input file
97        file: PathBuf,
98    },
99
100    /// Target format json
101    Json {
102        /// Property delimiter
103        ///
104        /// only used in combination with properties command
105        #[arg(short, long, default_value_t = Delimiter::Equals)]
106        property_delimiter: Delimiter,
107
108        /// Input file
109        file: PathBuf,
110    },
111}
112
113impl Display for TargetFormat {
114    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115        write!(f, "{:?}", self)
116    }
117}
118
119#[allow(dead_code)]
120impl TargetFormat {
121    pub fn path_buf(&self) -> &PathBuf {
122        match self {
123            TargetFormat::Properties { file, .. } => file,
124            TargetFormat::Json { file, .. } => file,
125            TargetFormat::Yaml { file, .. } => file,
126        }
127    }
128    pub fn delimiter(&self) -> Option<&Delimiter> {
129        match self {
130            TargetFormat::Properties {
131                property_delimiter, ..
132            } => Some(property_delimiter),
133            TargetFormat::Yaml {
134                property_delimiter, ..
135            } => Some(property_delimiter),
136            TargetFormat::Json {
137                property_delimiter, ..
138            } => Some(property_delimiter),
139        }
140    }
141}