use crate::util; pub struct AtabashChipher { alphabet: Vec, } impl AtabashChipher { pub fn new(alphabet: impl Into>) -> anyhow::Result { let alphabet = alphabet.into(); util::verify_alphabet(&alphabet)?; Ok(Self { alphabet }) } pub fn encode(&self, input: &str) -> anyhow::Result { let mut output = String::with_capacity(input.len()); for c in input.chars() { let index = self .alphabet .iter() .position(|&x| x == c) .ok_or(anyhow::anyhow!("cannot encode character {:?}", c))?; output.push(self.alphabet[self.alphabet.len() - index - 1]); } Ok(output) } pub fn decode(&self, input: &str) -> anyhow::Result { self.encode(input) } }