use std::fmt::{Display, Formatter, Error};
use hex::ToHex;
#[derive(Debug, PartialEq, Clone)]
pub enum Token {
Address([u8;20]),
FixedBytes(Vec<u8>),
Bytes(Vec<u8>),
Int([u8;32]),
Uint([u8;32]),
Bool(bool),
String(String),
FixedArray(Vec<Token>),
Array(Vec<Token>),
}
impl Display for Token {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match *self {
Token::Bool(b) => write!(f, "{}", b),
Token::String(ref s) => write!(f, "{}", s),
Token::Address(ref a) => write!(f, "{}", a.to_hex()),
Token::Bytes(ref bytes) | Token::FixedBytes(ref bytes) => write!(f, "{}", bytes.to_hex()),
Token::Uint(ref i) | Token::Int(ref i) => write!(f, "{}", i.to_hex()),
Token::Array(ref arr) | Token::FixedArray(ref arr) => {
let s = arr.iter()
.map(|ref t| format!("{}", t))
.collect::<Vec<String>>()
.join(",");
write!(f, "[{}]", s)
}
}
}
}
impl Token {
pub fn to_address(self) -> Option<[u8; 20]> {
match self {
Token::Address(address) => Some(address),
_ => None,
}
}
pub fn to_fixed_bytes(self) -> Option<Vec<u8>> {
match self {
Token::FixedBytes(bytes) => Some(bytes),
_ => None,
}
}
pub fn to_bytes(self) -> Option<Vec<u8>> {
match self {
Token::Bytes(bytes) => Some(bytes),
_ => None,
}
}
pub fn to_int(self) -> Option<[u8; 32]> {
match self {
Token::Int(int) => Some(int),
_ => None,
}
}
pub fn to_uint(self) -> Option<[u8; 32]> {
match self {
Token::Uint(uint) => Some(uint),
_ => None,
}
}
pub fn to_bool(self) -> Option<bool> {
match self {
Token::Bool(b) => Some(b),
_ => None,
}
}
pub fn to_string(self) -> Option<String> {
match self {
Token::String(s) => Some(s),
_ => None,
}
}
pub fn to_fixed_array(self) -> Option<Vec<Token>> {
match self {
Token::FixedArray(arr) => Some(arr),
_ => None,
}
}
pub fn to_array(self) -> Option<Vec<Token>> {
match self {
Token::Array(arr) => Some(arr),
_ => None,
}
}
}