Lev
2 years ago
4 changed files with 67 additions and 38 deletions
@ -0,0 +1,24 @@
|
||||
use serde::{Deserialize, Serialize}; |
||||
use crate::value::{Type, Value}; |
||||
|
||||
/// Description of a smart contract's state format.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
||||
pub struct StateDescriptor { |
||||
/// `Vec<(variable name, variable type)>`
|
||||
pub variables: Vec<(String, Type)>, |
||||
/// Template to turn variable values into the state bytes and back.
|
||||
pub template: String, |
||||
} |
||||
|
||||
impl StateDescriptor { |
||||
pub fn new(variables: Vec<(String, Type)>) -> Self { |
||||
StateDescriptor { variables, template: "".to_string() } |
||||
} |
||||
} |
||||
|
||||
/// The contract's state itself.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
||||
pub struct State { |
||||
/// The state's variables.
|
||||
pub variables: Vec<Value>, |
||||
} |
@ -0,0 +1,38 @@
|
||||
use crate::message::Address; |
||||
use serde::{Deserialize, Serialize}; |
||||
|
||||
/// Values used for variables in templates.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
||||
pub enum Value { |
||||
Int(i64), |
||||
Bool(bool), |
||||
Address(Address), |
||||
Token(Address), |
||||
Data(Vec<u8>), |
||||
String(String), |
||||
} |
||||
|
||||
/// Types for variable values.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Copy)] |
||||
pub enum Type { |
||||
Int, |
||||
Bool, |
||||
Address, |
||||
Token, |
||||
Data, |
||||
String, |
||||
} |
||||
|
||||
impl Value { |
||||
/// Returns the type of the value.
|
||||
pub fn var_type(&self) -> Type { |
||||
match self { |
||||
Value::Int(_) => Type::Int, |
||||
Value::Bool(_) => Type::Bool, |
||||
Value::Address(_) => Type::Address, |
||||
Value::Token(_) => Type::Token, |
||||
Value::Data(_) => Type::Data, |
||||
Value::String(_) => Type::String, |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue