projvar 0.19.9

A tiny CLI tool that tries to gather project specific meta-data in different ways, to store them into key=value pairs in a file for later use by other tools. See --list for the keys set by this tool.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// SPDX-FileCopyrightText: 2021-2023 Robin Vobruba <hoijui.quaero@gmail.com>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use cli_utils::BoxResult;
use enum_map::Enum;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fmt::Write;
use std::{
    borrow::Cow,
    collections::{HashMap, HashSet},
    fmt::Display,
    io::BufRead,
    iter::Iterator,
    sync::LazyLock,
};
use strum::{EnumCount, IntoEnumIterator};
use strum_macros::{EnumCount, EnumIter, EnumString, IntoStaticStr};
use thiserror::Error;

use std::str::FromStr;

use crate::environment::Environment;

/// Confidence in a concrete value returned by a call to [`crate::sources::VarSource::retrieve`].
/// Higher is better.
///
/// This is eventually used as one criteria to decide which value to prefer,
/// if multiple ones are present for a single key.
pub type Confidence = u8;
pub const C_HIGH: Confidence = 75;
pub const C_MIDDLE: Confidence = 50;
pub const C_LOW: Confidence = 25;

// #[derive(Clone)]
// #[derive(Debug)]
#[derive(Default, Serialize, Deserialize)]
pub struct Variable {
    key: &'static str,
    pub description: &'static str,
    pub default_required: bool,
    // pub alt_keys: &'static [&'static str], // This data was once present for all variables; see the commit that commented out this line with `git blame`
}

impl Variable {
    #[must_use]
    pub fn key(&self, environment: &Environment) -> Cow<'_, str> {
        match &environment.settings.key_prefix {
            Some(prefix) => Cow::Owned(prefix.clone() + self.key),
            None => Cow::Borrowed(self.key),
        }
    }

    /// The raw key, without prefix.
    /// NOTE You should probably use [`Self::key`] instead.
    #[must_use]
    pub const fn key_raw(&self) -> &'static str {
        self.key
    }
}

impl Display for Variable {
    fn fmt(
        &self,
        formatter: &mut std::fmt::Formatter<'_>,
    ) -> std::result::Result<(), std::fmt::Error> {
        write!(formatter, "{}", self.key)?;
        Ok(())
    }
}

static D_VARIABLE: LazyLock<Variable> = LazyLock::new(Variable::default);

impl<'a> Default for &'a Variable {
    fn default() -> &'a Variable {
        &D_VARIABLE
    }
}

#[remain::sorted]
// #[derive(Debug, EnumString, EnumIter, IntoStaticStr, PartialEq, Eq, Hash, Copy, Clone, Enum)]
// #[derive(Debug, EnumString, EnumIter, IntoStaticStr, Hash, Enum, EnumSetType)]
#[derive(
    Debug,
    EnumCount,
    EnumString,
    EnumIter,
    IntoStaticStr,
    Hash,
    Enum,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Clone,
    Copy,
    Serialize,
    Deserialize,
)]
pub enum Key {
    BuildArch,
    BuildBranch,
    BuildDate,
    BuildHostingUrl,
    // BuildIdent, // TODO This name is very bad, as it makes one think of BUILD_NUMBER; choose a different one! Maybe refunction it as well(?) -> `HumanVersion` (vs a machine-readable one like from git describe, which goes to `Version`), for example "Ubuntu 10.04 - UbsiDubsi"
    BuildNumber,
    BuildOs,
    BuildOsFamily,
    BuildTag,
    Ci,
    License,
    Licenses,
    Name,
    NameMachineReadable,
    RepoCloneUrl,
    RepoCloneUrlGit,
    RepoCloneUrlHttp,
    RepoCloneUrlSsh,
    RepoCommitPrefixUrl,
    RepoIssuesUrl,
    RepoRawVersionedPrefixUrl,
    RepoVersionedDirPrefixUrl,
    RepoVersionedFilePrefixUrl,
    RepoWebUrl,
    Version,
    VersionDate,
}

