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
use crate::*;
use std::convert::TryFrom;
use std::str::FromStr;
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub struct SimpleGroupDescContent {}
impl DescContent for SimpleGroupDescContent {
fn obj_type() -> u16 {
ObjectTypeCode::SimpleGroup.into()
}
type OwnerType = SubDescNone;
type AreaType = Option<Area>;
type AuthorType = SubDescNone;
type PublicKeyType = MNPublicKey;
}
#[derive(Clone, Debug)]
pub struct SimpleGroupBodyContent {
members: Vec<ObjectId>,
ood_list: Vec<DeviceId>,
ood_work_mode: Option<OODWorkMode>,
}
impl BodyContent for SimpleGroupBodyContent {
fn format(&self) -> u8 {
OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
}
}
impl SimpleGroupBodyContent {
pub fn new(
members: Vec<ObjectId>,
ood_work_mode: OODWorkMode,
ood_list: Vec<DeviceId>,
) -> Self {
Self {
members,
ood_work_mode: Some(ood_work_mode),
ood_list,
}
}
pub fn members(&self) -> &Vec<ObjectId> {
&self.members
}
pub fn members_mut(&mut self) -> &mut Vec<ObjectId> {
&mut self.members
}
pub fn ood_list(&self) -> &Vec<DeviceId> {
&self.ood_list
}
pub fn ood_list_mut(&mut self) -> &mut Vec<DeviceId> {
&mut self.ood_list
}
pub fn ood_work_mode(&self) -> OODWorkMode {
self.ood_work_mode
.clone()
.unwrap_or(OODWorkMode::Standalone)
}
pub fn set_ood_work_mode(&mut self, ood_work_mode: OODWorkMode) {
self.ood_work_mode = Some(ood_work_mode);
}
}
impl TryFrom<protos::SimpleGroupBodyContent> for SimpleGroupBodyContent {
type Error = BuckyError;
fn try_from(mut value: protos::SimpleGroupBodyContent) -> BuckyResult<Self> {
let mut ret = Self {
members: ProtobufCodecHelper::decode_buf_list(value.take_members())?,
ood_list: ProtobufCodecHelper::decode_buf_list(value.take_ood_list())?,
ood_work_mode: None,
};
if value.has_ood_work_mode() {
ret.ood_work_mode = Some(OODWorkMode::from_str(value.get_ood_work_mode())?);
}
Ok(ret)
}
}
impl TryFrom<&SimpleGroupBodyContent> for protos::SimpleGroupBodyContent {
type Error = BuckyError;
fn try_from(value: &SimpleGroupBodyContent) -> BuckyResult<Self> {
let mut ret = Self::new();
ret.set_members(ProtobufCodecHelper::encode_buf_list(&value.members)?);
ret.set_ood_list(ProtobufCodecHelper::encode_buf_list(&value.ood_list)?);
if let Some(ood_work_mode) = &value.ood_work_mode {
ret.set_ood_work_mode(ood_work_mode.to_string());
}
Ok(ret)
}
}
crate::inner_impl_default_protobuf_raw_codec!(SimpleGroupBodyContent);
pub type SimpleGroupType = NamedObjType<SimpleGroupDescContent, SimpleGroupBodyContent>;
pub type SimpleGroupBuilder = NamedObjectBuilder<SimpleGroupDescContent, SimpleGroupBodyContent>;
pub type SimpleGroupDesc = NamedObjectDesc<SimpleGroupDescContent>;
pub type SimpleGroupId = NamedObjectId<SimpleGroupType>;
pub type SimpleGroup = NamedObjectBase<SimpleGroupType>;
impl SimpleGroupDesc {
pub fn simple_group_id(&self) -> SimpleGroupId {
SimpleGroupId::try_from(self.calculate_id()).unwrap()
}
}
impl SimpleGroup {
pub fn new(
threshold: u8,
owners: Vec<PublicKey>,
members: Vec<ObjectId>,
ood_work_mode: OODWorkMode,
ood_list: Vec<DeviceId>,
area: Area,
) -> SimpleGroupBuilder {
let desc_content = SimpleGroupDescContent {};
let body_content = SimpleGroupBodyContent::new(members, ood_work_mode, ood_list);
SimpleGroupBuilder::new(desc_content, body_content)
.area(area)
.public_key((threshold, owners))
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn simple_group() {
let threshold = 0;
let members = vec![ObjectId::default()];
let ood_list = vec![DeviceId::default()];
let obj = SimpleGroup::new(
threshold,
vec![],
members,
OODWorkMode::Standalone,
ood_list,
Area::default(),
)
.build();
let buf = obj.to_vec().unwrap();
let decode_obj = SimpleGroup::clone_from_slice(&buf).unwrap();
assert!(obj.desc().simple_group_id() == decode_obj.desc().simple_group_id());
}
}