mod antelopevalue;
mod asset;
mod crypto;
mod float128;
mod name;
mod symbol;
mod time;
mod varint;
use std::str::FromStr;
use hex::FromHexError;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
pub type Bool = bool;
pub type Int8 = i8;
pub type Int16 = i16;
pub type Int32 = i32;
pub type Int64 = i64;
pub type Int128 = i128;
pub type Uint8 = u8;
pub type Uint16 = u16;
pub type Uint32 = u32;
pub type Uint64 = u64;
pub type Uint128 = u128;
pub use varint::{VarInt32, VarUint32};
pub type Float32 = f32;
pub type Float64 = f64;
pub use float128::Float128;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
pub struct Bytes(pub Vec<u8>);
impl Bytes {
pub fn new() -> Self { Bytes(vec![]) }
pub fn from_hex<T: AsRef<[u8]>>(data: T) -> Result<Bytes, FromHexError> {
Ok(Bytes(hex::decode(data)?))
}
pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
}
impl From<Vec<u8>> for Bytes {
fn from(v: Vec<u8>) -> Bytes {
Bytes(v)
}
}
impl From<&[u8]> for Bytes {
fn from(s: &[u8]) -> Bytes {
Bytes(s.to_vec())
}
}
impl From<Bytes> for Vec<u8> {
fn from(b: Bytes) -> Vec<u8> {
b.0
}
}
impl AsRef<[u8]> for Bytes {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl Serialize for Bytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
self.to_hex().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for Bytes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let hex_repr: &str = <&str>::deserialize(deserializer)?;
Bytes::from_hex(hex_repr).map_err(|e| de::Error::custom(e.to_string()))
}
}
pub type String = std::string::String;
pub use crate::types::time::{TimePoint, TimePointSec, BlockTimestamp};
macro_rules! impl_checksum {
($typ:ident, $size:literal) => {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct $typ(pub [u8; $size]);
impl $typ {
pub fn from_hex<T: AsRef<[u8]>>(data: T) -> Result<$typ, FromHexError> {
Ok(Self(hex::decode(data)?.try_into()
.map_err(|_| FromHexError::InvalidStringLength)?))
}
pub fn to_hex(&self) -> String {
hex::encode(self.0)
}
}
impl From<[u8; $size]> for $typ {
fn from(v: [u8; $size]) -> Self {
Self(v)
}
}
impl TryFrom<&str> for $typ {
type Error = FromHexError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::from_hex(s)
}
}
impl FromStr for $typ {
type Err = FromHexError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_hex(s)
}
}
impl Default for $typ {
fn default() -> Self {
Self::from([0; $size])
}
}
impl Serialize for $typ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
self.to_hex().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for $typ {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let hex_repr: &str = <&str>::deserialize(deserializer)?;
Self::from_hex(hex_repr).map_err(|e| de::Error::custom(e.to_string()))
}
}
}
}
impl_checksum!(Checksum160, 20);
impl_checksum!(Checksum256, 32);
impl_checksum!(Checksum512, 64);
pub use crate::types::crypto::{
CryptoData, CryptoDataType, InvalidCryptoData,
KeyType, PrivateKey, PublicKey, Signature,
};
pub use name::{Name, InvalidName};
pub use symbol::{Symbol, InvalidSymbol, SymbolCode};
pub use asset::{Asset, InvalidAsset, ExtendedAsset};
pub use antelopevalue::{AntelopeType, AntelopeValue, InvalidValue};
pub type ActionName = Name;
pub type ScopeName = Name;
pub type AccountName = Name;
pub type PermissionName = Name;
pub type TableName = Name;
pub type BlockId = Checksum256;
pub type Checksum = Checksum256;
pub type TransactionId = Checksum256;
pub type Digest = Checksum256;
pub type Weight = u16;
pub type BlockNum = u32;
pub type MicroSeconds = i64;
pub type Extensions = Vec<(u16, Bytes)>;