ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
use crate::utils::{BaseString, EncodedString};
use crate::Traits::{BruteForce, Decrypt, Encrypt};
#[cfg(feature = "python-integration")]
use pyo3::pyclass;

#[derive(Default, Clone)]
#[cfg_attr(feature = "python-integration", pyclass)]
pub struct Vigenere {
    pub(crate) key: EncodedString,
}

// common methods between both python and rust
impl Vigenere {
    pub fn new(key: String) -> Self {
        let key = BaseString::new(key).encode().unwrap();
        Self { key }
    }
}

#[cfg(feature = "python-integration")]
mod python_integration {
    use super::*;
    use crate::utils::python_integration::PyBaseString;
    use pyo3::prelude::*;
    use std::collections::HashMap;

    #[pymethods]
    impl Vigenere {
        #[new]
        fn __new__(key: String) -> Self {
            Self::new(key)
        }

        pub fn encrypt(&self, input: String) -> PyResult<PyBaseString> {
            match Encrypt::encrypt(self, input.into()) {
                Ok(s) => Ok(s.into()),
                Err(e) => Err(pyo3::exceptions::PyException::new_err(format!("{:?}", e))),
            }
        }

        pub fn decrypt(&self, input: String) -> PyResult<PyBaseString> {
            match Decrypt::decrypt(self, input.into()) {
                Ok(s) => Ok(s.into()),
                Err(e) => Err(pyo3::exceptions::PyException::new_err(format!("{:?}", e))),
            }
        }

        /*pub fn brute_force(
            &mut self,
            input: String,
            clear_text: Option<String>,
        ) -> PyResult<HashMap<usize, String>> {
            match BruteForce::brute_force(self, input, clear_text, None) {
                Ok(s) => Ok(s),
                Err(e) => Err(pyo3::exceptions::PyException::new_err(format!("{:?}", e))),
            }
        }*/

        pub fn __str__(&self) -> PyResult<String> {
            let mut s = String::new();
            s.push_str(&format!("Vigenére Cipher\n"));
            Ok(s)
        }
    }
}