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 #ifndef CLOUD_EXTENSION_BASIC_RUST_TYPES_H
17 #define CLOUD_EXTENSION_BASIC_RUST_TYPES_H
18 
19 #ifndef CLOUD_EXTENSION_ERROR_H
20 #include "error.h"
21 #endif
22 
23 #include <stddef.h>
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 enum OhCloudExtRustType {
32     VALUETYPE_NULL,
33     VALUETYPE_U32,
34     VALUETYPE_I32,
35     VALUETYPE_STRING,
36     VALUETYPE_VALUE,
37     VALUETYPE_VALUE_BUCKET,
38     VALUETYPE_DATABASE,
39     VALUETYPE_TABLE,
40     VALUETYPE_FIELD,
41     VALUETYPE_RELATION,
42     VALUETYPE_RELATION_SET,
43     VALUETYPE_CLOUD_ASSET,
44     VALUETYPE_APP_INFO,
45     VALUETYPE_VEC_U32,
46     VALUETYPE_VEC_STRING,
47     VALUETYPE_VEC_DATABASE,
48     VALUETYPE_HASHMAP_VALUE,
49 };
50 
51 /**
52 * @brief       Type declaration of Rust struct OhCloudExtVector.
53 * @attention   The memory is managed by Rust. Therefore, to memory leaks, users should call `OhCloudExtVectorFree`
54 *              to release the memory it occupies.
55 */
56 typedef struct {
57     const size_t id;
58 } OhCloudExtVector;
59 
60 /**
61  * @brief       Create a OhCloudExtVector enum according to OhCloudExtRustType.
62  * @attention   The memory is managed by Rust. Therefore, to memory leaks, users should call `OhCloudExtVectorFree`
63  *              to release the memory it occupies.
64  */
65 OhCloudExtVector *OhCloudExtVectorNew(OhCloudExtRustType typ);
66 
67 /**
68  * @breif       Get OhCloudExtRustType of the OhCloudExtVector pointer.
69  */
70 OhCloudExtRustType OhCloudExtVectorGetValueTyp(OhCloudExtVector *src);
71 
72 /**
73  * @breif       Push value into the OhCloudExtVector pointer.
74  * @attention   For Values pushed in, if they're created by other functions and allocated in Rust side,
75  *              pushing them will transfer the management to the OhCloudExtVector. Therefore, no more free is needed.
76  */
77 int OhCloudExtVectorPush(
78     OhCloudExtVector *src,
79     void *value,
80     unsigned int valueLen
81 );
82 
83 /**
84  * @breif       Get value from the OhCloudExtVector pointer.
85  * @attention   If value type is not raw C type, value will be copied so that the C side can get information stored
86  *              in Rust side. In this case, the pointer returned back should be freed by corresponding free function.
87  */
88 int OhCloudExtVectorGet(
89     const OhCloudExtVector *src,
90     unsigned int idx,
91     void **value,
92     unsigned int *valueLen
93 );
94 
95 /**
96  * @brief       Get length of a OhCloudExtVector pointer.
97  */
98 int OhCloudExtVectorGetLength(const OhCloudExtVector *src, unsigned int *len);
99 
100 /**
101  * @brief       Free a OhCloudExtVector pointer.
102  */
103 void OhCloudExtVectorFree(OhCloudExtVector *src);
104 
105 /**
106 * @brief       Type declaration of Rust struct OhCloudExtHashMap.
107 * @attention   The memory is managed by Rust. Therefore, to memory leaks, users should call `OhCloudExtHashMapFree`
108 *              to release the memory it occupies.
109 */
110 typedef struct {
111     const size_t id;
112 } OhCloudExtHashMap;
113 
114 /**
115  * @brief       Initialize a OhCloudExtHashMap enum by its OhCloudExtRustType. The key type is fixed to be String.
116  * @attention   The memory is managed by Rust. Therefore, to memory leaks, users should call `OhCloudExtHashMapFree`
117  *              to release the memory it occupies.
118  */
119 OhCloudExtHashMap *OhCloudExtHashMapNew(OhCloudExtRustType valueTyp);
120 
121 /**
122  * @brief       Get key type of a OhCloudExtHashMap pointer.
123  */
124 OhCloudExtRustType OhCloudExtHashMapGetKeyTyp(const OhCloudExtHashMap *src);
125 
126 /**
127  * @brief       Get value type of a OhCloudExtHashMap pointer.
128  */
129 OhCloudExtRustType OhCloudExtHashMapGetValueTyp(const OhCloudExtHashMap *src);
130 
131 /**
132  * @brief       Get length of a OhCloudExtHashMap pointer.
133  */
134 int OhCloudExtHashMapGetLength(const OhCloudExtHashMap *src, unsigned int *len);
135 
136 /**
137  * @brief       Insert key, value pairs into the OhCloudExtHashMap pointer.
138  * @attention   For Values pushed in, if they're created by other functions and allocated in Rust side,
139  *              pushing them will transfer the management to the OhCloudExtHashMap. Therefore, no more free is needed.
140  */
141 int OhCloudExtHashMapInsert(
142     OhCloudExtHashMap *src,
143     void *key,
144     unsigned int keyLen,
145     void *value,
146     unsigned int valueLen
147 );
148 
149 /**
150  * @brief       Get key, value pair from the OhCloudExtHashMap pointer.
151  * @param       src         [IN]
152  *              key         [OUT]  Vec<KeyType>, here fixed to be Vec<String> now
153  *              value       [OUT]  Vec<OhCloudExtRustType>
154  * @retval      SUCCESS
155  *              ERRNO_NULLPTR
156  * @attention   The vectors output should be freed by VectorFree.
157  */
158 int OhCloudExtHashMapIterGetKeyValuePair(
159     const OhCloudExtHashMap *src,
160     OhCloudExtVector **key,
161     OhCloudExtVector **value
162 );
163 
164 /**
165  * @brief       According to key, get value from the OhCloudExtHashMap pointer.
166  * @attention   If value type is not raw C type, value will be copied so that the C side can get information stored
167  *              in Rust side. In this case, the pointer returned back should be freed by corresponding free function.
168  */
169 int OhCloudExtHashMapGet(
170     const OhCloudExtHashMap *src,
171     const void *key,
172     unsigned int keyLen,
173     void **value,
174     unsigned int *valueLen
175 );
176 
177 /**
178  * @brief       Free a OhCloudExtHashMap pointer.
179  */
180 void OhCloudExtHashMapFree(OhCloudExtHashMap *src);
181 
182 
183 #ifdef __cplusplus
184 #if __cplusplus
185 }
186 #endif
187 #endif
188 
189 #endif // CLOUD_EXTENSION_BASIC_RUST_TYPES_H
190