fn encipher(&self, ptext: &str) -> CipherResult {
        input::in_alphabet(ptext, &self.key)?;

        let mut ptext: Vec<u8> = ptext.bytes().collect();
        if ptext.len() % 2 != 0 {
            ptext.push(self.pad);
        }

        let key = self.key.as_bytes();

        let mut ctext = Vec::with_capacity(ptext.len());
        for i in (0..ptext.len()).step_by(2) {
            if ptext[i] == ptext[i + 1] {
                ptext[i + 1] = self.pad;
            }

            let yx1 = match key.iter().position(|&c| c == ptext[i]) {
                Some(val) => val,
                None => return Err(CipherInputError::NotInAlphabet),
            };
            let yx2 = match key.iter().position(|&c| c == ptext[i + 1]) {
                Some(val) => val,
                None => return Err(CipherInputError::NotInAlphabet),
            };

            let (y1, x1) = (yx1 / 5, yx1 % 5);
            let (y2, x2) = (yx2 / 5, yx2 % 5);

            if y1 != y2 && x1 != x2 {
                // different rows and columns
                ctext.push(key[y1 * 5 + x2]);
                ctext.push(key[y2 * 5 + x1]);
            } else if y1 == y2 {
                // same row
                ctext.push(key[y1 * 5 + (x1 + 1) % 5]);
                ctext.push(key[y2 * 5 + (x2 + 1) % 5]);
            } else if x1 == x2 {
                // same column
                ctext.push(key[(y1 + 1) % 5 * 5 + x1]);
                ctext.push(key[(y2 + 1) % 5 * 5 + x2]);
            }
        }

        Ok(String::from_utf8(ctext).unwrap())
    }
/// Enciphers the given plaintext (a str reference) using the Playfair cipher
/// and returns the ciphertext as a `CipherResult`.
///
/// # Example
/// ```
/// use ciphers::{Cipher, Playfair};
///
/// let playfair = Playfair::new("ZGPTFOIHMUWDRCNYKEQAXVSBL", 'X');
///
/// let ctext = playfair.encipher("DEFENDTHEEASTWALLOFTHECASTLE");
/// assert_eq!(ctext.unwrap(), "RKPAWRPMYSELZCLFXUZFRSNQBPSA");
/// ```
Enciphers the given plaintext (a str reference) using the Playfair cipher
and returns the ciphertext as a `CipherResult`.
Enciphers the given plaintext ( a str reference ) using the Playfair cipher and returns the ciphertext as a ` CipherResult ` .
fn encipher(&self, ptext: &str) -> CipherResult {
        input::in_alphabet(ptext, &self.key)?;
        let mut ptext: Vec<u8> = ptext.bytes().collect();
        if ptext.len() % 2 != 0 {
            ptext.push(self.pad);
        }
        let key = self.key.as_bytes();
        let mut ctext = Vec::with_capacity(ptext.len());
        for i in (0..ptext.len()).step_by(2) {
            if ptext[i] == ptext[i + 1] {
                ptext[i + 1] = self.pad;
            }
            let yx1 = match key.iter().position(|&c| c == ptext[i]) {
                Some(val) => val,
                None => return Err(CipherInputError::NotInAlphabet),
            };
            let yx2 = match key.iter().position(|&c| c == ptext[i + 1]) {
                Some(val) => val,
                None => return Err(CipherInputError::NotInAlphabet),
            };
            let (y1, x1) = (yx1 / 5, yx1 % 5);
            let (y2, x2) = (yx2 / 5, yx2 % 5);
            if y1 != y2 && x1 != x2 {
                // different rows and columns
                ctext.push(key[y1 * 5 + x2]);
                ctext.push(key[y2 * 5 + x1]);
            } else if y1 == y2 {
                // same row
                ctext.push(key[y1 * 5 + (x1 + 1) % 5]);
                ctext.push(key[y2 * 5 + (x2 + 1) % 5]);
            } else if x1 == x2 {
                // same column
                ctext.push(key[(y1 + 1) % 5 * 5 + x1]);
                ctext.push(key[(y2 + 1) % 5 * 5 + x2]);
            }
        }
        Ok(String::from_utf8(ctext).unwrap())
    }
fn encipher ( & self , ptext : & str ) -> CipherResult { input :: in_alphabet ( ptext , & self . key ) ? ; let mut ptext : Vec < u8 > = ptext . bytes ( ) . collect ( ) ; if ptext . len ( ) % 2 != 0 { ptext . push ( self . pad ) ; } let key = self . key . as_bytes ( ) ; let mut ctext = Vec :: with_capacity ( ptext . len ( ) ) ; for i in ( 0 .. ptext . len ( ) ) . step_by ( 2 ) { if ptext [ i ] == ptext [ i + 1 ] { ptext [ i + 1 ] = self . pad ; } let yx1 = match key . iter ( ) . position ( | & c | c == ptext [ i ] ) { Some ( val ) => val , None => return Err ( CipherInputError :: NotInAlphabet ) , } ; let yx2 = match key . iter ( ) . position ( | & c | c == ptext [ i + 1 ] ) { Some ( val ) => val , None => return Err ( CipherInputError :: NotInAlphabet ) , } ; let ( y1 , x1 ) = ( yx1 / 5 , yx1 % 5 ) ; let ( y2 , x2 ) = ( yx2 / 5 , yx2 % 5 ) ; if y1 != y2 && x1 != x2 { // different rows and columns ctext . push ( key [ y1 * 5 + x2 ] ) ; ctext . push ( key [ y2 * 5 + x1 ] ) ; } else if y1 == y2 { // same row ctext . push ( key [ y1 * 5 + ( x1 + 1 ) % 5 ] ) ; ctext . push ( key [ y2 * 5 + ( x2 + 1 ) % 5 ] ) ; } else if x1 == x2 { // same column ctext . push ( key [ ( y1 + 1 ) % 5 * 5 + x1 ] ) ; ctext . push ( key [ ( y2 + 1 ) % 5 * 5 + x2 ] ) ; } } Ok ( String :: from_utf8 ( ctext ) . unwrap ( ) ) }
Enciphers the given plaintext (a str reference) using the Playfair cipher
and returns the ciphertext as a `CipherResult`.
Enciphers the given plaintext ( a str reference ) using the Playfair cipher and returns the ciphertext as a ` CipherResult ` .
&self
ptext
&str
&self
ptext
&str
example
```
use ciphers::{Cipher, Playfair}.

let playfair = Playfair::new("ZGPTFOIHMUWDRCNYKEQAXVSBL", 'X').

let ctext = playfair.encipher("DEFENDTHEEASTWALLOFTHECASTLE");
assert_eq!(ctext.unwrap(), "RKPAWRPMYSELZCLFXUZFRSNQBPSA");
```
` ` ` use ciphers :: { Cipher Playfair } . let playfair = Playfair :: new ( " ZGPTFOIHMUWDRCNYKEQAXVSBL " ' X ' ) . let ctext = playfair . encipher ( " DEFENDTHEEASTWALLOFTHECASTLE " ) ; assert_eq! ( ctext . unwrap () " RKPAWRPMYSELZCLFXUZFRSNQBPSA " ) ; ` ` `