cyfs_core/app/
app_status.rs

1use crate::coreobj::CoreObjectType;
2use crate::DecAppId;
3use cyfs_base::*;
4use serde::Serialize;
5
6#[derive(Clone, Debug, ProtobufEncode, ProtobufDecode, ProtobufTransform, Serialize)]
7#[cyfs_protobuf_type(crate::codec::protos::AppStatusDescContent)]
8pub struct AppStatusDescContent {
9    id: DecAppId,
10}
11impl DescContent for AppStatusDescContent {
12    fn obj_type() -> u16 {
13        CoreObjectType::AppStatus as u16
14    }
15
16    fn format(&self) -> u8 {
17        OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
18    }
19
20    type OwnerType = Option<ObjectId>;
21    type AreaType = SubDescNone;
22    type AuthorType = SubDescNone;
23    type PublicKeyType = SubDescNone;
24}
25
26#[derive(Clone, Debug, ProtobufEncode, ProtobufDecode, ProtobufTransform, Serialize)]
27#[cyfs_protobuf_type(crate::codec::protos::AppStatusContent)]
28pub struct AppStatusContent {
29    version: String,
30    status: u8,
31}
32
33impl BodyContent for AppStatusContent {
34    fn format(&self) -> u8 {
35        OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
36    }
37}
38
39type AppStatusType = NamedObjType<AppStatusDescContent, AppStatusContent>;
40type AppStatusBuilder = NamedObjectBuilder<AppStatusDescContent, AppStatusContent>;
41type AppStatusDesc = NamedObjectDesc<AppStatusDescContent>;
42
43pub type AppStatusId = NamedObjectId<AppStatusType>;
44pub type AppStatus = NamedObjectBase<AppStatusType>;
45
46pub trait AppStatusObj {
47    fn create(owner: ObjectId, id: DecAppId, version: String, status: bool) -> Self;
48    fn app_id(&self) -> &DecAppId;
49    fn version(&self) -> &str;
50    fn status(&self) -> bool;
51    fn set_version(&mut self, version: String);
52    fn set_status(&mut self, status: bool);
53}
54
55impl AppStatusObj for AppStatus {
56    fn create(owner: ObjectId, id: DecAppId, version: String, status: bool) -> Self {
57        let body = AppStatusContent {
58            version,
59            status: if status { 1 } else { 0 },
60        };
61        let desc = AppStatusDescContent { id };
62        AppStatusBuilder::new(desc, body)
63            .owner(owner)
64            .no_create_time()
65            .build()
66    }
67
68    fn app_id(&self) -> &DecAppId {
69        &self.desc().content().id
70    }
71
72    fn version(&self) -> &str {
73        &self.body_expect("").content().version
74    }
75
76    fn status(&self) -> bool {
77        self.body_expect("").content().status == 1
78    }
79
80    fn set_version(&mut self, version: String) {
81        self.body_mut_expect("").content_mut().version = version;
82        self.body_mut_expect("")
83            .increase_update_time(bucky_time_now());
84    }
85
86    fn set_status(&mut self, status: bool) {
87        self.body_mut_expect("").content_mut().status = if status { 1 } else { 0 };
88        self.body_mut_expect("")
89            .increase_update_time(bucky_time_now());
90    }
91}
92
93#[cfg(test)]
94mod test {
95    use crate::*;
96    use cyfs_base::*;
97
98    use std::convert::TryFrom;
99    use std::str::FromStr;
100
101    #[test]
102    fn test() {
103        let owner = ObjectId::from_str("5aSixgLtjoYcAFH9isc6KCqDgKfTJ8jpgASAoiRz5NLk").unwrap();
104        println!("will calc dec_app_id...");
105        let dec_app_id = DecApp::generate_id(owner, "test-dec-app");
106        println!("dec_app_id: {}", dec_app_id);
107
108        let version = "1.0.0.1";
109
110        let dec_id = DecAppId::try_from(dec_app_id.clone()).unwrap();
111        let app_status = AppStatus::create(owner.clone(), dec_id.clone(), version.to_owned(), true);
112
113        let id = app_status.desc().calculate_id();
114        println!("app_status_id: {}", id);
115
116        let buf = app_status.to_vec().unwrap();
117        let app_status2 = AppStatus::clone_from_slice(&buf).unwrap();
118        assert_eq!(app_status2.app_id(), &dec_id);
119        assert_eq!(app_status2.version(), version);
120        assert_eq!(app_status2.status(), true);
121        assert_eq!(app_status2.desc().calculate_id(), id);
122
123        let path = cyfs_util::get_app_data_dir("tests");
124        std::fs::create_dir_all(&path).unwrap();
125        let name = path.join("app_status.desc");
126        std::fs::write(&name, buf).unwrap();
127    }
128}