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
extern crate serde;
extern crate serde_json;
use std::io::Error;
#[allow(unused_imports)]
use std::string::{ String, ToString };
#[allow(unused_imports)]
use std::fs::{ File, OpenOptions };
use std::io::prelude::*;
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Transaction
{
uid: u64,
username: String,
content: String,
timestamp: String
}
impl Transaction
{
pub fn new( uid: u64, username: String, content: String, timestamp: String ) -> Self
{
Transaction
{
uid: uid,
username: username,
content: content,
timestamp: timestamp
}
}
#[allow(dead_code)]
pub fn write_to( &self, filename: &str ) -> Result< (), Error >
{
let mut file = OpenOptions::new( ).append( true ).create( true ).open( filename )?;
file.write_all( serde_json::to_string( &self )?.as_ref() );
Ok( () )
}
#[allow(dead_code)]
pub fn read_json( filename: &str ) -> Result< String, Error >
{
let mut file = OpenOptions::new( ).read( true ).open( filename )?;
let mut json = String::new();
file.read_to_string( &mut json );
Ok( json )
}
#[allow(dead_code)]
pub fn read_and_construct( filename: &str ) -> Result< Transaction, Error >
{
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" );
Ok( transaction )
}
#[allow(dead_code)]
pub fn get_value( &self ) -> &String
{
&self.content
}
}
pub fn dummy() -> Transaction
{
Transaction
{
uid: 5,
username: "name".to_string(),
content: "hello".to_string(),
timestamp: "now".to_string()
}
}