<commit_before>extern crate indy_crypto;

use std::error;
use std::error::Error;
use std::io;
use std::fmt;
use std::string::FromUtf8Error;
use libsqlite3_sys;
use std::ffi::NulError;
use std::str::Utf8Error;

use rusqlite;
use serde_json;
use base64;

use api::ErrorCode;
use errors::common::CommonError;
use errors::ToErrorCode;


#[derive(Debug)]
pub enum WalletError {
    InvalidHandle(String),
    UnknownType(String),
    TypeAlreadyRegistered(String),
    AlreadyExists(String),
    NotFound(String),
    IncorrectPool(String),
    PluggedWalletError(ErrorCode),
    AlreadyOpened(String),
    AccessFailed(String),
    CommonError(CommonError),
    InputError(String),
    EncodingError(String),
    StorageError(String),
    EncryptionError(String),
    ItemNotFound,
    ItemAlreadyExists,
    QueryError(String),
    IOError(io::Error),
    StructureError(String),
    ExportPathExists,
    ImportPathDoesNotExist,
    NotEmpty,
}


impl fmt::Display for WalletError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            WalletError::InvalidHandle(ref description) => write!(f, "Invalid wallet handle was passed: {}", description),
            WalletError::UnknownType(ref description) => write!(f, "Unknown wallet type: {}", description),
            WalletError::TypeAlreadyRegistered(ref description) => write!(f, "Wallet type already registered: {}", description),
            WalletError::AlreadyExists(ref description) => write!(f, "Wallet with this name already exists: {}", description),
            WalletError::NotFound(ref description) => write!(f, "Wallet not found: {}", description),
            WalletError::IncorrectPool(ref description) => write!(f, "Wallet used with different pool: {}", description),
            WalletError::PluggedWalletError(err_code) => write!(f, "Plugged wallet error: {}", err_code as i32),
            WalletError::AlreadyOpened(ref description) => write!(f, "Wallet already opened: {}", description),
            WalletError::AccessFailed(ref description) => write!(f, "Wallet security error: {}", description),
            WalletError::CommonError(ref err) => err.fmt(f),
            WalletError::InputError(ref description) => write!(f, "Wallet input error: {}", description),
            WalletError::EncodingError(ref description) => write!(f, "Wallet encoding error: {}", description),
            WalletError::StorageError(ref description) => write!(f, "Wallet storage error occurred. Description: {}", description),
            WalletError::EncryptionError(ref description) => write!(f, "Wallet encryption error occurred. Description: {}", description),
            WalletError::ItemNotFound => write!(f, "Item not found"),
            WalletError::ItemAlreadyExists => write!(f, "Item already exists"),
            WalletError::QueryError(ref description) => write!(f, "{}", description),
            WalletError::IOError(ref err) => write!(f, "IO error occurred during wallet operation: {}", err.description()),
            WalletError::StructureError(ref description) => write!(f, "Invalid structure of wallet input: {}", description),
            WalletError::NotEmpty => write!(f, "Wallet is not empty"),
            WalletError::ExportPathExists => write!(f, "Export file already exists"),
            WalletError::ImportPathDoesNotExist => write!(f, "Import file does not exist"),
        }
    }
}

