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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use crate::root_state::*;
use cyfs_base::*;
use cyfs_core::codec::protos::core_objects as protos;
use cyfs_core::*;

use std::convert::{TryFrom, TryInto};
use serde::Serialize;

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub struct AdminGlobalStateAccessModeData {
    pub category: GlobalStateCategory,
    pub access_mode: GlobalStateAccessMode,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub enum AdminCommand {
    GlobalStateAccessMode(AdminGlobalStateAccessModeData),
}

#[derive(Debug, Clone, Serialize)]
pub struct AdminDescContent {
    pub target: DeviceId,
    pub cmd: AdminCommand,
}

impl DescContent for AdminDescContent {
    fn obj_type() -> u16 {
        CoreObjectType::Admin as u16
    }

    fn format(&self) -> u8 {
        OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
    }

    type OwnerType = Option<ObjectId>;
    type AreaType = SubDescNone;
    type AuthorType = SubDescNone;
    type PublicKeyType = SubDescNone;
}


#[derive(Debug, Clone, Serialize)]
pub struct AdminBodyContent {}

impl BodyContent for AdminBodyContent {
    fn format(&self) -> u8 {
        OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
    }
}

type AdminObjectType = NamedObjType<AdminDescContent, AdminBodyContent>;
type AdminObjectBuilder = NamedObjectBuilder<AdminDescContent, AdminBodyContent>;
type AdminObjectDesc = NamedObjectDesc<AdminDescContent>;

pub type AdminObjectId = NamedObjectId<AdminObjectType>;
pub type AdminObject = NamedObjectBase<AdminObjectType>;

impl TryFrom<protos::AdminGlobalStateAccessModeData> for AdminGlobalStateAccessModeData {
    type Error = BuckyError;

    fn try_from(value: protos::AdminGlobalStateAccessModeData) -> BuckyResult<Self> {
        let category = match value.category {
            protos::AdminGlobalStateAccessModeData_Category::RootState => {
                GlobalStateCategory::RootState
            }
            protos::AdminGlobalStateAccessModeData_Category::LocalCache => {
                GlobalStateCategory::LocalCache
            }
        };

        let access_mode = match value.access_mode {
            protos::AdminGlobalStateAccessModeData_AccessMode::Read => GlobalStateAccessMode::Read,
            protos::AdminGlobalStateAccessModeData_AccessMode::Write => {
                GlobalStateAccessMode::Write
            }
        };

        Ok(Self {
            category,
            access_mode,
        })
    }
}

impl TryFrom<&AdminGlobalStateAccessModeData> for protos::AdminGlobalStateAccessModeData {
    type Error = BuckyError;

    fn try_from(value: &AdminGlobalStateAccessModeData) -> BuckyResult<Self> {
        let category = match value.category {
            GlobalStateCategory::RootState => {
                protos::AdminGlobalStateAccessModeData_Category::RootState
            }
            GlobalStateCategory::LocalCache => {
                protos::AdminGlobalStateAccessModeData_Category::LocalCache
            }
        };

        let access_mode = match value.access_mode {
            GlobalStateAccessMode::Read => protos::AdminGlobalStateAccessModeData_AccessMode::Read,
            GlobalStateAccessMode::Write => {
                protos::AdminGlobalStateAccessModeData_AccessMode::Write
            }
        };

        let mut ret = Self::new();
        ret.set_category(category);
        ret.set_access_mode(access_mode);

        Ok(ret)
    }
}

impl_default_protobuf_raw_codec!(AdminGlobalStateAccessModeData);

impl TryFrom<protos::AdminDescContent> for AdminDescContent {
    type Error = BuckyError;

    fn try_from(mut value: protos::AdminDescContent) -> BuckyResult<Self> {
        let cmd = match value.cmd {
            protos::AdminDescContent_Command::GlobalStateAccessMode => {
                if !value.has_global_state_access_mode() {
                    let msg = format!(
                        "invalid AdminDescContent global_state_access_mode field! {:?}",
                        value
                    );
                    error!("{}", msg);
                    return Err(BuckyError::new(BuckyErrorCode::InvalidFormat, msg));
                }

                let data =
                    ProtobufCodecHelper::decode_nested_item(value.take_global_state_access_mode())?;
                AdminCommand::GlobalStateAccessMode(data)
            }
        };

        let target = ProtobufCodecHelper::decode_buf(value.take_target())?;

        let ret = Self { target, cmd };

        Ok(ret)
    }
}

impl TryFrom<&AdminDescContent> for protos::AdminDescContent {
    type Error = BuckyError;

    fn try_from(value: &AdminDescContent) -> BuckyResult<Self> {
        let mut ret = protos::AdminDescContent::new();

        match value.cmd {
            AdminCommand::GlobalStateAccessMode(ref data) => {
                let data = data.try_into()?;
                ret.set_global_state_access_mode(data);
            }
        }

        ret.set_target(value.target.to_vec().unwrap());

        Ok(ret)
    }
}

impl_default_protobuf_raw_codec!(AdminDescContent);
impl_empty_protobuf_raw_codec!(AdminBodyContent);

impl ObjectFormatAutoWithSerde for AdminDescContent {}
impl ObjectFormatAutoWithSerde for AdminBodyContent {}

pub trait AdminObj {
    fn create(owner: ObjectId, target: DeviceId, cmd: AdminCommand) -> Self;

    fn target(&self) -> &DeviceId;

    fn into_command(self) -> AdminCommand;
}

impl AdminObj for AdminObject {
    fn create(owner: ObjectId, target: DeviceId, cmd: AdminCommand) -> Self {
        let desc = AdminDescContent { target, cmd };

        let body = AdminBodyContent {};

        AdminObjectBuilder::new(desc, body).owner(owner).build()
    }

    fn target(&self) -> &DeviceId {
        &self.desc().content().target
    }

    fn into_command(self) -> AdminCommand {
        self.into_desc().into_content().cmd
    }
}


#[cfg(test)]
mod test {
    use cyfs_base::*;
    use crate::*;

    use std::str::FromStr;

    #[test]
    fn test_object() {
        let data = AdminGlobalStateAccessModeData {
            category: GlobalStateCategory::RootState,
            access_mode: GlobalStateAccessMode::Read,
        };

        let cmd = AdminCommand::GlobalStateAccessMode(data);

        let target = DeviceId::from_str("5aSixgLkHa2NR4vSKJLYLPo5Av6CY3RJeFJegtF5iR1g").unwrap();
        let owner = PeopleId::default();
        let obj = AdminObject::create(owner.into(), target.clone(), cmd.clone());
        let buf = obj.to_vec().unwrap();
        println!("{}", hex::encode(&buf));

        let obj = AdminObject::clone_from_slice(&buf).unwrap();
        assert_eq!(*obj.target(), target);
        let c_cmd = obj.into_command();
        assert_eq!(c_cmd, cmd);
    }
}