Browse Source

Struct for a contract's state

master
Lev 2 years ago
parent
commit
1cf617c1f5
  1. 5
      agorata_contracts/src/lib.rs
  2. 24
      agorata_contracts/src/state.rs
  3. 38
      agorata_contracts/src/templates.rs
  4. 38
      agorata_contracts/src/value.rs

5
agorata_contracts/src/lib.rs

@ -2,8 +2,11 @@ extern crate serde;
/// Message and Address types /// Message and Address types
pub mod message; pub mod message;
/// Templating variables. /// Templates for types.
pub mod templates; pub mod templates;
/// Variables for matching.
pub mod value;
pub mod state;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

24
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<Value>,
}

38
agorata_contracts/src/templates.rs

@ -1,41 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::message::Address; use crate::value::{Type, Value};
/// 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,
}
}
}
/// Matchers of values /// Matchers of values
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]

38
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<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…
Cancel
Save