cyfs_base/objects/
simple_group.rs1use crate::*;
2
3use std::convert::TryFrom;
4use std::str::FromStr;
5
6#[derive(Clone, Debug, RawEncode, RawDecode)]
7pub struct SimpleGroupDescContent {}
8
9impl DescContent for SimpleGroupDescContent {
10 fn obj_type() -> u16 {
11 ObjectTypeCode::SimpleGroup.into()
12 }
13
14 type OwnerType = SubDescNone;
15 type AreaType = Option<Area>;
16 type AuthorType = SubDescNone;
17 type PublicKeyType = MNPublicKey;
18}
19
20#[derive(Clone, Debug)]
21pub struct SimpleGroupBodyContent {
22 members: Vec<ObjectId>,
23 ood_list: Vec<DeviceId>,
24 ood_work_mode: Option<OODWorkMode>,
25}
26
27impl BodyContent for SimpleGroupBodyContent {
28 fn format(&self) -> u8 {
29 OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
30 }
31}
32
33impl SimpleGroupBodyContent {
34 pub fn new(
35 members: Vec<ObjectId>,
36 ood_work_mode: OODWorkMode,
37 ood_list: Vec<DeviceId>,
38 ) -> Self {
39 Self {
40 members,
41 ood_work_mode: Some(ood_work_mode),
42 ood_list,
43 }
44 }
45
46 pub fn members(&self) -> &Vec<ObjectId> {
47 &self.members
48 }
49
50 pub fn members_mut(&mut self) -> &mut Vec<ObjectId> {
51 &mut self.members
52 }
53
54 pub fn ood_list(&self) -> &Vec<DeviceId> {
55 &self.ood_list
56 }
57
58 pub fn ood_list_mut(&mut self) -> &mut Vec<DeviceId> {
59 &mut self.ood_list
60 }
61
62 pub fn ood_work_mode(&self) -> OODWorkMode {
63 self.ood_work_mode
64 .clone()
65 .unwrap_or(OODWorkMode::Standalone)
66 }
67
68 pub fn set_ood_work_mode(&mut self, ood_work_mode: OODWorkMode) {
69 self.ood_work_mode = Some(ood_work_mode);
70 }
71}
72
73impl TryFrom<protos::SimpleGroupBodyContent> for SimpleGroupBodyContent {
75 type Error = BuckyError;
76
77 fn try_from(mut value: protos::SimpleGroupBodyContent) -> BuckyResult<Self> {
78 let mut ret = Self {
79 members: ProtobufCodecHelper::decode_buf_list(value.take_members())?,
80 ood_list: ProtobufCodecHelper::decode_buf_list(value.take_ood_list())?,
81 ood_work_mode: None,
82 };
83
84 if value.has_ood_work_mode() {
85 ret.ood_work_mode = Some(OODWorkMode::from_str(value.get_ood_work_mode())?);
86 }
87
88 Ok(ret)
89 }
90}
91
92impl TryFrom<&SimpleGroupBodyContent> for protos::SimpleGroupBodyContent {
93 type Error = BuckyError;
94
95 fn try_from(value: &SimpleGroupBodyContent) -> BuckyResult<Self> {
96 let mut ret = Self::new();
97
98 ret.set_members(ProtobufCodecHelper::encode_buf_list(&value.members)?);
99 ret.set_ood_list(ProtobufCodecHelper::encode_buf_list(&value.ood_list)?);
100
101 if let Some(ood_work_mode) = &value.ood_work_mode {
102 ret.set_ood_work_mode(ood_work_mode.to_string());
103 }
104
105 Ok(ret)
106 }
107}
108
109crate::inner_impl_default_protobuf_raw_codec!(SimpleGroupBodyContent);
110
111pub type SimpleGroupType = NamedObjType<SimpleGroupDescContent, SimpleGroupBodyContent>;
112pub type SimpleGroupBuilder = NamedObjectBuilder<SimpleGroupDescContent, SimpleGroupBodyContent>;
113
114pub type SimpleGroupDesc = NamedObjectDesc<SimpleGroupDescContent>;
115pub type SimpleGroupId = NamedObjectId<SimpleGroupType>;
116pub type SimpleGroup = NamedObjectBase<SimpleGroupType>;
117
118impl SimpleGroupDesc {
119 pub fn simple_group_id(&self) -> SimpleGroupId {
120 SimpleGroupId::try_from(self.calculate_id()).unwrap()
121 }
122}
123
124impl SimpleGroup {
125 pub fn new(
126 threshold: u8,
127 owners: Vec<PublicKey>,
128 members: Vec<ObjectId>,
129 ood_work_mode: OODWorkMode,
130 ood_list: Vec<DeviceId>,
131 area: Area,
132 ) -> SimpleGroupBuilder {
133 let desc_content = SimpleGroupDescContent {};
134
135 let body_content = SimpleGroupBodyContent::new(members, ood_work_mode, ood_list);
136
137 SimpleGroupBuilder::new(desc_content, body_content)
138 .area(area)
139 .public_key((threshold, owners))
140 }
141}
142
143#[cfg(test)]
144mod test {
145 use crate::*;
146
147 #[test]
148 fn simple_group() {
149 let threshold = 0;
150
151 let members = vec![ObjectId::default()];
152
153 let ood_list = vec![DeviceId::default()];
154
155 let obj = SimpleGroup::new(
156 threshold,
157 vec![],
158 members,
159 OODWorkMode::Standalone,
160 ood_list,
161 Area::default(),
162 )
163 .build();
164 let buf = obj.to_vec().unwrap();
170
171 let decode_obj = SimpleGroup::clone_from_slice(&buf).unwrap();
172
173 assert!(obj.desc().simple_group_id() == decode_obj.desc().simple_group_id());
174 }
175}