use std::error::Error;
use std::fmt::{self, Display, Formatter};
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, RustcEncodable, RustcDecodable)]
pub enum GetError {
NoSuchAccount,
NoSuchData,
NetworkOther(String),
}
impl<T: Into<String>> From<T> for GetError {
fn from(err: T) -> Self {
GetError::NetworkOther(err.into())
}
}
impl Display for GetError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match *self {
GetError::NoSuchAccount => write!(formatter, "Account does not exist for client"),
GetError::NoSuchData => write!(formatter, "Requested data not found"),
GetError::NetworkOther(ref error) => {
write!(formatter, "Error on Vault network: {}", error)
}
}
}
}
impl Error for GetError {
fn description(&self) -> &str {
match *self {
GetError::NoSuchAccount => "No such account",
GetError::NoSuchData => "No such data",
GetError::NetworkOther(ref error) => error,
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, RustcEncodable, RustcDecodable)]
pub enum MutationError {
NoSuchAccount,
AccountExists,
NoSuchData,
DataExists,
DataTooLarge,
LowBalance,
InvalidSuccessor,
InvalidOperation,
NetworkFull,
NetworkOther(String),
}
impl<T: Into<String>> From<T> for MutationError {
fn from(err: T) -> Self {
MutationError::NetworkOther(err.into())
}
}
impl Display for MutationError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match *self {
MutationError::NoSuchAccount => write!(formatter, "Account does not exist for client"),
MutationError::AccountExists => write!(formatter, "Account already exists for client"),
MutationError::NoSuchData => write!(formatter, "Requested data not found"),
MutationError::DataExists => write!(formatter, "Data given already exists"),
MutationError::DataTooLarge => write!(formatter, "Data given is too large"),
MutationError::LowBalance => {
write!(formatter, "Insufficient account balance for this operation")
}
MutationError::InvalidSuccessor => {
write!(formatter,
"Data given is not a valid successor of stored data")
}
MutationError::InvalidOperation => {
write!(formatter, "Requested operation is not allowed")
}
MutationError::NetworkFull => {
write!(formatter, "Network cannot store any further data")
}
MutationError::NetworkOther(ref error) => {
write!(formatter, "Error on Vault network: {}", error)
}
}
}
}
impl Error for MutationError {
fn description(&self) -> &str {
match *self {
MutationError::NoSuchAccount => "No such account",
MutationError::AccountExists => "Account exists",
MutationError::NoSuchData => "No such data",
MutationError::DataExists => "Data exists",
MutationError::DataTooLarge => "Data is too large",
MutationError::LowBalance => "Low account balance",
MutationError::InvalidSuccessor => "Invalid data successor",
MutationError::InvalidOperation => "Invalid operation",
MutationError::NetworkFull => "Network full",
MutationError::NetworkOther(ref error) => error,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn conversion_from_str_literal() {
fn mutate() -> Result<(), MutationError> {
Err("Mutation")?
}
let err_get = GetError::from("Get");
let err_mutation = mutate().unwrap_err();
match (err_get, err_mutation) {
(GetError::NetworkOther(val0), MutationError::NetworkOther(val1)) => {
assert_eq!(&val0, "Get");
assert_eq!(&val1, "Mutation");
}
err => panic!("Unexpected conversion: {:?}", err),
}
}
}