1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 mod asset;
17 mod connect;
18 mod database;
19 mod error;
20 mod ffi;
21 mod function;
22
23 use std::collections::HashMap;
24
25 pub(crate) use asset::AssetLoader;
26 pub use asset::{CloudAsset, CloudAssets};
27 pub(crate) use connect::{
28 AppInfo, CloudData, Connect, Database, Databases, Field, FieldRaw, FieldType, Fields,
29 OrderTable, Schema, SchemaOrderTables, ServiceInfo, UnsubscriptionInfo, ValueBuckets,
30 };
31 pub use connect::{AssetStatus, ValueBucket};
32 pub(crate) use database::DatabaseStub;
33 /// Error message at the ipc.
34 pub use error::Error;
35 pub(crate) use error::Errors;
36 use ipc::parcel::{Deserialize, MsgParcel, Serialize};
37 use ipc::IpcResult;
38
string_hash_map_raw_write<V: Serialize>( msg_parcel: &mut MsgParcel, hash_map: &HashMap<String, V>, ) -> IpcResult<()>39 pub(crate) fn string_hash_map_raw_write<V: Serialize>(
40 msg_parcel: &mut MsgParcel,
41 hash_map: &HashMap<String, V>,
42 ) -> IpcResult<()> {
43 msg_parcel.write(&(hash_map.len() as i32))?;
44 let mut keys: Vec<&String> = hash_map.keys().collect();
45 keys.sort();
46 for key in keys {
47 msg_parcel.write_string16(&String::from(key))?;
48 msg_parcel.write(&hash_map[key])?;
49 }
50 Ok(())
51 }
52
string_hash_map_raw_read<V: Deserialize>( msg_parcel: &mut MsgParcel, ) -> IpcResult<HashMap<String, V>>53 pub(crate) fn string_hash_map_raw_read<V: Deserialize>(
54 msg_parcel: &mut MsgParcel,
55 ) -> IpcResult<HashMap<String, V>> {
56 let length = msg_parcel.read::<i32>()?;
57 let mut hash_map = HashMap::new();
58 for _ in 0..length {
59 let key = msg_parcel.read_string16()?;
60 let value = msg_parcel.read::<V>()?;
61 hash_map.insert(key, value);
62 }
63 Ok(hash_map)
64 }
65
vec_raw_write<T: Serialize>( msg_parcel: &mut MsgParcel, vector: &Vec<T>, ) -> IpcResult<()>66 pub(crate) fn vec_raw_write<T: Serialize>(
67 msg_parcel: &mut MsgParcel,
68 vector: &Vec<T>,
69 ) -> IpcResult<()> {
70 msg_parcel.write(&(vector.len() as i32))?;
71 for value in vector {
72 msg_parcel.write(value)?;
73 }
74 Ok(())
75 }
76
vec_raw_read<T: Deserialize>(msg_parcel: &mut MsgParcel) -> IpcResult<Vec<T>>77 pub(crate) fn vec_raw_read<T: Deserialize>(msg_parcel: &mut MsgParcel) -> IpcResult<Vec<T>> {
78 let length = msg_parcel.read::<i32>()? as usize;
79 let mut vector = Vec::with_capacity(length);
80 for _ in 0..length {
81 let value = msg_parcel.read::<T>()?;
82 vector.push(value);
83 }
84 Ok(vector)
85 }
86
87 #[cfg(test)]
88 mod test {
89 use ipc::parcel::MsgParcel;
90
91 use crate::ipc_conn::{vec_raw_read, vec_raw_write};
92
93 /// UT test for vec_raw_write.
94 ///
95 /// # Title
96 /// ut_vec_raw_write
97 ///
98 /// # Brief
99 /// 1. Create a `Vec` and `MsgParcel` struct.
100 /// 2. Write the data in the `Vec` to the `MsgParcel`.
101 /// 3. Read the data and check if it is correct.
102 #[test]
ut_vec_raw_write()103 fn ut_vec_raw_write() {
104 let vector = vec![
105 String::from("value1"),
106 String::from("value2"),
107 String::from("value3"),
108 ];
109
110 let mut parcel = MsgParcel::new();
111 assert!(vec_raw_write(&mut parcel, &vector).is_ok());
112
113 assert_eq!(parcel.read::<i32>().unwrap(), 3);
114 assert_eq!(parcel.read::<String>().unwrap(), String::from("value1"));
115 assert_eq!(parcel.read::<String>().unwrap(), String::from("value2"));
116 assert_eq!(parcel.read::<String>().unwrap(), String::from("value3"));
117 }
118
119 /// UT test for vec_raw_read.
120 ///
121 /// # Title
122 /// ut_vec_raw_read
123 ///
124 /// # Brief
125 /// 1. Create a `Vec` and `MsgParcel` struct.
126 /// 2. Write the data in the `Vec` to the `MsgParcel`.
127 /// 3. Read the data in the `MsgParcel` to the `Vec`.
128 /// 4. Check if it is correct.
129 #[test]
ut_vec_raw_read()130 fn ut_vec_raw_read() {
131 let vector = vec![
132 String::from("value1"),
133 String::from("value2"),
134 String::from("value3"),
135 ];
136
137 let mut parcel = MsgParcel::new();
138 assert!(vec_raw_write(&mut parcel, &vector).is_ok());
139
140 let result = vec_raw_read::<String>(&mut parcel).unwrap();
141 assert_eq!(vector, result);
142 }
143 }
144