/// Converts a `"CamelCase"` string into an `"UPPER_SNAKE_CASE"` one.
///
/// for example:
///
/// ```
/// //# fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use projvar::var::camel_to_upper_snake_case;
/// assert_eq!(
///     camel_to_upper_snake_case("someLowerCaseStartingTest"),
///     "SOME_LOWER_CASE_STARTING_TEST"
/// );
/// assert_eq!(
///     camel_to_upper_snake_case("SomeUpperCaseStartingTest"),
///     "SOME_UPPER_CASE_STARTING_TEST"
/// );
/// // NOTE From here on, we start seeing the limitation of this simple algorithm
/// assert_eq!(
///     camel_to_upper_snake_case("somethingWith123ANumber"),
///     "SOMETHING_WITH123_A_NUMBER"
/// );
/// assert_eq!(
///     camel_to_upper_snake_case("somethingWith123aNumber"),
///     "SOMETHING_WITH123A_NUMBER"
/// );
/// //# Ok(())
/// //# }
/// ```
#[must_use]
pub fn camel_to_upper_snake_case(id: &str) -> String {
    static R_UPPER_SEL: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"(?P<after>[A-Z])").unwrap());
    let res = R_UPPER_SEL.replace_all(id, "_$after").to_uppercase();
    res.strip_prefix('_').unwrap_or(&res).to_string()
}

#[derive(Error, Debug)]
#[error("Not a valid key name: '{name}'")]
pub struct InvalidKey {
    name: String,
}

/// Converts an `"UPPER_SNAKE_CASE"` string into an `"CamelCase"` one.
///
/// for example:
///
/// ```
/// //# fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use projvar::var::upper_snake_to_camel_case;
/// assert_eq!(
///     upper_snake_to_camel_case("SOME_UPPER_CASE_STARTING_TEST"),
///     "SomeUpperCaseStartingTest"
/// );
/// // NOTE From here on, we start seeing the limitation of this simple algorithm
/// assert_eq!(
///     upper_snake_to_camel_case("SOMETHING_WITH123A_NUMBER"),
///     "SomethingWith123aNumber"
/// );
/// //# Ok(())
/// //# }
/// ```
#[must_use]
pub fn upper_snake_to_camel_case(id: &str) -> String {
    // static ref R_PREF: Regex = Regex::new(r"^_").unwrap();
    // static ref R_SUFF: Regex = Regex::new(r"_$").unwrap();
    static R_FIRST: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(.)").unwrap());
    static R_UPPER_SEL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(.)_(.)").unwrap());
    let id = id.to_lowercase();
    // let id= R_PREF.replace(&id, "");
    // let id= R_SUFF.replace(&id, "");
    let id = R_FIRST.replace_all(&id, |captures: &regex::Captures| captures[1].to_uppercase());
    let id = R_UPPER_SEL.replace_all(&id, |captures: &regex::Captures| {
        captures[1].to_owned() + &captures[2].to_uppercase()
    });
    id.strip_prefix('_')
        .unwrap_or(&id)
        .strip_suffix('_')
        .unwrap_or(&id)
        .to_string()
}

impl Key {
    /// Tries to create a `Key` from a string identifier.
    /// This might be the exact name of the `Key` (like "Name"),
    /// or the associated variable key (like `"PROJECT_NAME"`).
    ///
    /// # Errors
    ///
    /// If the given identifier could not be mapped to any `Key` variant.
    pub fn from_name_or_var_key(key_prefix: &Regex, id: &str) -> Result<Self, InvalidKey> {
        Self::from_str(id)
            .or_else(|_| {
                Self::from_str(&upper_snake_to_camel_case(
                    key_prefix.replace(id, "").as_ref(),
                ))
            })
            .map_err(|_err| InvalidKey {
                name: id.to_owned(),
            })
    }
}

// pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
// where
//     P: AsRef<Path>,
// {
//     let file = File::open(filename)?;
//     Ok(io::BufReader::new(file).lines())
// }

fn unquote(pot_quoted: &str) -> &str {
    let len = pot_quoted.len();
    if len > 1 {
        let mut chars = pot_quoted.chars();
        let first_char = chars.next().unwrap();
        let last_char = chars.last().unwrap();
        if (first_char == '"' && last_char == '"') || (first_char == '\'' && last_char == '\'') {
            return &pot_quoted[1..len - 1];
        }
    }
    pot_quoted
}

