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
// Crate inclusion
//
// For serialization 
extern crate serde;
extern crate serde_json;

// Use statements
//
// Standard error 
use std::io::Error;
// String functionality
#[allow(unused_imports)]
use std::string::{ String, ToString };
// For File io
#[allow(unused_imports)]
use std::fs::{ File, OpenOptions };
use std::io::prelude::*;

/*
 *
 * Transaction:
 *     - This file holds the functionality for creating and using transactions 
 *
 */

// Transaction struct 
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Transaction
{

    // ID for the transation
    uid: u64,
    // username
    username: String,
    // content
    content: String,
    // timestamp
    timestamp: String
    
}

// Functions for transaction
impl Transaction
{

    // Constructor for a new transaction 
    pub fn new( uid: u64, username: String, content: String, timestamp: String ) -> Self
    {

        Transaction
        {
            
            uid: uid,
            username: username,
            content: content,
            timestamp: timestamp

        }
        
    }
    
    // Write the transaction to a file
    #[allow(dead_code)]
    pub fn write_to( &self, filename: &str ) -> Result< (), Error >
    {

        // Open the filepath with append specification
        let mut file = OpenOptions::new(  ).append( true ).create( true ).open( filename )?;
        // Write the json to the filepath
        file.write_all( serde_json::to_string( &self )?.as_ref() );
        // Return the result 
        Ok( () )
        
    }

    // Read in from json 
    #[allow(dead_code)]
    pub fn read_json( filename: &str ) -> Result< String, Error >
    {

        // Open a readable file at the filepath
        let mut file = OpenOptions::new( ).read( true ).open( filename )?;
        // Read in json
        let mut json = String::new();
        file.read_to_string( &mut json );
        // Return the string
        Ok( json )
        
    }

    // Read in from json and construct transaction
    #[allow(dead_code)]
    pub fn read_and_construct( filename: &str ) -> Result< Transaction, Error >
    {

        // Construct the transaction
        let string = Transaction::read_json( filename ).expect( "Could not read json" );
        let transaction : Transaction = serde_json::from_str( string.as_ref() ).expect( "Could not convert to transaction" );
        // Return the transaction
        Ok( transaction )
        
    }

    // Returns the transactions value
    #[allow(dead_code)]
    pub fn get_value( &self ) -> &String
    {

        &self.content
        
    }

}

// Returns a dummy transaction
pub fn dummy() -> Transaction
{

    Transaction
    {

        uid: 5,
        username: "name".to_string(),
        content: "hello".to_string(),
        timestamp: "now".to_string()
            
    }
    
}