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
use crate::utils::UNIT_LETTER_MAP;

use super::utils::{string_to_letter_map, Letter, LetterMapping};

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
/// The Enigma Reflector or Umkehrwalze
pub struct Reflector {
    wiring: LetterMapping,
}

impl Default for Reflector {
    fn default() -> Self {
        Reflector {
            wiring: UNIT_LETTER_MAP,
        }
    }
}

impl Reflector {
    /// Create a `Reflector` from a `str`
    #[must_use]
    pub const fn from_static_str(string: &'static str) -> Reflector {
        Reflector {
            wiring: string_to_letter_map(string),
        }
    }
}

#[allow(missing_docs)]
pub const REFLECTOR_A: Reflector = Reflector::from_static_str("EJMZALYXVBWFCRQUONTSPIKHGD");

#[allow(missing_docs)]
pub const REFLECTOR_B: Reflector = Reflector::from_static_str("YRUHQSLDPXNGOKMIEBFZCWVJAT");

#[allow(missing_docs)]
pub const REFLECTOR_C: Reflector = Reflector::from_static_str("FVPJIAOYEDRZXWGCTKUQSBNMHL");

#[allow(missing_docs)]
pub const REFLECTOR_B_THIN: Reflector = Reflector::from_static_str("ENKQAUYWJICOPBLMDXZVFTHRGS");

#[allow(missing_docs)]
pub const REFLECTOR_C_THIN: Reflector = Reflector::from_static_str("RDOBJNTKVEHMLFCWZAXGYIPSUQ");

impl core::ops::Index<Letter> for Reflector {
    type Output = Letter;

    fn index(&self, index: Letter) -> &Self::Output {
        &self.wiring[index]
    }
}