/// Parses a file containing lines with strings of the from "KEY=VALUE".
///
/// Empty lines and those starting with either "#" or "//" are ignored.
///
/// # Errors
///
/// If there is a problem with reading the file.
///
/// If any line has a bad form, missing key and/or value.
pub fn parse_vars_file_reader(mut reader: impl BufRead) -> BoxResult<HashMap<String, String>> {
    // Ignore empty lines and those starting with '#' or "//"
    static R_IGNORE_LINE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^($|#|//)").unwrap());
    let mut vars = HashMap::<String, String>::new();

    for line in cli_utils::lines_iterator(&mut reader, true) {
        let line = line?;
        let line = line.trim();
        if !R_IGNORE_LINE.is_match(line) {
            let (key, value) = parse_key_value_str(line)?;
            let value = unquote(&value);
            vars.insert(key.clone(), value.to_owned());
        }
    }
    Ok(vars)
}

// pub fn parse_vars_file(var_file: &str) -> BoxResult<HashMap<String, String>> {
//     lazy_static! {
//         // Ignore empty lines or
//         static ref R_IGNORE_LINE: Regex = Regex::new(r"^($|#|//)").unwrap();
//     }
//     let mut vars = HashMap::<String, String>::new();
//     for line in read_lines(var_file)? {
//         let line = line?;
//         let line = line.trim();
//         if !R_IGNORE_LINE.is_match(line) {
//             let (key, value) = parse_key_value_str(line)?;
//             vars.insert(key.to_owned(), value.to_owned());
//         }
//     }
//     Ok(vars)
// }

/// Parses a string with the pattern "KEY=VALUE" into a (key, value) tuple.
///
/// # Errors
///
/// If the string has a bad form, missing key and/or value.
pub fn parse_key_value_str(key_value: &str) -> BoxResult<(String, String)> {
    let mut splitter = key_value.splitn(2, '=');
    let key = splitter
        .next()
        .ok_or("Failed to parse key; key-value pairs have to be of the form \"key=value\"")?;
    let value = splitter
        .next()
        .ok_or("Failed to parse value; key-value pairs have to be of the form \"key=value\"")?;
    Ok((key.to_owned(), value.to_owned()))
}

/// Checks if a string represents a valid key-value pair,
/// conforming to the pattern "KEY=VALUE".
///
/// # Errors
///
/// If the string does not represent a valid key-value pair.
pub fn is_key_value_str_valid(key_value: &str) -> Result<(), String> {
    parse_key_value_str(key_value)
        .map(|_kv| ())
        .map_err(|_err| String::from("Not a valid key=value pair"))
}

#[must_use]
pub fn list_keys(environment: &Environment) -> String {
    static HEADER: &str = "| Default Required | Key | Description |\n";
    static HEADER_SEP: &str = "| - | --- | ------------ |\n";
    static ROW_LEN_ESTIMATE: usize = 140;

    // the estimated size of the table in chars
    let table_chars_estimate = HEADER.len() + HEADER_SEP.len() + (Key::COUNT * ROW_LEN_ESTIMATE);
    let mut table = String::with_capacity(table_chars_estimate);

    table.push_str("\n\n");
    table.push_str(HEADER);
    table.push_str(HEADER_SEP);
    for key in Key::iter() {
        let var = get(key);
        let def = if var.default_required { "[x]" } else { "[ ]" };
        writeln!(
            table,
            "| {} | `{}` | {} |",
            def,
            var.key(environment),
            var.description
        )
        .expect("Out of memory(?)");
    }
    table.push('\n');

    log::trace!("Table size (in chars), estimated: {table_chars_estimate}");
    log::trace!("Table size (in chars), actual:    {}", table.len());
    //assert!(table_chars_estimate >= table.len());
    table
}

pub const KEY_BUILD_ARCH: &str = "BUILD_ARCH";
pub const KEY_BUILD_BRANCH: &str = "BUILD_BRANCH";
pub const KEY_BUILD_DATE: &str = "BUILD_DATE";
pub const KEY_BUILD_HOSTING_URL: &str = "BUILD_HOSTING_URL";
pub const KEY_BUILD_NUMBER: &str = "BUILD_NUMBER";
pub const KEY_BUILD_OS: &str = "BUILD_OS";
pub const KEY_BUILD_OS_FAMILY: &str = "BUILD_OS_FAMILY";
pub const KEY_BUILD_TAG: &str = "BUILD_TAG";
pub const KEY_CI: &str = "CI";
pub const KEY_LICENSE: &str = "LICENSE";
pub const KEY_LICENSES: &str = "LICENSES";
pub const KEY_NAME: &str = "NAME";
pub const KEY_NAME_MACHINE_READABLE: &str = "NAME_MACHINE_READABLE";
pub const KEY_REPO_CLONE_URL: &str = "REPO_CLONE_URL";
pub const KEY_REPO_CLONE_URL_HTTP: &str = "REPO_CLONE_URL_HTTP";
pub const KEY_REPO_CLONE_URL_SSH: &str = "REPO_CLONE_URL_SSH";
pub const KEY_REPO_CLONE_URL_GIT: &str = "REPO_CLONE_URL_GIT";
pub const KEY_REPO_COMMIT_PREFIX_URL: &str = "REPO_COMMIT_PREFIX_URL";
pub const KEY_REPO_ISSUES_URL: &str = "REPO_ISSUES_URL";
pub const KEY_REPO_RAW_VERSIONED_PREFIX_URL: &str = "REPO_RAW_VERSIONED_PREFIX_URL";
pub const KEY_REPO_VERSIONED_DIR_PREFIX_URL: &str = "REPO_VERSIONED_DIR_PREFIX_URL";
pub const KEY_REPO_VERSIONED_FILE_PREFIX_URL: &str = "REPO_VERSIONED_FILE_PREFIX_URL";
pub const KEY_REPO_WEB_URL: &str = "REPO_WEB_URL";
pub const KEY_VERSION: &str = "VERSION";
pub const KEY_VERSION_DATE: &str = "VERSION_DATE";

const VAR_BUILD_ARCH: Variable = Variable {
    key: KEY_BUILD_ARCH,
    description: "The computer hardware architecture we are building on. \
        (common values: 'x86', 'x86_64')",
    default_required: false,
};
const VAR_BUILD_BRANCH: Variable = Variable {
    key: KEY_BUILD_BRANCH,
    description: "The development branch name, for example: \
        \"master\", \
        \"develop\"",
    default_required: false,
};
const VAR_BUILD_DATE: Variable = Variable {
    key: KEY_BUILD_DATE,
    description: "Date of this build, for example: \
        \"2021-12-31 23:59:59\" (see --date-format)",
    default_required: false,
};
const VAR_BUILD_HOSTING_URL: Variable = Variable {
    key: KEY_BUILD_HOSTING_URL,
    description: "Web URL under which the generated output will be available, \
        for example: \
        https://osegermany.gitlab.io/OHS-3105",
    default_required: false,
};
const VAR_BUILD_NUMBER: Variable = Variable {
    key: KEY_BUILD_NUMBER,
    description: "The build number (1, 2, 3) starts at 1 for each repo and branch.",
    default_required: false,
};
const VAR_BUILD_OS: Variable = Variable {
    key: KEY_BUILD_OS,
    description: "The operating system we are building on. \
        (common values: 'linux', 'macos', 'windows')",
    default_required: false,
};
const VAR_BUILD_OS_FAMILY: Variable = Variable {
    key: KEY_BUILD_OS_FAMILY,
    description: "The operating system family we are building on. \
        (should be either 'unix' or 'windows')",
    default_required: false,
};
const VAR_BUILD_TAG: Variable = Variable {
    key: KEY_BUILD_TAG,
    description: "The tag of a commit that kicked off the build. \
        This value is only available on tags. \
        Not available for builds against branches.",
    default_required: false,
};
const VAR_CI: Variable = Variable {
    key: KEY_CI,
    description: "'true' if running on a CI/build-bot; unset otherwise.",
    default_required: false,
};
const VAR_LICENSE: Variable = Variable {
    key: KEY_LICENSE,
    description: "The main License identifier of the sources, \
        preferably from the SPDX specs, for example: \
        \"AGPL-3.0-or-later\", \
        \"CC-BY-SA-4.0\"",
    default_required: true,
};
const VAR_LICENSES: Variable = Variable {
    key: KEY_LICENSES,
    description: "The identifiers of all the licenses of this project, \
        preferably from the SPDX specs, comma separated, for example: \
        \"AGPL-3.0-or-later, \
        CC0-1.0, \
        Unlicense\"",
    default_required: true,
};
const VAR_NAME: Variable = Variable {
    key: KEY_NAME,
    description: "The human focused name of the project.",
    default_required: true,
};
const VAR_NAME_MACHINE_READABLE: Variable = Variable {
    key: KEY_NAME_MACHINE_READABLE,
    description: "The machine readable name of the project.",
    default_required: true,
};
const VAR_REPO_CLONE_URL: Variable = Variable {
    key: KEY_REPO_CLONE_URL,
    description: "The original repo clone URL; \
        may use any valid git URL scheme. \
        May not conform to the URL specification. \
        It is commonly used for anonymous fetch-only access.",
    default_required: true,
};
const VAR_REPO_CLONE_URL_HTTP: Variable = Variable {
    key: KEY_REPO_CLONE_URL_HTTP,
    description: "The repo clone URL, HTTP(S) version. \
        It always conforms to the URL specification. \
        It is commonly used for anonymous fetch-only access.",
    default_required: false,
};
const VAR_REPO_CLONE_URL_SSH: Variable = Variable {
    key: KEY_REPO_CLONE_URL_SSH,
    description: "The repo clone URL, SSH version. \
        It always conforms to the URL specification. \
        It is commonly used for authenticated, fetch and push access.",
    default_required: false,
};
const VAR_REPO_CLONE_URL_GIT: Variable = Variable {
    key: KEY_REPO_CLONE_URL_GIT,
    description: "The repo clone URL, Git protocol version. \
        It always conforms to the URL specification. \
        It is used for non-authenticated fetch access. \
        Most repo hosters do not support it.",
    default_required: false,
};
const VAR_REPO_COMMIT_PREFIX_URL: Variable = Variable {
    key: KEY_REPO_COMMIT_PREFIX_URL,
    description: // TODO Elaborate the "Add commit SHA" part
        "The repo commit prefix URL. \
        Add commit SHA. \
        The part in []: \
        [https://github.com/hoijui/nim-ci/commit]/23f84b91]",
    default_required: true,
};
const VAR_REPO_ISSUES_URL: Variable = Variable {
    key: KEY_REPO_ISSUES_URL,
    description: "The repo issues URL, for example: \
        https://gitlab.com/openflexure/openflexure-microscope/issues",
    default_required: true,
};
const VAR_REPO_RAW_VERSIONED_PREFIX_URL: Variable = Variable {
    key: KEY_REPO_RAW_VERSIONED_PREFIX_URL,
    description: "The repo raw prefix URL. \
        Add version (tag, branch, SHA) and file path. \
        The part in []: \
        [https://raw.githubusercontent.com/hoijui/nim-ci]/master/.github/workflows/docker.yml]",
    default_required: true,
};
const VAR_REPO_VERSIONED_DIR_PREFIX_URL: Variable = Variable {
    key: KEY_REPO_VERSIONED_DIR_PREFIX_URL,
    description: // TODO Elaborate the "Add ..." part
        "The repo directory prefix URL. \
        Add version (tag, branch, SHA) and directory path. \
        The part in []: \
        [https://github.com/hoijui/nim-ci]/master/.github/workflows/docker.yml]",
    default_required: true,
};
const VAR_REPO_VERSIONED_FILE_PREFIX_URL: Variable = Variable {
    key: KEY_REPO_VERSIONED_FILE_PREFIX_URL,
    description: // TODO Elaborate the "Add ..." part
        "The repo file prefix URL. \
        Add version (tag, branch, SHA) and file path. \
        The part in []: \
        [https://github.com/hoijui/nim-ci]/master/.github/workflows/docker.yml]",
    default_required: true,
};
const VAR_REPO_WEB_URL: Variable = Variable {
    key: KEY_REPO_WEB_URL,
    description: "The repo web UI URL, for example: \
        https://gitlab.com/OSEGermany/OHS-3105",
    default_required: true,
};
const VAR_VERSION: Variable = Variable {
    key: KEY_VERSION,
    description: "The project version, for example: \
        \"1.10.3\", \
        \"0.2.0-1-ga5387ac-dirty\"",
    default_required: true,
};
const VAR_VERSION_DATE: Variable = Variable {
    key: KEY_VERSION_DATE,
    description: "Date this version was committed to source control, for example: \
        \"2021-12-31 23:59:59\" \
        (see --date-format)",
    default_required: true,
};

/// Returns a reference to the variable settings associated with the given key.
#[must_use]
#[remain::check]
pub const fn get(key: Key) -> &'static Variable {
    #[remain::sorted]
    match key {
        Key::BuildArch => &VAR_BUILD_ARCH,
        Key::BuildBranch => &VAR_BUILD_BRANCH,
        Key::BuildDate => &VAR_BUILD_DATE,
        Key::BuildHostingUrl => &VAR_BUILD_HOSTING_URL,
        Key::BuildNumber => &VAR_BUILD_NUMBER,
        Key::BuildOs => &VAR_BUILD_OS,
        Key::BuildOsFamily => &VAR_BUILD_OS_FAMILY,
        Key::BuildTag => &VAR_BUILD_TAG,
        Key::Ci => &VAR_CI,
        Key::License => &VAR_LICENSE,
        Key::Licenses => &VAR_LICENSES,
        Key::Name => &VAR_NAME,
        Key::NameMachineReadable => &VAR_NAME_MACHINE_READABLE,
        Key::RepoCloneUrl => &VAR_REPO_CLONE_URL,
        Key::RepoCloneUrlGit => &VAR_REPO_CLONE_URL_GIT,
        Key::RepoCloneUrlHttp => &VAR_REPO_CLONE_URL_HTTP,
        Key::RepoCloneUrlSsh => &VAR_REPO_CLONE_URL_SSH,
        Key::RepoCommitPrefixUrl => &VAR_REPO_COMMIT_PREFIX_URL,
        Key::RepoIssuesUrl => &VAR_REPO_ISSUES_URL,
        Key::RepoRawVersionedPrefixUrl => &VAR_REPO_RAW_VERSIONED_PREFIX_URL,
        Key::RepoVersionedDirPrefixUrl => &VAR_REPO_VERSIONED_DIR_PREFIX_URL,
        Key::RepoVersionedFilePrefixUrl => &VAR_REPO_VERSIONED_FILE_PREFIX_URL,
        Key::RepoWebUrl => &VAR_REPO_WEB_URL,
        Key::Version => &VAR_VERSION,
        Key::VersionDate => &VAR_VERSION_DATE,
    }
}

