From 1cf617c1f5af44016b5fa9bc972b54fe20768660 Mon Sep 17 00:00:00 2001 From: ennucore Date: Fri, 12 Aug 2022 15:41:44 +0300 Subject: [PATCH] Struct for a contract's state --- agorata_contracts/src/lib.rs | 5 +++- agorata_contracts/src/state.rs | 24 +++++++++++++++++++ agorata_contracts/src/templates.rs | 38 +----------------------------- agorata_contracts/src/value.rs | 38 ++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 38 deletions(-) create mode 100644 agorata_contracts/src/state.rs create mode 100644 agorata_contracts/src/value.rs diff --git a/agorata_contracts/src/lib.rs b/agorata_contracts/src/lib.rs index b050ce8..61f892a 100644 --- a/agorata_contracts/src/lib.rs +++ b/agorata_contracts/src/lib.rs @@ -2,8 +2,11 @@ extern crate serde; /// Message and Address types pub mod message; -/// Templating variables. +/// Templates for types. pub mod templates; +/// Variables for matching. +pub mod value; +pub mod state; #[cfg(test)] mod tests { diff --git a/agorata_contracts/src/state.rs b/agorata_contracts/src/state.rs new file mode 100644 index 0000000..edc8910 --- /dev/null +++ b/agorata_contracts/src/state.rs @@ -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, +} diff --git a/agorata_contracts/src/templates.rs b/agorata_contracts/src/templates.rs index ff7130a..b7710ba 100644 --- a/agorata_contracts/src/templates.rs +++ b/agorata_contracts/src/templates.rs @@ -1,41 +1,5 @@ use serde::{Deserialize, Serialize}; -use crate::message::Address; - -/// 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), - 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, - } - } -} +use crate::value::{Type, Value}; /// Matchers of values #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] diff --git a/agorata_contracts/src/value.rs b/agorata_contracts/src/value.rs new file mode 100644 index 0000000..77a8f7f --- /dev/null +++ b/agorata_contracts/src/value.rs @@ -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), + 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, + } + } +}