fvm-std 1.0.0

tool for user to write contract which will be run in hyperchain with rust
Documentation
use crate::prelude::*;
use fixed_hash::construct_fixed_hash;
use scale::{Decode, Encode};
use serde::{Serialize, Serializer};
use scale_info::TypeInfo;

construct_fixed_hash! {
/// A byte array of length 32 representing the block hash, etc.
///
    #[derive(Encode, Decode, TypeInfo)]
    pub struct H256(32);
}

construct_fixed_hash! {
/// A byte array of length 20 representing the Address.
///
    #[derive(Encode, Decode, TypeInfo)]
    pub struct H160(20);
}

impl AsRef<H160> for H160 {
    fn as_ref(&self) -> &H160 {
        self
    }
}

impl AsRef<H256> for H256 {
    fn as_ref(&self) -> &H256 {
        self
    }
}

impl H256 {
    pub fn to_hex_string(&self) -> String {
        in_to_hex_string(&self.0)
    }
}

fn in_to_hex_string(data: &[u8]) -> String {
    use core::fmt::Write;
    let mut s = String::with_capacity(data.len() * 2);
    for v in data.iter() {
        write!(s, "{:02x}", *v).unwrap();
    }
    s
}

/// Byte array of length 20
///
/// #![feature(proc_macro_hygiene)]
/// use fvm_std::types::Address;
/// let a = Address::default();
///
pub type Address = H160;


impl Address {
    pub fn to_hex_string(&self) -> String {
        in_to_hex_string(&self.0)
    }
}

impl Serialize for Address {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where S: Serializer {
        serializer.serialize_str(self.to_hex_string().as_str())
    }
}


impl H160 {
    pub const fn new(val: [u8; 20]) -> Self {
        H160(val)
    }
}

impl H256 {
    pub const fn new(val: [u8; 32]) -> Self {
        H256(val)
    }
}

impl Serialize for H256 {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where S: Serializer {
        serializer.serialize_str(self.to_hex_string().as_str())
    }
}

// CRITICAL ERROR WARNING NOTICE INFO DEBUG
pub enum LogLevel {
    CRITICAL,
    ERROR,
    WARNING,
    NOTICE,
    INFO,
    DEBUG,
}

impl LogLevel {
    pub fn to_int(&self) -> u32 {
        let res = match self {
            LogLevel::CRITICAL => 0,
            LogLevel::ERROR => 1,
            LogLevel::WARNING => 2,
            LogLevel::NOTICE => 3,
            LogLevel::INFO => 4,
            LogLevel::DEBUG => 5,
        };
        return res;
    }
}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;
    use crate::types::{Address, H160, H256, in_to_hex_string};
    use scale::Encode;


    #[test]
    fn test_to_hex_string() {
        assert_eq!("e68891e4bbac616263".to_string(), in_to_hex_string("我们abc".as_bytes()))
    }

    #[test]
    fn test_types() {
        let h160 = H160::new([1; 20]);
        let h_encode = h160.encode();
        assert_eq!(h_encode.as_slice(), &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);

        let address = Address::new([1; 20]);
        let a_encode = address.encode();
        assert_eq!(a_encode.as_slice(), &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);

        let h256 = H256::new([1; 32]);
        let h2_encode = h256.encode();
        assert_eq!(h2_encode.as_slice(), &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
    }
}