impl error::Error for WalletError {
    fn description(&self) -> &str {
        match *self {
            WalletError::InvalidHandle(ref description) => description,
            WalletError::UnknownType(ref description) => description,
            WalletError::TypeAlreadyRegistered(ref description) => description,
            WalletError::AlreadyExists(ref description) => description,
            WalletError::NotFound(ref description) => description,
            WalletError::IncorrectPool(ref description) => description,
            WalletError::PluggedWalletError(_) => "Plugged wallet error",
            WalletError::AlreadyOpened(ref description) => description,
            WalletError::AccessFailed(ref description) => description,
            WalletError::CommonError(ref err) => err.description(),
            WalletError::InputError(ref description) => description,
            WalletError::EncodingError(ref description) => description,
            WalletError::StorageError(ref description) => description,
            WalletError::EncryptionError(ref description) => description,
            WalletError::ItemNotFound => "Item not found",
            WalletError::ItemAlreadyExists => "Item already exists",
            WalletError::QueryError(ref description) => description,
            WalletError::IOError(ref err) => err.description(),
            WalletError::StructureError(ref description) => description,
            WalletError::NotEmpty => "Wallet is not empty",
            WalletError::ExportPathExists => "Export file already exists",
            WalletError::ImportPathDoesNotExist => "Imort file does not exist",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            WalletError::InvalidHandle(_) => None,
            WalletError::UnknownType(_) => None,
            WalletError::TypeAlreadyRegistered(_) => None,
            WalletError::AlreadyExists(_) => None,
            WalletError::NotFound(_) => None,
            WalletError::IncorrectPool(_) => None,
            WalletError::PluggedWalletError(_) => None,
            WalletError::AlreadyOpened(_) => None,
            WalletError::AccessFailed(_) => None,
            WalletError::CommonError(ref err) => Some(err),
            WalletError::InputError(_) => None,
            WalletError::EncodingError(_) => None,
            WalletError::StorageError(_) => None,
            WalletError::EncryptionError(_) => None,
            WalletError::ItemNotFound => None,
            WalletError::ItemAlreadyExists => None,
            WalletError::QueryError(_) => None,
            WalletError::IOError(ref err) => Some(err),
            WalletError::StructureError(_) => None,
            WalletError::NotEmpty => None,
            WalletError::ExportPathExists => None,
            WalletError::ImportPathDoesNotExist => None,
        }
    }
}

impl ToErrorCode for WalletError {
    fn to_error_code(&self) -> ErrorCode {
        match *self {
            WalletError::InvalidHandle(_) => ErrorCode::WalletInvalidHandle,
            WalletError::UnknownType(_) => ErrorCode::WalletUnknownTypeError,
            WalletError::TypeAlreadyRegistered(_) => ErrorCode::WalletTypeAlreadyRegisteredError,
            WalletError::AlreadyExists(_) => ErrorCode::WalletAlreadyExistsError,
            WalletError::NotFound(_) => ErrorCode::WalletNotFoundError,
            WalletError::IncorrectPool(_) => ErrorCode::WalletIncompatiblePoolError,
            WalletError::PluggedWalletError(err_code) => err_code,
            WalletError::AlreadyOpened(_) => ErrorCode::WalletAlreadyOpenedError,
            WalletError::AccessFailed(_) => ErrorCode::WalletAccessFailed,
            WalletError::CommonError(ref err) => err.to_error_code(),
            WalletError::InputError(_) => ErrorCode::WalletInputError,
            WalletError::EncodingError(_) => ErrorCode::WalletDecodingError,
            WalletError::StorageError(_) => ErrorCode::WalletStorageError,
            WalletError::EncryptionError(_) => ErrorCode::WalletEncryptonError,
            WalletError::ItemNotFound => ErrorCode::WalletItemNotFound,
            WalletError::ItemAlreadyExists => ErrorCode::WalletItemAlreadyExists,
            WalletError::QueryError(_) => ErrorCode::WalletQueryError,
            WalletError::IOError(_) => ErrorCode::WalletIOError,
            WalletError::StructureError(_) => ErrorCode::WalletStructureError,
            WalletError::NotEmpty => ErrorCode::WalletNotEmpty,
            WalletError::ExportPathExists => ErrorCode::WalletExportPathExists,
            WalletError::ImportPathDoesNotExist => ErrorCode::WalletImportPathDoesNotExist,
        }
    }
}


impl From<CommonError> for WalletError {
    fn from(err: CommonError) -> WalletError {
        WalletError::CommonError(err)
    }
}

