gitignore_template_generator/
constant.rs

1//! Define globally-shared constants.
2
3pub mod error_messages {
4    //! Constants for error messages to be displayed.
5
6    /// An error occurred during [`std::process::Command::output`].
7    pub const CMD_EXECUTION_FAILURE: &str = "Failed to execute command";
8
9    /// An error occurred during HTTP body parsing.
10    pub const BODY_PARSING_ISSUE: &str = "An error occurred during body parsing";
11
12    /// An error occurred while reading a file and converting it to a String
13    /// instance.
14    pub const FILE_READ_TO_STRING_FAILURE: &str = "Failed to read expected output file";
15
16    /// Commas found in cli positional args.
17    pub const COMMAS_NOT_ALLOWED: &str = "Commas are not allowed in template names";
18
19    /// An error occurred during an api call.
20    pub const API_CALL_FAILURE: &str = "An error occurred during the API call: {error}";
21
22    /// A HTTP error 400 occurred during api call.
23    pub const HTTP_400: &str = "http status: 400";
24
25    /// A HTTP error 404 occurred during api call.
26    pub const HTTP_404: &str = "http status: 404";
27
28    /// User requested author infos but none is available.
29    pub const AUTHOR_INFOS_NOT_AVAILABLE: &str = "Author information not available.";
30
31    /// User requested version infos but none is available.
32    pub const VERSION_INFOS_NOT_AVAILABLE: &str = "Version information not available.";
33
34    /// `White_Space` found in cli positional args.
35    ///
36    /// `White_Space` is specified in the
37    /// [Unicode Character Database][ucd] [`PropList.txt`].
38    ///
39    /// [ucd]: https://www.unicode.org/reports/tr44/
40    /// [`PropList.txt`]: https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
41    pub const WHITESPACES_NOT_ALLOWED: &str =
42        "Whitespace characters are not allowed in template names";
43
44    // One or more template names are not supported.
45    pub const INEXISTENT_TEMPLATE_NAMES: &str = "Following template names are not supported: {templates}.\nFor the list of available template names, try '--list'.";
46
47    // Conversion of String to u64.
48    pub const FAILED_U64_CONVERSION: &str = "Failed to convert to u64";
49
50    /// Timeout for current HTTP call.
51    pub const TIMEOUT: &str = "timeout: global";
52
53    /// Invalid utf-8 encoding.
54    pub const INVALID_ENCODING: &str = "io: stream did not contain valid UTF-8";
55
56    /// No slash in front of URI.
57    pub const URI_WITHOUT_STARTING_SLASH: &str = "URIs must start a slash (/)";
58
59    /// Invalid URL.
60    pub const INVALID_URL: &str = "Value must be a valid URL";
61
62    pub const LOCAL_GENERATION: &str =
63        "An error occurred while generating template from local file system";
64
65    pub const LOCAL_LISTING: &str =
66        "An error occurred while listing templates from local file system";
67
68    pub const UNSUPPORTED_TEMPLATE: &str = "One or more provided template names are not supported\nTo enable robust template names check, retry with '--check'.\nFor the list of available template names, try '--list'.";
69
70    pub const READ_HOME_ENV_VAR: &str = "An error occurred when trying to read $HOME, which is required for local generation: {error}";
71}
72
73pub mod help_messages {
74    //! Constants for help messages to be displayed.
75
76    /// Help message bound to [`crate::parser::Args::template_names`]
77    /// field (i.e. positional args).
78    pub const TEMPLATE_NAMES: &str = "A non-empty list of gitignore template names";
79
80    /// Help message bound to [`crate::parser::Args::show_author`]
81    /// field (i.e. author option).
82    pub const AUTHOR: &str = "Print author";
83
84    /// Help message bound to [`crate::parser::Args::server_url`]
85    /// field (i.e. server url option).
86    pub const SERVER_URL: &str = "The template manager url";
87
88    /// Help message bound to [`crate::parser::Args::show_help`]
89    /// field (i.e. help option).
90    pub const HELP: &str = "Print help";
91
92    /// Help message bound to [`crate::parser::Args::show_version`]
93    /// field (i.e. version option).
94    pub const VERSION: &str = "Print version";
95
96    /// Help message bound to [`crate::parser::Args::generator_uri`]
97    /// field (i.e. generator uri option).
98    pub const GENERATOR_URI: &str = "The template generator uri";
99
100    /// Help message bound to [`crate::parser::Args::show_list`]
101    /// field (i.e. list option).
102    pub const LIST: &str = "List available templates";
103
104    /// Help message bound to [`crate::parser::Args::lister_uri`]
105    /// field (i.e. lister uri option).
106    pub const LISTER_URI: &str = "The template lister uri";
107
108    /// Help message bound to [`crate::parser::Args::check_template_names`]
109    /// field (i.e. check option).
110    pub const CHECK: &str = "Enable robust template names check";
111
112    /// Help message bound to [`crate::parser::Args::timeout`]
113    /// field (i.e. timeout option).
114    pub const TIMEOUT: &str = "The template generation and listing service calls timeout";
115
116    /// Help message bound to [`crate::parser::Args::timeout_unit`]
117    /// field (i.e. timeout unit option).
118    pub const TIMEOUT_UNIT: &str = "The timeout unit";
119}
120
121pub mod help_texts {
122    pub const NOTHING_TO_BE_PRINTED: &str = "Nothing to be printed";
123
124    pub const ENV_VAR_RESET: &str = "{name} env var was set to {value}. Resetting it...";
125    pub const ENV_VAR_REMOVAL_BEFORE: &str = "{name} is set. Removing it...";
126    pub const ENV_VAR_REMOVAL_AFTER: &str = "{name} env var wasn't set. Removing it...";
127
128    pub const RESET: &str = "Reset!";
129    pub const REMOVED: &str = "Removed!";
130
131    pub const TEST_CXT_DROPPED: &str = "Test context dropped!";
132    pub const TEST_CTX_CREATED: &str = "Test context created!";
133
134    pub const DEFAULT_TIMEOUT: &str = "{second}s/{millis}ms";
135}
136
137pub mod cli_options {
138    //! Constants for short and long cli options specifier.
139
140    use crate::parser::CliOptionName;
141
142    /// Short and long specifier for author option.
143    ///
144    /// **Value**: `-a --author`
145    pub const AUTHOR: CliOptionName = CliOptionName {
146        short: "a",
147        long: "author",
148    };
149
150    /// Short and long specifier for server url option.
151    ///
152    /// **Value**: `-s --server-url`
153    pub const SERVER_URL: CliOptionName = CliOptionName {
154        short: "s",
155        long: "server-url",
156    };
157
158    /// Short and long specifier for help option.
159    ///
160    /// **Value**: `-h --help`
161    pub const HELP: CliOptionName = CliOptionName {
162        short: "h",
163        long: "help",
164    };
165
166    /// Short and long specifier for version option.
167    ///
168    /// **Value**: `-V --version`
169    pub const VERSION: CliOptionName = CliOptionName {
170        short: "V",
171        long: "version",
172    };
173
174    /// Short and long specifier for generator uri option.
175    ///
176    /// **Value**: `-g --generator-uri`
177    pub const GENERATOR_URI: CliOptionName = CliOptionName {
178        short: "g",
179        long: "generator-uri",
180    };
181
182    /// Short and long specifier for template list option.
183    ///
184    /// **Value**: `-l --list`
185    pub const LIST: CliOptionName = CliOptionName {
186        short: "l",
187        long: "list",
188    };
189
190    /// Short and long specifier for lister uri option.
191    ///
192    /// **Value**: `-i --lister-uri`
193    pub const LISTER_URI: CliOptionName = CliOptionName {
194        short: "i",
195        long: "lister-uri",
196    };
197
198    /// Short and long specifier for check option.
199    ///
200    /// **Value**: `-c --check`
201    pub const CHECK: CliOptionName = CliOptionName {
202        short: "c",
203        long: "check",
204    };
205
206    /// Short and long specifier for timeout option.
207    ///
208    /// **Value**: `-t --timeout`
209    pub const TIMEOUT: CliOptionName = CliOptionName {
210        short: "t",
211        long: "timeout",
212    };
213
214    /// Short and long specifier for timeout option.
215    ///
216    /// **Value**: `-u --timeout-unit`
217    pub const TIMEOUT_UNIT: CliOptionName = CliOptionName {
218        short: "u",
219        long: "timeout-unit",
220    };
221}
222
223pub mod parser_infos {
224    //! Constants for general parser infos.
225
226    /// About text to be displayed when requesting help.
227    pub const ABOUT: &str = "Generate templates for .gitignore files";
228}
229
230pub mod exit_status {
231    //! Constants for script exit status code.
232
233    /// Exit status code for successful script execution.
234    pub const SUCCESS: i32 = 0;
235
236    /// Exit status code for generic script error.
237    pub const GENERIC: i32 = 2;
238
239    /// Exit status code for HTTP body parsing error.
240    pub const BODY_PARSING_ISSUE: i32 = 3;
241
242    /// Exit status code for any error from HTTP client itself.
243    pub const HTTP_CLIENT_ERROR: i32 = 4;
244}
245
246pub mod template_manager {
247    //! Constants for gitignore template manager service.
248
249    use crate::parser::TimeoutUnit;
250
251    /// Env variable name pointing to local template directory
252    pub const HOME_ENV_VAR: &str = "GITIGNORE_TEMPLATE_GENERATOR_HOME";
253
254    /// Default local template directory
255    pub const DEFAULT_HOME: &str = ".gitignore_template_generator";
256
257    pub const DEFAULT_TEMPLATE_DIR: &str = "{home_path}/.gitignore_template_generator/templates";
258
259    /// Template manager service base URL.
260    pub const BASE_URL: &str = "https://www.toptal.com";
261
262    /// Template generator service URI.
263    ///
264    /// Used in conjunction with [`BASE_URL`] to build full URL and make
265    /// API call.
266    pub const GENERATOR_URI: &str = "/developers/gitignore/api";
267
268    /// Template lister service URI.
269    ///
270    /// Used in conjunction with [`BASE_URL`] to build full URL and make
271    /// API call.
272    pub const LISTER_URI: &str = "/developers/gitignore/api/list";
273
274    /// Timeout in seconds for HTTP calls to generator/lister service (str version).
275    pub const TIMEOUT: &str = "5";
276
277    /// Timeout in milliseconds for HTTP calls to generator/lister service (str version).
278    pub const TIMEOUT_MILLISECOND: &str = "5000";
279
280    /// Timeout in seconds for HTTP calls to generator/lister service (integer version).
281    pub const TIMEOUT_INT: u64 = 5;
282
283    /// Timeout milliseconds for HTTP calls to generator/lister service (integer version).
284    pub const TIMEOUT_MILLISECOND_INT: u64 = 5000;
285
286    /// Second timeout unit (str version).
287    pub const TIMEOUT_UNIT: &str = "second";
288
289    /// Millisecond timeout unit (str version).
290    pub const TIMEOUT_MILLISECOND_UNIT: &str = "millisecond";
291
292    /// Second timeout unit (enum version).
293    ///
294    /// `value` - TimeoutUnit::SECOND
295    pub const TIMEOUT_UNIT_ENUM: TimeoutUnit = TimeoutUnit::SECOND;
296}
297
298pub mod path {
299    //! Constants for file or directory path.
300
301    /// Path to directory containing test output expectations.
302    pub const TEST_EXPECTATIONS: &str = "tests/expected";
303
304    /// Path to directory containing test resources.
305    pub const TEST_RESOURCES: &str = "tests/resources";
306}