1mod aggregate;
2mod filter;
3
4pub use self::aggregate::*;
5pub use self::filter::*;
6
7use crate::dto::identity::Identity;
8use crate::Items;
9use crate::UpsertOptions;
10use crate::{CogniteExternalId, CogniteId, EqIdentity, IntoPatch, IntoPatchItem, UpdateList};
11use crate::{Patch, UpdateMap, UpdateSet, UpdateSetNull};
12use serde::{Deserialize, Serialize};
13use serde_with::skip_serializing_none;
14use std::collections::HashMap;
15
16use super::common::GeoLocation;
17
18#[skip_serializing_none]
19#[derive(Serialize, Deserialize, Debug, Clone)]
20#[serde(rename_all = "camelCase")]
21pub struct AssetAggregate {
23 pub child_count: Option<i32>,
25 pub depth: Option<i32>,
27 pub path: Option<Vec<CogniteId>>,
29}
30
31#[skip_serializing_none]
32#[derive(Serialize, Deserialize, Debug, Default, Clone)]
33#[serde(rename_all = "camelCase")]
34pub struct Asset {
36 pub id: i64,
38 pub name: String,
40 pub external_id: Option<String>,
42 pub parent_id: Option<i64>,
44 pub parent_external_id: Option<String>,
46 pub description: Option<String>,
48 pub metadata: Option<HashMap<String, String>>,
52 pub source: Option<String>,
54 pub created_time: i64,
56 pub last_updated_time: i64,
58 pub root_id: Option<i64>,
60 pub aggregates: Option<AssetAggregate>,
62 pub data_set_id: Option<i64>,
64 pub labels: Option<Vec<CogniteExternalId>>,
66 pub geo_location: Option<GeoLocation>,
68}
69
70impl Asset {
71 pub fn new(
82 name: &str,
83 description: &str,
84 external_id: Option<String>,
85 parent_id: Option<i64>,
86 metadata: Option<HashMap<String, String>>,
87 source: Option<String>,
88 ) -> Asset {
89 Asset {
90 name: String::from(name),
91 id: 0,
92 external_id,
93 parent_id,
94 description: Some(String::from(description)),
95 metadata,
96 source,
97 created_time: 0,
98 last_updated_time: 0,
99 root_id: None,
100 aggregates: None,
101 data_set_id: None,
102 parent_external_id: None,
103 labels: None,
104 geo_location: None,
105 }
106 }
107}
108
109#[skip_serializing_none]
110#[derive(Serialize, Deserialize, Debug, Default, Clone)]
111#[serde(rename_all = "camelCase")]
112pub struct AddAsset {
114 pub name: String,
116 pub external_id: Option<String>,
118 pub parent_id: Option<i64>,
121 pub description: Option<String>,
123 pub metadata: Option<HashMap<String, String>>,
127 pub source: Option<String>,
129 pub parent_external_id: Option<String>,
138 pub data_set_id: Option<i64>,
140 pub labels: Option<Vec<CogniteExternalId>>,
142 pub geo_location: Option<GeoLocation>,
144}
145
146impl From<Asset> for AddAsset {
147 fn from(asset: Asset) -> AddAsset {
148 AddAsset {
149 name: asset.name,
150 external_id: asset.external_id,
151 parent_id: asset.parent_id,
152 description: asset.description,
153 metadata: asset.metadata,
154 source: asset.source,
155 parent_external_id: if asset.parent_id.is_none() {
156 asset.parent_external_id
157 } else {
158 None
159 },
160 data_set_id: asset.data_set_id,
161 labels: asset.labels,
162 geo_location: asset.geo_location,
163 }
164 }
165}
166
167impl EqIdentity for AddAsset {
168 fn eq(&self, id: &Identity) -> bool {
169 match id {
170 Identity::Id { id: _ } => false,
171 Identity::ExternalId { external_id } => self.external_id.as_ref() == Some(external_id),
172 }
173 }
174}
175
176#[skip_serializing_none]
177#[derive(Serialize, Deserialize, Debug, Default, Clone)]
178#[serde(rename_all = "camelCase")]
179pub struct PatchAsset {
181 pub external_id: Option<UpdateSetNull<String>>,
183 pub name: Option<UpdateSet<String>>,
185 pub description: Option<UpdateSetNull<String>>,
187 pub data_set_id: Option<UpdateSetNull<i64>>,
189 pub metadata: Option<UpdateMap<String, String>>,
193 pub source: Option<UpdateSetNull<String>>,
195 pub parent_id: Option<UpdateSet<i64>>,
198 pub parent_external_id: Option<UpdateSet<String>>,
207 pub labels: Option<UpdateList<CogniteExternalId, CogniteExternalId>>,
209 pub geo_location: Option<UpdateSetNull<GeoLocation>>,
211}
212
213impl IntoPatch<Patch<PatchAsset>> for Asset {
214 fn patch(self, options: &UpsertOptions) -> Patch<PatchAsset> {
215 Patch::<PatchAsset> {
216 id: to_idt!(self),
217 update: PatchAsset {
218 external_id: self.external_id.patch(options),
219 name: self.name.patch(options),
220 description: self.description.patch(options),
221 data_set_id: self.data_set_id.patch(options),
222 metadata: self.metadata.patch(options),
223 source: self.source.patch(options),
224 parent_id: self.parent_id.and_then(|p| p.patch(options)),
225 parent_external_id: None,
226 labels: self.labels.patch(options),
227 geo_location: self.geo_location.patch(options),
228 },
229 }
230 }
231}
232
233impl IntoPatch<PatchAsset> for AddAsset {
234 fn patch(self, options: &UpsertOptions) -> PatchAsset {
235 let mut parent_id = None;
236 let mut parent_external_id = None;
237 if let Some(p_id) = self.parent_id {
238 parent_id = p_id.patch(options);
239 } else if let Some(p_extid) = self.parent_external_id {
240 parent_external_id = p_extid.patch(options);
241 }
242 PatchAsset {
243 external_id: self.external_id.patch(options),
244 name: self.name.patch(options),
245 description: self.description.patch(options),
246 data_set_id: self.data_set_id.patch(options),
247 metadata: self.metadata.patch(options),
248 source: self.source.patch(options),
249 parent_id,
250 parent_external_id,
251 labels: self.labels.patch(options),
252 geo_location: self.geo_location.patch(options),
253 }
254 }
255}
256
257impl From<Asset> for Patch<PatchAsset> {
258 fn from(value: Asset) -> Self {
259 IntoPatch::<Patch<PatchAsset>>::patch(value, &Default::default())
260 }
261}
262
263#[derive(Serialize, Deserialize, Debug, Default)]
264#[serde(rename_all = "camelCase")]
265pub struct RetrieveAssetsRequestData {
267 pub ignore_unknown_ids: bool,
269 pub aggregated_properties: Option<Vec<AssetAggregatedProperty>>,
271}
272
273pub type RetrieveAssetsRequest<T> = Items<T, RetrieveAssetsRequestData>;
275
276#[derive(Serialize, Deserialize, Debug, Default)]
277#[serde(rename_all = "camelCase")]
278pub struct DeleteAssetsRequestData {
280 pub ignore_unknown_ids: bool,
282 pub recursive: bool,
284}
285
286pub type DeleteAssetsRequest<R> = Items<R, DeleteAssetsRequestData>;