impl From<io::Error> for WalletError {
    fn from(err: io::Error) -> WalletError {
        WalletError::IOError(err)<commit_msg>removed WalletError::IOError (CommonError instead)<commit_after>extern crate indy_crypto;

use std::error;
use std::error::Error;
use std::io;
use std::fmt;
use std::string::FromUtf8Error;
use libsqlite3_sys;
use std::ffi::NulError;
use std::str::Utf8Error;

use rusqlite;
use serde_json;
use base64;

use api::ErrorCode;
use errors::common::CommonError;
use errors::ToErrorCode;


#[derive(Debug)]
pub enum WalletError {
    InvalidHandle(String),
    UnknownType(String),
    TypeAlreadyRegistered(String),
    AlreadyExists(String),
    NotFound(String),
    IncorrectPool(String),
    PluggedWalletError(ErrorCode),
    AlreadyOpened(String),
    AccessFailed(String),
    CommonError(CommonError),
    InputError(String),
    EncodingError(String),
    StorageError(String),
    EncryptionError(String),
    ItemNotFound,
    ItemAlreadyExists,
    QueryError(String),
    StructureError(String),
    ExportPathExists,
    ImportPathDoesNotExist,
    NotEmpty,
}


impl fmt::Display for WalletError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            WalletError::InvalidHandle(ref description) => write!(f, "Invalid wallet handle was passed: {}", description),
            WalletError::UnknownType(ref description) => write!(f, "Unknown wallet type: {}", description),
            WalletError::TypeAlreadyRegistered(ref description) => write!(f, "Wallet type already registered: {}", description),
            WalletError::AlreadyExists(ref description) => write!(f, "Wallet with this name already exists: {}", description),
            WalletError::NotFound(ref description) => write!(f, "Wallet not found: {}", description),
            WalletError::IncorrectPool(ref description) => write!(f, "Wallet used with different pool: {}", description),
            WalletError::PluggedWalletError(err_code) => write!(f, "Plugged wallet error: {}", err_code as i32),
            WalletError::AlreadyOpened(ref description) => write!(f, "Wallet already opened: {}", description),
            WalletError::AccessFailed(ref description) => write!(f, "Wallet security error: {}", description),
            WalletError::CommonError(ref err) => err.fmt(f),
            WalletError::InputError(ref description) => write!(f, "Wallet input error: {}", description),
            WalletError::EncodingError(ref description) => write!(f, "Wallet encoding error: {}", description),
            WalletError::StorageError(ref description) => write!(f, "Wallet storage error occurred. Description: {}", description),
            WalletError::EncryptionError(ref description) => write!(f, "Wallet encryption error occurred. Description: {}", description),
            WalletError::ItemNotFound => write!(f, "Item not found"),
            WalletError::ItemAlreadyExists => write!(f, "Item already exists"),
            WalletError::QueryError(ref description) => write!(f, "{}", description),
            WalletError::StructureError(ref description) => write!(f, "Invalid structure of wallet input: {}", description),
            WalletError::NotEmpty => write!(f, "Wallet is not empty"),
            WalletError::ExportPathExists => write!(f, "Export file already exists"),
            WalletError::ImportPathDoesNotExist => write!(f, "Import file does not exist"),
        }
    }
}

