Debian Codegraph @

src/cli/types/special_names.rs

raw
1 use std::fmt;
2
3 /// Special names are used to identify special certificates.
4 ///
5 /// Special certificates are created by `sq`. Currently, they
6 /// correspond to shadow CAs. First, addressing them by fingerprint
7 /// is annoying. But, since they are created by `sq`, they have a
8 /// different fingerprint on each system. This makes it possible to
9 #[derive(Clone, Debug, PartialEq, Eq)]
10 pub enum SpecialName {
11 PublicDirectories,
12 KeysOpenpgpOrg,
13 KeysMailvelopeCom,
14 ProtonMe,
15 WKD,
16 DANE,
17 Autocrypt,
18 Web,
19 // NB: If you add a new variant, be sure to update
20 // SPECIAL_VARIANTS and SPECIAL_STRINGS!
21 }
22
23 // Ideally SPECIAL_VARIANTS and SPECIAL_VALUES would be a slice of
24 // tuples. But, because clap needs a slice of names, we split it up.
25 const SPECIAL_VARIANTS: &'static [SpecialName] = &[
26 SpecialName::PublicDirectories,
27 SpecialName::KeysOpenpgpOrg,
28 SpecialName::KeysMailvelopeCom,
29 SpecialName::ProtonMe,
30 SpecialName::WKD,
31 SpecialName::DANE,
32 SpecialName::Autocrypt,
33 SpecialName::Web,
34 ];
35
36 const SPECIAL_STRINGS: &'static [&'static str] = &[
37 "public-directories",
38 "keys.openpgp.org",
39 "keys.mailvelope.com",
40 "proton.me",
41 "wkd",
42 "dane",
43 "autocrypt",
44 "web",
45 ];
46
47 impl fmt::Display for SpecialName {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>)
49 -> fmt::Result
50 {
51 assert_eq!(SPECIAL_VARIANTS.len(), SPECIAL_STRINGS.len());
52
53 for (variant, string)
54 in SPECIAL_VARIANTS.iter().zip(SPECIAL_STRINGS.iter())
55 {
56 if variant == self {
57 return write!(f, "{}", string);
58 }
59 }
60 panic!("You didn't update SPECIAL_VARIANTS");
61 }
62 }
63
64 impl clap::ValueEnum for SpecialName {
65 fn value_variants<'a>() -> &'a [Self] {
66 SPECIAL_VARIANTS
67 }
68
69 fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
70 assert_eq!(SPECIAL_VARIANTS.len(), SPECIAL_STRINGS.len());
71
72 for (variant, string)
73 in SPECIAL_VARIANTS.iter().zip(SPECIAL_STRINGS.iter())
74 {
75 if variant == self {
76 return Some(string.into());
77 }
78 }
79 panic!("You didn't update SPECIAL_VARIANTS");
80 }
81 }