use crate::{
component::EnigmaComponent,
error::{EnigmaError, EnigmaResult},
state::EnigmaState,
};
#[derive(Debug, Clone)]
pub struct Reflector {
mapping: [u8; 256],
}
impl Reflector {
pub fn new(mapping: [u8; 256]) -> EnigmaResult<Self> {
for i in 0..256 {
let j = mapping[i] as usize;
if mapping[j] != i as u8 {
return Err(EnigmaError::InvalidConfiguration(
"reflector mapping must be symmetric (involution)".into(),
));
}
}
Ok(Self { mapping })
}
pub fn identity() -> Self {
let mut mapping = [0u8; 256];
for (i, v) in mapping.iter_mut().enumerate() {
*v = i as u8;
}
Self { mapping }
}
pub fn paired() -> Self {
let mut mapping = [0u8; 256];
let mut i = 0;
while i < 256 {
mapping[i] = (i + 1) as u8;
mapping[i + 1] = i as u8;
i += 2;
}
Self { mapping }
}
}
impl EnigmaComponent for Reflector {
fn forward(&self, input: u8, _state: &EnigmaState) -> u8 {
self.mapping[input as usize]
}
fn backward(&self, input: u8, _state: &EnigmaState) -> u8 {
self.mapping[input as usize]
}
}