cognite/dto/data_modeling/
common.rs

1use std::collections::HashMap;
2
3use derivative::Derivative;
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7use crate::{models::views::ViewReference, PropertyIdentifier};
8
9#[derive(Serialize, Deserialize, Clone, Debug)]
10#[serde(rename_all = "camelCase")]
11/// ID of a non-versioned resource in the data modeling API.
12pub struct ItemId {
13    /// Resource space.
14    pub space: String,
15    /// Resource external ID.
16    pub external_id: String,
17}
18
19#[skip_serializing_none]
20#[derive(Serialize, Deserialize, Clone, Debug)]
21#[serde(rename_all = "camelCase")]
22/// ID of an optionally versioned resource in the data modelling API.
23pub struct ItemIdOptionalVersion {
24    /// Resource space.
25    pub space: String,
26    /// Resource external ID.
27    pub external_id: String,
28    /// Resurce version.
29    pub version: Option<String>,
30}
31
32#[derive(Serialize, Deserialize, Clone, Debug)]
33#[serde(rename_all = "camelCase", tag = "type")]
34/// A reference to the source of a property.
35pub enum SourceReference {
36    /// A view with a specific version.
37    View(ViewReference),
38    /// A container.
39    Container(ItemId),
40}
41
42#[derive(Serialize, Deserialize, Clone, Debug)]
43#[serde(rename_all = "camelCase", tag = "type")]
44/// A reference to a property in a source view or container.
45pub struct SourcePropertyReference {
46    /// ID of the view or container.
47    pub source: SourceReference,
48    /// Identifier of the property in the source.
49    pub identifier: String,
50}
51
52#[derive(Serialize, Deserialize, Clone, Debug)]
53#[serde(rename_all = "camelCase")]
54/// ID of a space.
55pub struct SpaceId {
56    /// Space value
57    pub space: String,
58}
59
60#[derive(Serialize, Deserialize, Clone, Debug)]
61#[serde(rename_all = "camelCase", tag = "type")]
62/// A reference to a view, but tagged with `type: view`
63pub enum TaggedViewReference {
64    /// Tagged view variant
65    View(ViewReference),
66}
67
68impl From<ViewReference> for TaggedViewReference {
69    fn from(value: ViewReference) -> Self {
70        Self::View(value)
71    }
72}
73
74#[derive(Serialize, Deserialize, Clone, Debug)]
75#[serde(rename_all = "camelCase", tag = "type")]
76/// A reference to a container, but tagged with `type: container`
77pub enum TaggedContainerReference {
78    /// Tagged container reference.
79    Container(ItemId),
80}
81
82impl TaggedContainerReference {
83    /// Create a new tagged container reference.
84    pub fn new(space: impl Into<String>, external_id: impl Into<String>) -> Self {
85        Self::Container(ItemId {
86            space: space.into(),
87            external_id: external_id.into(),
88        })
89    }
90}
91
92impl From<ItemId> for TaggedContainerReference {
93    fn from(value: ItemId) -> Self {
94        Self::Container(value)
95    }
96}
97
98#[derive(Serialize, Deserialize, Clone, Debug, Default)]
99#[serde(rename_all = "camelCase")]
100/// Enum for whether a resource is used for nodes, edges, or both.
101pub enum UsedFor {
102    #[default]
103    /// Used for nodes.
104    Node,
105    /// Used for edges.
106    Edge,
107    /// Used for records.
108    Record,
109    /// Used for both nodes and edges.
110    All,
111}
112
113#[skip_serializing_none]
114#[derive(Default, Serialize, Deserialize, Derivative, Clone, Debug)]
115#[serde(rename_all = "camelCase")]
116/// Description of a text property.
117pub struct TextProperty {
118    #[derivative(Default(value = "false"))]
119    /// Whether this is a list or not.
120    pub list: Option<bool>,
121    /// Optional text collation.
122    pub collation: Option<String>,
123    /// Maximum allowed length of the list.
124    pub max_list_size: Option<i32>,
125    /// Maximum allowed size of each text entry, as utf-8 bytes.
126    pub max_text_size: Option<i32>,
127}
128
129#[skip_serializing_none]
130#[derive(Serialize, Deserialize, Derivative, Clone, Debug)]
131#[serde(rename_all = "camelCase")]
132/// Reference to a unit in the Cognite unit catalog.
133pub struct UnitReference {
134    /// The external ID of the unit in the Cognite unit catalog.
135    pub external_id: String,
136    /// The value of the unit in the source.
137    pub source_unit: Option<String>,
138}
139
140#[skip_serializing_none]
141#[derive(Default, Serialize, Deserialize, Derivative, Clone, Debug)]
142#[serde(rename_all = "camelCase")]
143/// Description of a primitive property
144pub struct PrimitiveProperty {
145    #[derivative(Default(value = "false"))]
146    /// Whether this is a list or not.
147    pub list: Option<bool>,
148    /// The unit of the data stored in this property.
149    /// Can only be assigned to types float32 or float64,
150    /// external ID needs to match with a unit in the Cognite unit catalog.
151    pub unit: Option<UnitReference>,
152    /// Maximum allowed length of the list.
153    pub max_list_size: Option<i32>,
154}
155
156#[skip_serializing_none]
157#[derive(Serialize, Deserialize, Default, Clone, Debug)]
158#[serde(rename_all = "camelCase")]
159/// Description of an enum value in an enum property.
160pub struct EnumValueDescription {
161    /// Name of the enum value.
162    pub name: Option<String>,
163    /// Enum value description.
164    pub description: Option<String>,
165}
166
167#[skip_serializing_none]
168#[derive(Serialize, Deserialize, Default, Clone, Debug)]
169#[serde(rename_all = "camelCase")]
170/// Description of an enum property.
171pub struct EnumProperty {
172    /// The value to use when the enum value is unknown.
173    pub unknown_value: Option<String>,
174    /// Map from enum value identifier to description.
175    pub values: HashMap<String, EnumValueDescription>,
176}
177
178#[skip_serializing_none]
179#[derive(Serialize, Deserialize, Derivative, Clone, Debug)]
180#[serde(rename_all = "camelCase")]
181/// Description of a property referencing a CDF resource.
182pub struct CDFExternalIdReference {
183    #[derivative(Default(value = "false"))]
184    /// Whether this is a list or not.
185    pub list: Option<bool>,
186    /// Maximum allowed length of the list.
187    pub max_list_size: Option<i32>,
188}
189
190#[derive(Serialize, Deserialize, Clone, Debug, Default)]
191#[serde(rename_all = "camelCase")]
192/// Direction to sort data modeling query results in.
193pub enum SortDirection {
194    #[default]
195    /// Sort ascending.
196    Ascending,
197    /// Sort descending.
198    Descending,
199}
200
201#[skip_serializing_none]
202#[derive(Serialize, Deserialize, Clone, Debug)]
203#[serde(rename_all = "camelCase")]
204/// Sort on a dynamic property
205pub struct PropertySort {
206    /// List of strings representing the property
207    pub property: Vec<String>,
208    /// Direction to sort.
209    pub direction: Option<SortDirection>,
210    /// Whether nulls are first or last.
211    pub nulls_first: Option<bool>,
212}
213
214impl PropertySort {
215    /// Create a new property sort object.
216    ///
217    /// # Arguments
218    ///
219    /// * `property` - Property to sort by
220    /// * `direction` - Direction to sort in.
221    /// * `nulls_first` - Whether to put nulls first or last.
222    pub fn new(
223        property: impl PropertyIdentifier,
224        direction: SortDirection,
225        nulls_first: bool,
226    ) -> Self {
227        Self {
228            property: property.into_identifier(),
229            direction: Some(direction),
230            nulls_first: Some(nulls_first),
231        }
232    }
233}