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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::*;
use std::convert::TryFrom;
use serde::Serialize;
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub struct OrgDescContent {}
impl DescContent for OrgDescContent {
fn obj_type() -> u16 {
ObjectTypeCode::Org.into()
}
type OwnerType = Option<ObjectId>;
type AreaType = SubDescNone;
type AuthorType = SubDescNone;
type PublicKeyType = SubDescNone;
}
#[derive(Clone, Debug, Serialize)]
pub struct Director {
pub id: ObjectId,
pub right: u8,
}
pub struct BoardOfDirector {}
pub enum DepartmentMember {}
pub struct Department {}
#[derive(Clone, Debug, Serialize)]
pub struct OrgMember {
pub id: ObjectId,
pub right: u8,
pub shares: u64,
}
#[derive(Clone, Debug, Serialize)]
pub struct OrgBodyContent {
pub members: Vec<OrgMember>,
pub directors: Vec<Director>,
pub total_equity: u64,
}
impl BodyContent for OrgBodyContent {
fn format(&self) -> u8 {
OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
}
}
impl TryFrom<protos::Director> for Director {
type Error = BuckyError;
fn try_from(mut value: protos::Director) -> BuckyResult<Self> {
Ok(Self {
id: ProtobufCodecHelper::decode_buf(value.take_id())?,
right: value.get_right() as u8,
})
}
}
impl TryFrom<&Director> for protos::Director {
type Error = BuckyError;
fn try_from(value: &Director) -> BuckyResult<Self> {
let mut ret = Self::new();
ret.set_id(value.id.to_vec()?);
ret.set_right(value.right as u32);
Ok(ret)
}
}
impl TryFrom<protos::OrgMember> for OrgMember {
type Error = BuckyError;
fn try_from(mut value: protos::OrgMember) -> BuckyResult<Self> {
Ok(Self {
id: ProtobufCodecHelper::decode_buf(value.take_id())?,
right: value.get_right() as u8,
shares: value.get_shares(),
})
}
}
impl TryFrom<&OrgMember> for protos::OrgMember {
type Error = BuckyError;
fn try_from(value: &OrgMember) -> BuckyResult<Self> {
let mut ret = Self::new();
ret.set_id(value.id.to_vec()?);
ret.set_right(value.right as u32);
ret.set_shares(value.shares);
Ok(ret)
}
}
impl TryFrom<protos::OrgBodyContent> for OrgBodyContent {
type Error = BuckyError;
fn try_from(mut value: protos::OrgBodyContent) -> BuckyResult<Self> {
Ok(Self {
members: ProtobufCodecHelper::decode_nested_list(value.take_members())?,
directors: ProtobufCodecHelper::decode_nested_list(value.take_directors())?,
total_equity: value.total_equity,
})
}
}
impl TryFrom<&OrgBodyContent> for protos::OrgBodyContent {
type Error = BuckyError;
fn try_from(value: &OrgBodyContent) -> BuckyResult<Self> {
let mut ret = Self::new();
ret.set_members(ProtobufCodecHelper::encode_nested_list(&value.members)?);
ret.set_directors(ProtobufCodecHelper::encode_nested_list(&value.directors)?);
ret.set_total_equity(value.total_equity);
Ok(ret)
}
}
crate::inner_impl_default_protobuf_raw_codec!(OrgBodyContent);
pub type OrgType = NamedObjType<OrgDescContent, OrgBodyContent>;
pub type OrgBuilder = NamedObjectBuilder<OrgDescContent, OrgBodyContent>;
pub type OrgDesc = NamedObjectDesc<OrgDescContent>;
pub type OrgId = NamedObjectId<OrgType>;
pub type Org = NamedObjectBase<OrgType>;
impl OrgDesc {
pub fn action_id(&self) -> OrgId {
OrgId::try_from(self.calculate_id()).unwrap()
}
}
impl NamedObjectBase<OrgType> {
pub fn new(members: Vec<OrgMember>, directors: Vec<Director>) -> OrgBuilder {
let desc_content = OrgDescContent {};
let body_content = OrgBodyContent {
members,
directors,
total_equity: 0,
};
OrgBuilder::new(desc_content, body_content)
}
pub fn members(&self) -> &Vec<OrgMember> {
&self.body().as_ref().unwrap().content().members
}
pub fn members_mut(&mut self) -> &mut Vec<OrgMember> {
&mut self.body_mut().as_mut().unwrap().content_mut().members
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn org() {
let action = Org::new(vec![], vec![]).build();
let buf = action.to_vec().unwrap();
let _obj = Org::clone_from_slice(&buf).unwrap();
}
}