re_protos/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//! This crate contains generated types for the remote store gRPC service API.
//! Generation is done using the `re_protos_builder` crate.
//!
//! We want clear separation between 'internal' types and gRPC types and don't want
//! to use gRPC types in the rerun viewer codebase. That's why we implement all the
//! necessary conversion code (in the form of `From` and `TryFrom` traits) in this crate.

pub mod external {
    pub use prost;
}

// This extra module is needed, because of how imports from different packages are resolved.
// For example, `rerun.remote_store.v1alpha1.EncoderVersion` is resolved to `super::super::remote_store::v1alpha1::EncoderVersion`.
// We need an extra module in the path to `common` to make that work.
// Additionally, the `common` module itself has to exist with a `v1alpha1` module inside of it,
// which is the reason for the `common`, `log_msg`, `remote_store`, etc. modules below.

// Note: Be careful with `#[path]` attributes: https://github.com/rust-lang/rust/issues/35016
mod v1alpha1 {
    // Note: `allow(clippy::all)` does NOT allow all lints
    #![allow(clippy::all, clippy::pedantic, clippy::nursery)]

    #[path = "./rerun.catalog.v1alpha1.rs"]
    pub mod rerun_catalog_v1alpha1;

    #[path = "./rerun.catalog.v1alpha1.ext.rs"]
    pub mod rerun_catalog_v1alpha1_ext;

    #[path = "./rerun.common.v1alpha1.rs"]
    pub mod rerun_common_v1alpha1;

    #[path = "./rerun.common.v1alpha1.ext.rs"]
    pub mod rerun_common_v1alpha1_ext;

    #[path = "./rerun.log_msg.v1alpha1.rs"]
    pub mod rerun_log_msg_v1alpha1;

    #[path = "./rerun.sdk_comms.v1alpha1.rs"]
    pub mod rerun_sdk_comms_v1alpha1;

    #[path = "./rerun.manifest_registry.v1alpha1.rs"]
    pub mod rerun_manifest_registry_v1alpha1;

    #[path = "./rerun.manifest_registry.v1alpha1.ext.rs"]
    pub mod rerun_manifest_registry_v1alpha1_ext;

    #[path = "./rerun.frontend.v1alpha1.rs"]
    pub mod rerun_frontend_v1alpha1;

    #[path = "./rerun.redap_tasks.v1alpha1.rs"]
    pub mod rerun_redap_tasks_v1alpha1;
}

pub mod common {
    pub mod v1alpha1 {
        pub use crate::v1alpha1::rerun_common_v1alpha1::*;
        pub mod ext {
            pub use crate::v1alpha1::rerun_common_v1alpha1_ext::*;
        }
    }
}

pub mod log_msg {
    pub mod v1alpha1 {
        pub use crate::v1alpha1::rerun_log_msg_v1alpha1::*;
    }
}

pub mod manifest_registry {
    #[rustfmt::skip] // keep these constants single line for easy sorting
    pub mod v1alpha1 {
        pub use crate::v1alpha1::rerun_manifest_registry_v1alpha1::*;
        pub mod ext {
            pub use crate::v1alpha1::rerun_manifest_registry_v1alpha1_ext::*;
        }

        /// `DatasetManifest` mandatory field names. All mandatory metadata fields are prefixed
        /// with "rerun_" to avoid conflicts with user-defined fields.
        pub const DATASET_MANIFEST_ID_FIELD_NAME: &str = "rerun_partition_id";
        pub const DATASET_MANIFEST_PARTITION_MANIFEST_UPDATED_AT_FIELD_NAME: &str = "rerun_partition_manifest_updated_at";
        pub const DATASET_MANIFEST_PARTITION_MANIFEST_URL_FIELD_NAME: &str = "rerun_partition_manifest_url";
        pub const DATASET_MANIFEST_RECORDING_TYPE_FIELD_NAME: &str = "rerun_partition_type";
        pub const DATASET_MANIFEST_REGISTRATION_TIME_FIELD_NAME: &str = "rerun_registration_time";
        pub const DATASET_MANIFEST_START_TIME_FIELD_NAME: &str = "rerun_start_time";
        pub const DATASET_MANIFEST_STORAGE_URL_FIELD_NAME: &str = "rerun_storage_url";
    }
}

pub mod catalog {
    pub mod v1alpha1 {
        pub use crate::v1alpha1::rerun_catalog_v1alpha1::*;
        pub mod ext {
            pub use crate::v1alpha1::rerun_catalog_v1alpha1_ext::*;
        }
    }
}

pub mod frontend {
    pub mod v1alpha1 {
        pub use crate::v1alpha1::rerun_frontend_v1alpha1::*;
    }
}

pub mod sdk_comms {
    pub mod v1alpha1 {
        pub use crate::v1alpha1::rerun_sdk_comms_v1alpha1::*;
    }
}

pub mod redap_tasks {
    pub mod v1alpha1 {
        pub use crate::v1alpha1::rerun_redap_tasks_v1alpha1::*;
    }
}

// ---

#[derive(Debug, thiserror::Error)]
pub enum TypeConversionError {
    #[error("missing required field: {package_name}.{type_name}.{field_name}")]
    MissingField {
        package_name: &'static str,
        type_name: &'static str,
        field_name: &'static str,
    },

