enigma/rotor/
from_args.rs1use crate::enigma_int::ToEnigmaInt;
2
3use super::Rotor;
4
5impl Rotor {
6 pub fn from_args(encoding: &String, turnover: &String, position: &String) -> Rotor {
7 if encoding.len() != 26 {
9 panic!("Rotor encoding must be 26 characters long.\n --rotor \"ENCODING\" TURNOVER POSITION");
10 }
11
12 if turnover.len() != 1 {
13 panic!("Rotor turnover must be 1 character.\n --rotor \"ENCODING\" TURNOVER POSITION");
14 }
15
16 if position.len() != 1 {
17 panic!("Rotor position must be 1 character.\n --rotor \"ENCODING\" TURNOVER POSITION");
18 }
19
20 let wiring = encoding
21 .chars()
22 .map(|c| c.to_internal_int())
23 .collect::<Vec<usize>>();
24
25 let turnover = turnover.chars().next().unwrap().to_internal_int();
27
28 let turnover_index = match wiring.iter().position(|&c| c == turnover) {
29 Some(i) => i,
30 None => panic!("Rotor turnover must be a character of the encoding."),
31 };
32
33 let position = position.chars().next().unwrap().to_internal_int();
35
36 let position_index = match wiring.iter().position(|&c| c == position) {
37 Some(i) => i,
38 None => panic!("Rotor position must be a character of the encoding"),
39 };
40
41 Rotor {
42 wiring,
43 turnover: turnover_index,
44 position: position_index,
45 }
46 }
47}