impl error::Error for WalletError {
    fn description(&self) -> &str {
        match *self {
            WalletError::InvalidHandle(ref description) => description,
            WalletError::UnknownType(ref description) => description,
            WalletError::TypeAlreadyRegistered(ref description) => description,
            WalletError::AlreadyExists(ref description) => description,
            WalletError::NotFound(ref description) => description,
            WalletError::IncorrectPool(ref description) => description,
            WalletError::PluggedWalletError(_) => "Plugged wallet error",
            WalletError::AlreadyOpened(ref description) => description,
            WalletError::AccessFailed(ref description) => description,
            WalletError::CommonError(ref err) => err.description(),
            WalletError::InputError(ref description) => description,
            WalletError::EncodingError(ref description) => description,
            WalletError::StorageError(ref description) => description,
            WalletError::EncryptionError(ref description) => description,
            WalletError::ItemNotFound => "Item not found",
            WalletError::ItemAlreadyExists => "Item already exists",
            WalletError::QueryError(ref description) => description,
            WalletError::StructureError(ref description) => description,
            WalletError::NotEmpty => "Wallet is not empty",
            WalletError::ExportPathExists => "Export file already exists",
            WalletError::ImportPathDoesNotExist => "Imort file does not exist",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            WalletError::InvalidHandle(_) => None,
            WalletError::UnknownType(_) => None,
            WalletError::TypeAlreadyRegistered(_) => None,
            WalletError::AlreadyExists(_) => None,
            WalletError::NotFound(_) => None,
            WalletError::IncorrectPool(_) => None,
            WalletError::PluggedWalletError(_) => None,
            WalletError::AlreadyOpened(_) => None,
            WalletError::AccessFailed(_) => None,
            WalletError::CommonError(ref err) => Some(err),
            WalletError::InputError(_) => None,
            WalletError::EncodingError(_) => None,
            WalletError::StorageError(_) => None,
            WalletError::EncryptionError(_) => None,
            WalletError::ItemNotFound => None,
            WalletError::ItemAlreadyExists => None,
            WalletError::QueryError(_) => None,
            WalletError::StructureError(_) => None,
            WalletError::NotEmpty => None,
            WalletError::ExportPathExists => None,
            WalletError::ImportPathDoesNotExist => None,
        }
    }
}

impl ToErrorCode for WalletError {
    fn to_error_code(&self) -> ErrorCode {
        match *self {
            WalletError::InvalidHandle(_) => ErrorCode::WalletInvalidHandle,
            WalletError::UnknownType(_) => ErrorCode::WalletUnknownTypeError,
            WalletError::TypeAlreadyRegistered(_) => ErrorCode::WalletTypeAlreadyRegisteredError,
            WalletError::AlreadyExists(_) => ErrorCode::WalletAlreadyExistsError,
            WalletError::NotFound(_) => ErrorCode::WalletNotFoundError,
            WalletError::IncorrectPool(_) => ErrorCode::WalletIncompatiblePoolError,
            WalletError::PluggedWalletError(err_code) => err_code,
            WalletError::AlreadyOpened(_) => ErrorCode::WalletAlreadyOpenedError,
            WalletError::AccessFailed(_) => ErrorCode::WalletAccessFailed,
            WalletError::CommonError(ref err) => err.to_error_code(),
            WalletError::InputError(_) => ErrorCode::WalletInputError,
            WalletError::EncodingError(_) => ErrorCode::WalletDecodingError,
            WalletError::StorageError(_) => ErrorCode::WalletStorageError,
            WalletError::EncryptionError(_) => ErrorCode::WalletEncryptonError,
            WalletError::ItemNotFound => ErrorCode::WalletItemNotFound,
            WalletError::ItemAlreadyExists => ErrorCode::WalletItemAlreadyExists,
            WalletError::QueryError(_) => ErrorCode::WalletQueryError,
            WalletError::StructureError(_) => ErrorCode::WalletStructureError,
            WalletError::NotEmpty => ErrorCode::WalletNotEmpty,
            WalletError::ExportPathExists => ErrorCode::WalletExportPathExists,
            WalletError::ImportPathDoesNotExist => ErrorCode::WalletImportPathDoesNotExist,
        }
    }
}


impl From<CommonError> for WalletError {
    fn from(err: CommonError) -> WalletError {
        WalletError::CommonError(err)
    }
}

impl From<io::Error> for WalletError {
    fn from(err: io::Error) -> WalletError {
        WalletError::from(CommonError::from(err))
{'fraction_non_alphanumeric': 0.12431677395845986, 'fraction_numerical': 0.0008502368516944005, 'mean_word_length': 3.494268558951965, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 193, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2106859', 'n_tokens_mistral': 4307, 'n_tokens_neox': 4335, 'n_words': 855}