    #[error("invalid value for field {package_name}.{type_name}.{field_name}: {reason}")]
    InvalidField {
        package_name: &'static str,
        type_name: &'static str,
        field_name: &'static str,
        reason: String,
    },

    #[error("failed to parse timestamp: {0}")]
    InvalidTime(#[from] jiff::Error),

    #[error("failed to decode: {0}")]
    DecodeError(#[from] prost::DecodeError),

    #[error("failed to encode: {0}")]
    EncodeError(#[from] prost::EncodeError),

    #[error("failed to convert arrow data: {0}")]
    ArrowError(#[from] arrow::error::ArrowError),

    #[error("{0}")]
    UnknownEnumValue(#[from] prost::UnknownEnumValue),

    #[error("could not parse url: {0}")]
    UrlParseError(#[from] url::ParseError),
}

impl TypeConversionError {
    #[inline]
    pub fn missing_field<T: prost::Name>(field_name: &'static str) -> Self {
        Self::MissingField {
            package_name: T::PACKAGE,
            type_name: T::NAME,
            field_name,
        }
    }

    #[allow(clippy::needless_pass_by_value)] // false-positive
    #[inline]
    pub fn invalid_field<T: prost::Name>(field_name: &'static str, reason: &impl ToString) -> Self {
        Self::InvalidField {
            package_name: T::PACKAGE,
            type_name: T::NAME,
            field_name,
            reason: reason.to_string(),
        }
    }
}

impl From<TypeConversionError> for tonic::Status {
    #[inline]
    fn from(value: TypeConversionError) -> Self {
        Self::invalid_argument(value.to_string())
    }
}

#[cfg(feature = "py")]
impl From<TypeConversionError> for pyo3::PyErr {
    #[inline]
    fn from(value: TypeConversionError) -> Self {
        pyo3::exceptions::PyValueError::new_err(value.to_string())
    }
}

#[macro_export]
macro_rules! missing_field {
    ($type:ty, $field:expr $(,)?) => {
        $crate::TypeConversionError::missing_field::<$type>($field)
    };
}

#[macro_export]
macro_rules! invalid_field {
    ($type:ty, $field:expr, $reason:expr $(,)?) => {
        $crate::TypeConversionError::invalid_field::<$type>($field, &$reason)
    };
}

// ---

// TODO(cmc): move this somewhere else
mod sizes {
    use re_byte_size::SizeBytes;

    impl SizeBytes for crate::log_msg::v1alpha1::LogMsg {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { msg } = self;

            match msg {
                Some(msg) => msg.heap_size_bytes(),
                None => 0,
            }
        }
    }

    impl SizeBytes for crate::log_msg::v1alpha1::log_msg::Msg {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            match self {
                Self::SetStoreInfo(set_store_info) => set_store_info.heap_size_bytes(),
                Self::ArrowMsg(arrow_msg) => arrow_msg.heap_size_bytes(),
                Self::BlueprintActivationCommand(blueprint_activation_command) => {
                    blueprint_activation_command.heap_size_bytes()
                }
            }
        }
    }

    impl SizeBytes for crate::log_msg::v1alpha1::SetStoreInfo {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { row_id, info } = self;

            row_id.heap_size_bytes() + info.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::common::v1alpha1::Tuid {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { inc, time_ns } = self;

            inc.heap_size_bytes() + time_ns.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::log_msg::v1alpha1::StoreInfo {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self {
                application_id,
                store_id,
                store_source,
                store_version,
            } = self;

            application_id.heap_size_bytes()
                + store_id.heap_size_bytes()
                + store_source.heap_size_bytes()
                + store_version.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::common::v1alpha1::ApplicationId {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { id } = self;

            id.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::common::v1alpha1::StoreId {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { kind, id } = self;

            kind.heap_size_bytes() + id.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::common::v1alpha1::TableId {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { id } = self;

            id.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::log_msg::v1alpha1::StoreSource {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { kind, extra } = self;

            kind.heap_size_bytes() + extra.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::log_msg::v1alpha1::StoreSourceExtra {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { payload } = self;

            payload.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::log_msg::v1alpha1::StoreVersion {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self { crate_version_bits } = self;

            crate_version_bits.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::log_msg::v1alpha1::ArrowMsg {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self {
                store_id,
                compression,
                uncompressed_size,
                encoding,
                payload,
            } = self;

            store_id.heap_size_bytes()
                + compression.heap_size_bytes()
                + uncompressed_size.heap_size_bytes()
                + encoding.heap_size_bytes()
                + payload.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::log_msg::v1alpha1::BlueprintActivationCommand {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self {
                blueprint_id,
                make_active,
                make_default,
            } = self;

            blueprint_id.heap_size_bytes()
                + make_active.heap_size_bytes()
                + make_default.heap_size_bytes()
        }
    }

    impl SizeBytes for crate::common::v1alpha1::DataframePart {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self {
                encoder_version,
                payload,
            } = self;

            encoder_version.heap_size_bytes() + payload.heap_size_bytes()
        }
    }
}