ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
#[cfg(feature = "python-integration")]
use pyo3::PyErr;
use std::fmt::Display;
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum CharacterParseError {
    #[error("Invalid character: {0}")]
    InvalidCharacter(char),
    #[error("Invalid index: {0}")]
    InvalidIndex(usize),
    #[cfg(feature = "python-integration")]
    #[error("Error: {0}")]
    Error(String),
    #[error("Unknown string type, cannot decode")]
    UnknownStringType,
}

#[cfg(feature = "python-integration")]
impl From<PyErr> for CharacterParseError {
    fn from(err: PyErr) -> CharacterParseError {
        CharacterParseError::Error(err.to_string())
    }
}

#[cfg(feature = "python-integration")]
impl From<CharacterParseError> for PyErr {
    fn from(err: CharacterParseError) -> PyErr {
        pyo3::exceptions::PyException::new_err(format!("{:?}", err))
    }
}

/// Enum representing possible errors in the Caesar cipher.
///
/// # Variants
///
/// * `InvalidShift` - Represents an error that occurs when the shift value is invalid.
///   This could be due to the shift value being outside the range `1..26`.
/// * `CharacterParseError` - Represents an error that occurs when a character cannot be parsed.
///   This is a wrapper around the `CharacterParseError` from the `error` module.
#[derive(Error, Debug, PartialEq)]
pub enum CeasarError {
    #[error("Invalid shift value")]
    InvalidShift,
    #[error("Character parse error: {0}")]
    CharacterParseError(String),
    #[error("Invalid clear text: {0}")]
    InvalidClearText(String),
    #[error("Invalid cipher text: {0}")]
    InvalidCipherText(String),
    #[cfg(feature = "python-integration")]
    #[error("Error: {0}")]
    Error(String),
}

#[cfg(feature = "python-integration")]
impl From<PyErr> for CeasarError {
    fn from(err: PyErr) -> CeasarError {
        CeasarError::Error(err.to_string())
    }
}

#[cfg(feature = "python-integration")]
impl From<CeasarError> for PyErr {
    fn from(err: CeasarError) -> PyErr {
        pyo3::exceptions::PyException::new_err(format!("{:?}", err))
    }
}

impl From<CharacterParseError> for CeasarError {
    fn from(err: CharacterParseError) -> CeasarError {
        CeasarError::CharacterParseError(err.to_string())
    }
}

impl From<CeasarError> for () {
    fn from(err: CeasarError) {}
}

/// `MixError` is an enumeration of all possible errors that can occur in the `Mix` struct.
///
/// # Variants
///
/// * `InvalidInput` - This variant is used when the input string is not valid. For example, if the length of the input string is not a multiple of the length of the key.
/// * `InvalidKey` - This variant is used when the key is not valid. For example, if the elements of the key are not less than `ALPHABET_LEN`, or if all keys do not have the same length.
/// * `CharacterParseError(CharacterParseError)` - This variant is used when there is an error in parsing the characters of the input string. The `CharacterParseError` struct contains more information about the error.
#[derive(Error, Debug, PartialEq)]
pub enum MixError {
    #[error("Invalid input")]
    InvalidInput,
    #[error("Invalid key")]
    InvalidKey,
    #[error("Character parse error: {0}")]
    CharacterParseError(CharacterParseError),
}

#[cfg(feature = "python-integration")]
impl From<MixError> for PyErr {
    fn from(err: MixError) -> PyErr {
        pyo3::exceptions::PyException::new_err(format!("{:?}", err))
    }
}

/// Enum representing possible errors in the RSA cipher.
#[derive(Error, Debug, PartialEq)]
pub enum RSAError {
    #[error("Invalid character: {0}")]
    CharacterParseError(CharacterParseError),
    #[error("Cannot validate the key, key is a public key")]
    CannotValidate,
    #[error("Error occurd: {0}")]
    Error(String),
    // Define error variants here
    #[error("The private key was not set, please factorize and calculate 'd' first")]
    PrivateKeyNotSet,
}

impl From<CharacterParseError> for RSAError {
    fn from(err: CharacterParseError) -> RSAError {
        RSAError::CharacterParseError(err)
    }
}

#[cfg(feature = "python-integration")]
impl From<PyErr> for RSAError {
    fn from(err: PyErr) -> RSAError {
        RSAError::Error(err.to_string())
    }
}

#[cfg(feature = "python-integration")]
impl From<RSAError> for PyErr {
    fn from(err: RSAError) -> PyErr {
        pyo3::exceptions::PyException::new_err(format!("{:?}", err))
    }
}
/// Enum representing possible errors in the des cipher.
#[derive(Debug, PartialEq, Error)]
pub enum DESError {
    #[error("Character parse error: {0}")]
    CharacterParseError(CharacterParseError),
    #[cfg(feature = "python-integration")]
    #[error("Error: {0}")]
    Error(String),
    // Define error variants here
    #[error("Invalid key length: {0} (in bytes)")]
    InvalidKeyLength(usize),
    #[error("Invalid key: {0}")]
    InvalidKey(String),
}

#[cfg(feature = "python-integration")]
impl From<PyErr> for DESError {
    fn from(err: PyErr) -> DESError {
        DESError::Error(err.to_string())
    }
}
#[cfg(feature = "python-integration")]
impl From<DESError> for PyErr {
    fn from(err: DESError) -> PyErr {
        pyo3::exceptions::PyException::new_err(format!("{:?}", err))
    }
}

impl From<CharacterParseError> for DESError {
    fn from(err: CharacterParseError) -> DESError {
        DESError::CharacterParseError(err)
    }
}

#[derive(Debug, PartialEq, Error)]
pub enum AffineError {
    #[error("Character parse error: {0}")]
    CharacterParseError(CharacterParseError),
    #[error("Error: {0}")]
    Error(String),
    // Define error variants here
}

#[cfg(feature = "python-integration")]
impl From<PyErr> for AffineError {
    fn from(err: PyErr) -> AffineError {
        AffineError::Error(err.to_string())
    }
}

#[cfg(feature = "python-integration")]
impl From<AffineError> for PyErr {
    fn from(err: AffineError) -> PyErr {
        pyo3::exceptions::PyException::new_err(format!("{:?}", err))
    }
}

impl From<CharacterParseError> for AffineError {
    fn from(err: CharacterParseError) -> AffineError {
        AffineError::CharacterParseError(err)
    }
}

#[derive(Debug, PartialEq, Error)]
pub enum VigenereError {
    #[error("Character parse error: {0}")]
    CharacterParseError(CharacterParseError),
    #[error("Error: {0}")]
    Error(String),
    // Define error variants here
}

#[cfg(feature = "python-integration")]
impl From<PyErr> for VigenereError {
    fn from(err: PyErr) -> VigenereError {
        VigenereError::Error(err.to_string())
    }
}

#[cfg(feature = "python-integration")]
impl From<VigenereError> for PyErr {
    fn from(err: VigenereError) -> PyErr {
        pyo3::exceptions::PyException::new_err(format!("{:?}", err))
    }
}

impl From<CharacterParseError> for VigenereError {
    fn from(err: CharacterParseError) -> VigenereError {
        VigenereError::CharacterParseError(err)
    }
}