cyfs_base/objects/
org.rs

1use crate::*;
2
3use std::convert::TryFrom;
4use serde::Serialize;
5
6#[derive(Clone, Debug, RawEncode, RawDecode)]
7pub struct OrgDescContent {}
8
9impl DescContent for OrgDescContent {
10    fn obj_type() -> u16 {
11        ObjectTypeCode::Org.into()
12    }
13
14    type OwnerType = Option<ObjectId>;
15    type AreaType = SubDescNone;
16    type AuthorType = SubDescNone;
17    type PublicKeyType = SubDescNone;
18}
19
20#[derive(Clone, Debug, Serialize)]
21pub struct Director {
22    pub id: ObjectId,
23    pub right: u8,
24}
25
26pub struct BoardOfDirector {}
27
28pub enum DepartmentMember {}
29
30pub struct Department {}
31
32#[derive(Clone, Debug, Serialize)]
33pub struct OrgMember {
34    pub id: ObjectId,
35    pub right: u8,
36    pub shares: u64,
37}
38
39#[derive(Clone, Debug, Serialize)]
40pub struct OrgBodyContent {
41    pub members: Vec<OrgMember>,
42    pub directors: Vec<Director>,
43    pub total_equity: u64,
44}
45
46impl BodyContent for OrgBodyContent {
47    fn format(&self) -> u8 {
48        OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
49    }
50}
51
52// body使用protobuf编解码
53impl TryFrom<protos::Director> for Director {
54    type Error = BuckyError;
55
56    fn try_from(mut value: protos::Director) -> BuckyResult<Self> {
57        Ok(Self {
58            id: ProtobufCodecHelper::decode_buf(value.take_id())?,
59            right: value.get_right() as u8,
60        })
61    }
62}
63
64impl TryFrom<&Director> for protos::Director {
65    type Error = BuckyError;
66
67    fn try_from(value: &Director) -> BuckyResult<Self> {
68        let mut ret = Self::new();
69        ret.set_id(value.id.to_vec()?);
70        ret.set_right(value.right as u32);
71
72        Ok(ret)
73    }
74}
75
76impl TryFrom<protos::OrgMember> for OrgMember {
77    type Error = BuckyError;
78
79    fn try_from(mut value: protos::OrgMember) -> BuckyResult<Self> {
80        Ok(Self {
81            id: ProtobufCodecHelper::decode_buf(value.take_id())?,
82            right: value.get_right() as u8,
83            shares: value.get_shares(),
84        })
85    }
86}
87
88impl TryFrom<&OrgMember> for protos::OrgMember {
89    type Error = BuckyError;
90
91    fn try_from(value: &OrgMember) -> BuckyResult<Self> {
92        let mut ret = Self::new();
93        ret.set_id(value.id.to_vec()?);
94        ret.set_right(value.right as u32);
95        ret.set_shares(value.shares);
96
97        Ok(ret)
98    }
99}
100
101impl TryFrom<protos::OrgBodyContent> for OrgBodyContent {
102    type Error = BuckyError;
103
104    fn try_from(mut value: protos::OrgBodyContent) -> BuckyResult<Self> {
105        Ok(Self {
106            members: ProtobufCodecHelper::decode_nested_list(value.take_members())?,
107            directors: ProtobufCodecHelper::decode_nested_list(value.take_directors())?,
108            total_equity: value.total_equity,
109        })
110    }
111}
112
113impl TryFrom<&OrgBodyContent> for protos::OrgBodyContent {
114    type Error = BuckyError;
115
116    fn try_from(value: &OrgBodyContent) -> BuckyResult<Self> {
117        let mut ret = Self::new();
118        ret.set_members(ProtobufCodecHelper::encode_nested_list(&value.members)?);
119        ret.set_directors(ProtobufCodecHelper::encode_nested_list(&value.directors)?);
120        ret.set_total_equity(value.total_equity);
121
122        Ok(ret)
123    }
124}
125
126crate::inner_impl_default_protobuf_raw_codec!(OrgBodyContent);
127
128pub type OrgType = NamedObjType<OrgDescContent, OrgBodyContent>;
129pub type OrgBuilder = NamedObjectBuilder<OrgDescContent, OrgBodyContent>;
130
131pub type OrgDesc = NamedObjectDesc<OrgDescContent>;
132pub type OrgId = NamedObjectId<OrgType>;
133pub type Org = NamedObjectBase<OrgType>;
134
135impl OrgDesc {
136    pub fn action_id(&self) -> OrgId {
137        OrgId::try_from(self.calculate_id()).unwrap()
138    }
139}
140
141impl NamedObjectBase<OrgType> {
142    pub fn new(members: Vec<OrgMember>, directors: Vec<Director>) -> OrgBuilder {
143        let desc_content = OrgDescContent {};
144        let body_content = OrgBodyContent {
145            members,
146            directors,
147            total_equity: 0,
148        };
149        OrgBuilder::new(desc_content, body_content)
150    }
151
152    pub fn members(&self) -> &Vec<OrgMember> {
153        &self.body().as_ref().unwrap().content().members
154    }
155
156    pub fn members_mut(&mut self) -> &mut Vec<OrgMember> {
157        &mut self.body_mut().as_mut().unwrap().content_mut().members
158    }
159}
160
161#[cfg(test)]
162mod test {
163    use crate::*;
164    //use std::path::Path;
165
166    #[test]
167    fn org() {
168        let action = Org::new(vec![], vec![]).build();
169
170        // let p = Path::new("f:\\temp\\org.obj");
171        // if p.parent().unwrap().exists() {
172        //     action.clone().encode_to_file(p, false);
173        // }
174
175        let buf = action.to_vec().unwrap();
176        let _obj = Org::clone_from_slice(&buf).unwrap();
177    }
178}