1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * SPDX-License-Identifier: Apache-2.0
 */
#![allow(dead_code)]
#![allow(unused_imports)]
use crate::dataapi::{typeschema::ContractType, typeschema::Format, TypeSchema};
use std::fmt;
use std::str::FromStr;
use Format::Other;
use log::{debug, trace};
#[derive(Debug, Clone, Copy)]
/// Should this transaction be submitted or evaluated?
pub enum TxType {
    Submit,
    Evaluate,
}
#[derive(Debug, Clone)]
pub struct ParameterDefn {
    pub name: String,
    pub type_schema: TypeSchema,
    pub transient: bool,
}

impl std::fmt::Display for ParameterDefn {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
        write!(f, "{:?} {:?}", self.name, self.type_schema)
    }
}

impl std::convert::From<&str> for ParameterDefn {
    fn from(tx: &str) -> Self {
        // parse out the contract_name here
        let type_name: String;
        let arg_name: String;
        match tx.find(':') {
            Some(s) => {
                arg_name = tx[..s - 1].to_string();
                type_name = tx[s + 2..].to_string();
            }
            None => panic!("Code is not correct"),
        }

        debug!("{} -> {} {}",tx,arg_name,type_name);
        
        Self {
            name: arg_name,
            transient: false,
            type_schema: TypeSchema {
                contract_type: ContractType::from_str(&type_name[..]).unwrap(),
                format: Option::None,
            },
        }
    }
}

#[derive(Default, Debug, Clone)]
pub struct TransactionFn {
    name: String,
    return_type: TypeSchema,
    parameters: Vec<ParameterDefn>,
    transient_ids: Vec<String>,
}

impl fmt::Display for TransactionFn {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TxFn <{}> {:?}", self.name, self.parameters)
    }
}

impl TransactionFn {
    pub fn new(name: &str) -> TransactionFn {
        TransactionFn {
            name: String::from(name),
            return_type: TypeSchema {
                contract_type: ContractType::CTString,
                format: Option::None,
            },
            parameters: vec![],
            transient_ids: vec![],
        }
    }

    pub fn get_name(&self) -> String {
        self.name.clone()
    }

    pub fn get_return(&self) -> TypeSchema {
        self.return_type
    }

    pub fn get_parameters(&self) -> Vec<ParameterDefn> {
        self.parameters.clone()
    }

    pub fn get_transient_ids(&self) -> Vec<String> {
        self.transient_ids.clone()
    }
}

#[derive(Default, Debug, Clone)]
pub struct TransactionFnBuilder {
    name: String,
    return_type: TypeSchema,
    parameters: Vec<ParameterDefn>,
    transient_ids: Vec<String>,
}

impl TransactionFnBuilder {
    pub fn new() -> TransactionFnBuilder {
        TransactionFnBuilder::default()
    }

    pub fn name(&mut self, name: &str)  {
        self.name = name.to_string();
    }

    pub fn return_type(&mut self, return_type: TypeSchema) {
        self.return_type = return_type;
    }

    pub fn add_arg(&mut self, arg: &str) {
        self.parameters.push(ParameterDefn::from(arg));
    }

    pub fn add_transient_id(&mut self, arg: &str) {
        self.transient_ids.push(arg.to_string());
    }

    pub fn build(self) -> TransactionFn {
        TransactionFn {
            name: self.name,
            return_type: self.return_type,
            parameters: self.parameters,
            transient_ids: self.transient_ids,
        }
    }
}