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
use fabric_contract::contract::*;
use fabric_contract::data::*;
use serde::{Deserialize, Serialize};
use std::str::from_utf8;
use log::{debug};
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Asset {
id: String,
color: String,
size: i32,
owner: String,
appraised_value: i32,
}
impl Asset {
pub fn new(id: String, color: String, size: i32, owner: String, appraised_value: i32) -> Asset {
Asset {
id,
color,
size,
owner,
appraised_value,
}
}
pub fn get_owner(&self) -> String {
self.owner.clone()
}
pub fn update_owner(&mut self, owner: String) -> () {
self.owner = owner;
}
pub fn get_color(&self) -> String {
return self.color.clone();
}
pub fn get_id(&self) -> String {
return self.id.clone();
}
}
impl DataType for Asset {
fn to_state(&self) -> State {
let json = serde_json::to_string(self).unwrap();
debug!("ToState::{}",&json.as_str());
let buffer = json.into_bytes();
State::from((self.id.clone(), buffer))
}
fn get_key(&self) -> String {
Asset::form_key(&self.id.clone())
}
fn build_from_state(state: State) -> Self {
let b = state.value();
let str = match from_utf8(&b) {
Ok(a) => a,
Err(_) => panic!("Err"),
};
debug!("build_from_state:: {}",&str);
serde_json::from_str(str).unwrap()
}
fn form_key(k: &String) -> String {
format!("Asset::{}",k)
}
}
impl WireBufferFromReturnType<Asset> for WireBuffer {
fn from_rt(self: &mut Self, s: Asset) {
let json = serde_json::to_string(&s).unwrap();
debug!("wire buffer returning the value {}",json.as_str());
let buffer = json.into_bytes();
self.buffer = Some(buffer);
}
}
impl From<&WireBuffer> for Asset {
fn from(wb: &WireBuffer) -> Self {
match &wb.buffer {
Some(buffer) => {
match std::str::from_utf8(&buffer) {
Ok(a) => serde_json::from_str(a).unwrap(),
_ => unreachable!(),
}
}
None => panic!(),
}
}
}