fn create_default_keys() -> HashSet<Key> {
    let mut def_keys = HashSet::<Key>::new();
    for key in Key::iter() {
        let variable = get(key);
        if variable.default_required {
            def_keys.insert(key);
        }
    }
    def_keys
}

static DEFAULT_KEYS: LazyLock<HashSet<Key>> = LazyLock::new(create_default_keys);

#[must_use]
// pub fn default_keys() -> EnumSet<Key> {
pub fn default_keys() -> &'static HashSet<Key> {
    &DEFAULT_KEYS
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_camel_to_upper_snake_case() -> BoxResult<()> {
        assert_eq!(camel_to_upper_snake_case("Version"), "VERSION");
        assert_eq!(camel_to_upper_snake_case("version"), "VERSION");
        assert_eq!(
            camel_to_upper_snake_case("ProjectVersion"),
            "PROJECT_VERSION"
        );
        assert_eq!(
            camel_to_upper_snake_case("projectVersion"),
            "PROJECT_VERSION"
        );
        assert_eq!(camel_to_upper_snake_case("RepoWebUrl"), "REPO_WEB_URL");
        assert_eq!(camel_to_upper_snake_case("repoWebUrl"), "REPO_WEB_URL");

        Ok(())
    }

    #[test]
    fn test_from_name_or_var_key() -> BoxResult<()> {
        let r_prefix_none = Regex::new("^").unwrap();
        let r_prefix_project = Regex::new("^PROJECT_").unwrap();

        assert_eq!(
            Key::from_name_or_var_key(&r_prefix_none, "VERSION")?,
            Key::Version
        );
        assert_eq!(
            Key::from_name_or_var_key(&r_prefix_project, "VERSION")?,
            Key::Version
        );
        assert_eq!(
            Key::from_name_or_var_key(&r_prefix_project, "PROJECT_VERSION")?,
            Key::Version
        );
        assert_eq!(
            Key::from_name_or_var_key(&r_prefix_none, "REPO_WEB_URL")?,
            Key::RepoWebUrl
        );
        assert_eq!(
            Key::from_name_or_var_key(&r_prefix_project, "REPO_WEB_URL")?,
            Key::RepoWebUrl
        );
        assert_eq!(
            Key::from_name_or_var_key(&r_prefix_project, "PROJECT_REPO_WEB_URL")?,
            Key::RepoWebUrl
        );

        assert!(Key::from_name_or_var_key(&r_prefix_none, "PROJECT_VERSION").is_err());
        assert!(Key::from_name_or_var_key(&r_prefix_none, "PROJECT_REPO_WEB_URL").is_err());

        Ok(())
    }
}