cryptor/cryptor/algorithm/enigma/
reflector.rs

1
2// Copyright (c) 2017 Atsushi Miyake
3//
4// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or http://apache.org/licenses/LICENSE-2.0>
5// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to those terms.
7
8/// enigma module
9use super::{ SubstitutionTable, ALPHABETS };
10
11/// utility module
12use utility::{ GetIndex, IndexResult };
13
14pub struct Reflector {
15    substitution_table: SubstitutionTable
16}
17
18impl Reflector {
19
20    pub fn new(substitution_table: SubstitutionTable) -> Self {
21        Reflector {
22            substitution_table
23        }
24    }
25
26    pub fn reflect(&self, character: &char) -> char {
27        let characters = ALPHABETS.to_vec();
28        match characters.get_index(character) {
29            IndexResult::Exist(index) => self.substitution_table.characters[index],
30            IndexResult::None         => *character
31        }
32    }
33}