Skip to main content

mz_catalog/durable/objects/
serialization.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! This module is responsible for serializing catalog objects into Protobuf.
11
12use mz_ore::cast::CastFrom;
13use mz_proto::TryFromProtoError;
14use mz_repr::ColumnName;
15
16use crate::durable::objects::state_update::StateUpdateKindJson;
17use crate::durable::objects::{
18    AuditLogKey, ClusterIntrospectionSourceIndexKey, ClusterIntrospectionSourceIndexValue,
19    ClusterKey, ClusterReplicaKey, ClusterReplicaSizeKey, ClusterReplicaSizeValue,
20    ClusterReplicaValue, ClusterSystemConfigurationKey, ClusterSystemConfigurationValue,
21    ClusterValue, CommentKey, CommentValue, ConfigKey, ConfigValue, DatabaseKey, DatabaseValue,
22    DefaultPrivilegesKey, DefaultPrivilegesValue, GidMappingKey, GidMappingValue, IdAllocKey,
23    IdAllocValue, IndexColumn, IndexColumnsKey, IndexColumnsValue,
24    IntrospectionSourceIndexCatalogItemId, IntrospectionSourceIndexGlobalId, ItemColumn,
25    ItemColumnsKey, ItemColumnsValue, ItemKey, ItemValue, NetworkPolicyKey, NetworkPolicyValue,
26    QueryPolicyKey, QueryPolicyValue, ReplicaSystemConfigurationKey,
27    ReplicaSystemConfigurationValue, RoleKey, RoleValue, SchemaKey, SchemaValue,
28    ServerConfigurationKey, ServerConfigurationValue, SettingKey, SettingValue, SourceReference,
29    SourceReferencesKey, SourceReferencesValue, StorageCollectionMetadataKey,
30    StorageCollectionMetadataValue, SystemCatalogItemId, SystemGlobalId, SystemPrivilegesKey,
31    SystemPrivilegesValue, TxnWalShardValue, UnfinalizedShardKey,
32};
33use crate::durable::{
34    BurstState, ClusterConfig, ClusterVariant, ClusterVariantManaged, ReconfigurationState,
35    ReconfigurationStatus, ReconfigurationTarget, ReplicaConfig, ReplicaLocation,
36};
37
38use super::{RoleAuthKey, RoleAuthValue};
39
40mod audit_log;
41mod foreign;
42mod rust_type;
43
44pub use rust_type::{ProtoMapEntry, ProtoType, RustType};
45
46pub mod proto {
47    pub use mz_catalog_protos::objects::*;
48}
49
50impl From<proto::StateUpdateKind> for StateUpdateKindJson {
51    fn from(value: proto::StateUpdateKind) -> Self {
52        StateUpdateKindJson::from_serde(value)
53    }
54}
55
56impl TryFrom<StateUpdateKindJson> for proto::StateUpdateKind {
57    type Error = String;
58
59    fn try_from(value: StateUpdateKindJson) -> Result<Self, Self::Error> {
60        value.try_to_serde::<Self>().map_err(|err| err.to_string())
61    }
62}
63
64impl RustType<proto::ClusterConfig> for ClusterConfig {
65    fn into_proto(&self) -> proto::ClusterConfig {
66        proto::ClusterConfig {
67            variant: self.variant.into_proto(),
68            workload_class: self.workload_class.clone(),
69            query_policy: self.query_policy.into_proto(),
70        }
71    }
72
73    fn from_proto(proto: proto::ClusterConfig) -> Result<Self, TryFromProtoError> {
74        Ok(Self {
75            variant: proto.variant.into_rust()?,
76            workload_class: proto.workload_class,
77            query_policy: proto.query_policy.into_rust()?,
78        })
79    }
80}
81
82impl RustType<proto::ClusterVariant> for ClusterVariant {
83    fn into_proto(&self) -> proto::ClusterVariant {
84        match self {
85            ClusterVariant::Managed(ClusterVariantManaged {
86                size,
87                availability_zones,
88                logging,
89                arrangement_compression,
90                replication_factor,
91                optimizer_feature_overrides,
92                schedule,
93                auto_scaling_strategy,
94                reconfiguration,
95                burst,
96            }) => proto::ClusterVariant::Managed(proto::ManagedCluster {
97                size: size.to_string(),
98                availability_zones: availability_zones.clone(),
99                logging: logging.into_proto(),
100                arrangement_compression: *arrangement_compression,
101                replication_factor: *replication_factor,
102                optimizer_feature_overrides: optimizer_feature_overrides.into_proto(),
103                schedule: schedule.into_proto(),
104                auto_scaling_strategy: auto_scaling_strategy.into_proto(),
105                reconfiguration: reconfiguration.into_proto(),
106                burst: burst.into_proto(),
107            }),
108            ClusterVariant::Unmanaged => proto::ClusterVariant::Unmanaged,
109        }
110    }
111
112    fn from_proto(proto: proto::ClusterVariant) -> Result<Self, TryFromProtoError> {
113        match proto {
114            proto::ClusterVariant::Unmanaged => Ok(Self::Unmanaged),
115            proto::ClusterVariant::Managed(managed) => Ok(Self::Managed(ClusterVariantManaged {
116                size: managed.size,
117                availability_zones: managed.availability_zones,
118                logging: managed.logging.into_rust()?,
119                arrangement_compression: managed.arrangement_compression,
120                replication_factor: managed.replication_factor,
121                optimizer_feature_overrides: managed.optimizer_feature_overrides.into_rust()?,
122                schedule: managed.schedule.into_rust()?,
123                auto_scaling_strategy: managed.auto_scaling_strategy.into_rust()?,
124                reconfiguration: managed.reconfiguration.into_rust()?,
125                burst: managed.burst.into_rust()?,
126            })),
127        }
128    }
129}
130
131impl RustType<proto::ReconfigurationState> for ReconfigurationState {
132    fn into_proto(&self) -> proto::ReconfigurationState {
133        proto::ReconfigurationState {
134            target: self.target.into_proto(),
135            deadline: self.deadline.into(),
136            on_timeout: self.on_timeout.into_proto(),
137            status: self.status.into_proto(),
138        }
139    }
140
141    fn from_proto(proto: proto::ReconfigurationState) -> Result<Self, TryFromProtoError> {
142        Ok(Self {
143            target: proto.target.into_rust()?,
144            deadline: mz_repr::Timestamp::new(proto.deadline),
145            on_timeout: proto.on_timeout.into_rust()?,
146            status: proto.status.into_rust()?,
147        })
148    }
149}
150
151impl RustType<proto::ReconfigurationStatus> for ReconfigurationStatus {
152    fn into_proto(&self) -> proto::ReconfigurationStatus {
153        match self {
154            ReconfigurationStatus::InProgress => proto::ReconfigurationStatus::InProgress,
155            ReconfigurationStatus::Finalized => proto::ReconfigurationStatus::Finalized,
156            ReconfigurationStatus::TimedOut => proto::ReconfigurationStatus::TimedOut,
157            ReconfigurationStatus::Cancelled => proto::ReconfigurationStatus::Cancelled,
158            ReconfigurationStatus::ResourceExhausted => {
159                proto::ReconfigurationStatus::ResourceExhausted
160            }
161        }
162    }
163
164    fn from_proto(proto: proto::ReconfigurationStatus) -> Result<Self, TryFromProtoError> {
165        Ok(match proto {
166            proto::ReconfigurationStatus::InProgress => ReconfigurationStatus::InProgress,
167            proto::ReconfigurationStatus::Finalized => ReconfigurationStatus::Finalized,
168            proto::ReconfigurationStatus::TimedOut => ReconfigurationStatus::TimedOut,
169            proto::ReconfigurationStatus::Cancelled => ReconfigurationStatus::Cancelled,
170            proto::ReconfigurationStatus::ResourceExhausted => {
171                ReconfigurationStatus::ResourceExhausted
172            }
173        })
174    }
175}
176
177impl RustType<proto::ReconfigurationTarget> for ReconfigurationTarget {
178    fn into_proto(&self) -> proto::ReconfigurationTarget {
179        proto::ReconfigurationTarget {
180            size: self.size.clone(),
181            replication_factor: self.replication_factor,
182            availability_zones: self.availability_zones.clone(),
183            logging: self.logging.into_proto(),
184            arrangement_compression: self.arrangement_compression,
185        }
186    }
187
188    fn from_proto(proto: proto::ReconfigurationTarget) -> Result<Self, TryFromProtoError> {
189        Ok(Self {
190            size: proto.size,
191            replication_factor: proto.replication_factor,
192            availability_zones: proto.availability_zones,
193            logging: proto.logging.into_rust()?,
194            arrangement_compression: proto.arrangement_compression,
195        })
196    }
197}
198
199impl RustType<proto::BurstState> for BurstState {
200    fn into_proto(&self) -> proto::BurstState {
201        proto::BurstState {
202            burst_size: self.burst_size.clone(),
203            linger_duration: self.linger_duration.into_proto(),
204            steady_ready_at: self.steady_ready_at.map(Into::into),
205        }
206    }
207
208    fn from_proto(proto: proto::BurstState) -> Result<Self, TryFromProtoError> {
209        Ok(Self {
210            burst_size: proto.burst_size,
211            linger_duration: proto.linger_duration.into_rust()?,
212            steady_ready_at: proto.steady_ready_at.map(mz_repr::Timestamp::new),
213        })
214    }
215}
216
217impl RustType<proto::ReplicaConfig> for ReplicaConfig {
218    fn into_proto(&self) -> proto::ReplicaConfig {
219        proto::ReplicaConfig {
220            logging: self.logging.into_proto(),
221            location: self.location.into_proto(),
222            arrangement_compression: self.arrangement_compression,
223        }
224    }
225
226    fn from_proto(proto: proto::ReplicaConfig) -> Result<Self, TryFromProtoError> {
227        Ok(ReplicaConfig {
228            location: proto.location.into_rust()?,
229            logging: proto.logging.into_rust()?,
230            arrangement_compression: proto.arrangement_compression,
231        })
232    }
233}
234
235impl RustType<proto::ReplicaLocation> for ReplicaLocation {
236    fn into_proto(&self) -> proto::ReplicaLocation {
237        match self {
238            ReplicaLocation::Unmanaged {
239                storagectl_addrs,
240                computectl_addrs,
241            } => proto::ReplicaLocation::Unmanaged(proto::UnmanagedLocation {
242                storagectl_addrs: storagectl_addrs.clone(),
243                computectl_addrs: computectl_addrs.clone(),
244            }),
245            ReplicaLocation::Managed {
246                size,
247                availability_zones,
248                billed_as,
249                internal,
250                pending,
251            } => proto::ReplicaLocation::Managed(proto::ManagedLocation {
252                size: size.to_string(),
253                availability_zones: availability_zones.clone(),
254                billed_as: billed_as.clone(),
255                internal: *internal,
256                pending: *pending,
257            }),
258        }
259    }
260
261    fn from_proto(proto: proto::ReplicaLocation) -> Result<Self, TryFromProtoError> {
262        match proto {
263            proto::ReplicaLocation::Unmanaged(location) => Ok(ReplicaLocation::Unmanaged {
264                storagectl_addrs: location.storagectl_addrs,
265                computectl_addrs: location.computectl_addrs,
266            }),
267            proto::ReplicaLocation::Managed(location) => Ok(ReplicaLocation::Managed {
268                availability_zones: location.availability_zones,
269                billed_as: location.billed_as,
270                internal: location.internal,
271                size: location.size,
272                pending: location.pending,
273            }),
274        }
275    }
276}
277
278impl RustType<proto::SettingKey> for SettingKey {
279    fn into_proto(&self) -> proto::SettingKey {
280        proto::SettingKey {
281            name: self.name.to_string(),
282        }
283    }
284
285    fn from_proto(proto: proto::SettingKey) -> Result<Self, TryFromProtoError> {
286        Ok(SettingKey { name: proto.name })
287    }
288}
289
290impl RustType<proto::SettingValue> for SettingValue {
291    fn into_proto(&self) -> proto::SettingValue {
292        proto::SettingValue {
293            value: self.value.to_string(),
294        }
295    }
296
297    fn from_proto(proto: proto::SettingValue) -> Result<Self, TryFromProtoError> {
298        Ok(SettingValue { value: proto.value })
299    }
300}
301
302impl RustType<proto::IdAllocKey> for IdAllocKey {
303    fn into_proto(&self) -> proto::IdAllocKey {
304        proto::IdAllocKey {
305            name: self.name.to_string(),
306        }
307    }
308
309    fn from_proto(proto: proto::IdAllocKey) -> Result<Self, TryFromProtoError> {
310        Ok(IdAllocKey { name: proto.name })
311    }
312}
313
314impl RustType<proto::IdAllocValue> for IdAllocValue {
315    fn into_proto(&self) -> proto::IdAllocValue {
316        proto::IdAllocValue {
317            next_id: self.next_id,
318        }
319    }
320
321    fn from_proto(proto: proto::IdAllocValue) -> Result<Self, TryFromProtoError> {
322        Ok(IdAllocValue {
323            next_id: proto.next_id,
324        })
325    }
326}
327
328impl RustType<proto::GidMappingKey> for GidMappingKey {
329    fn into_proto(&self) -> proto::GidMappingKey {
330        proto::GidMappingKey {
331            schema_name: self.schema_name.to_string(),
332            object_type: self.object_type.into_proto(),
333            object_name: self.object_name.to_string(),
334        }
335    }
336
337    fn from_proto(proto: proto::GidMappingKey) -> Result<Self, TryFromProtoError> {
338        Ok(GidMappingKey {
339            schema_name: proto.schema_name,
340            object_type: proto.object_type.into_rust()?,
341            object_name: proto.object_name,
342        })
343    }
344}
345
346impl RustType<proto::GidMappingValue> for GidMappingValue {
347    fn into_proto(&self) -> proto::GidMappingValue {
348        proto::GidMappingValue {
349            catalog_id: self.catalog_id.into_proto(),
350            global_id: self.global_id.into_proto(),
351            fingerprint: self.fingerprint.to_string(),
352        }
353    }
354
355    fn from_proto(proto: proto::GidMappingValue) -> Result<Self, TryFromProtoError> {
356        Ok(GidMappingValue {
357            catalog_id: proto.catalog_id.into_rust()?,
358            global_id: proto.global_id.into_rust()?,
359            fingerprint: proto.fingerprint,
360        })
361    }
362}
363
364impl RustType<proto::ClusterKey> for ClusterKey {
365    fn into_proto(&self) -> proto::ClusterKey {
366        proto::ClusterKey {
367            id: self.id.into_proto(),
368        }
369    }
370
371    fn from_proto(proto: proto::ClusterKey) -> Result<Self, TryFromProtoError> {
372        Ok(ClusterKey {
373            id: proto.id.into_rust()?,
374        })
375    }
376}
377
378impl RustType<proto::ClusterValue> for ClusterValue {
379    fn into_proto(&self) -> proto::ClusterValue {
380        proto::ClusterValue {
381            name: self.name.to_string(),
382            config: self.config.into_proto(),
383            owner_id: self.owner_id.into_proto(),
384            privileges: self.privileges.into_proto(),
385        }
386    }
387
388    fn from_proto(proto: proto::ClusterValue) -> Result<Self, TryFromProtoError> {
389        Ok(ClusterValue {
390            name: proto.name,
391            config: proto.config.into_rust()?,
392            owner_id: proto.owner_id.into_rust()?,
393            privileges: proto.privileges.into_rust()?,
394        })
395    }
396}
397
398impl RustType<proto::ClusterIntrospectionSourceIndexKey> for ClusterIntrospectionSourceIndexKey {
399    fn into_proto(&self) -> proto::ClusterIntrospectionSourceIndexKey {
400        proto::ClusterIntrospectionSourceIndexKey {
401            cluster_id: self.cluster_id.into_proto(),
402            name: self.name.to_string(),
403        }
404    }
405
406    fn from_proto(
407        proto: proto::ClusterIntrospectionSourceIndexKey,
408    ) -> Result<Self, TryFromProtoError> {
409        Ok(ClusterIntrospectionSourceIndexKey {
410            cluster_id: proto.cluster_id.into_rust()?,
411            name: proto.name,
412        })
413    }
414}
415
416impl RustType<proto::ClusterIntrospectionSourceIndexValue>
417    for ClusterIntrospectionSourceIndexValue
418{
419    fn into_proto(&self) -> proto::ClusterIntrospectionSourceIndexValue {
420        proto::ClusterIntrospectionSourceIndexValue {
421            catalog_id: self.catalog_id.into_proto(),
422            global_id: self.global_id.into_proto(),
423            oid: self.oid,
424        }
425    }
426
427    fn from_proto(
428        proto: proto::ClusterIntrospectionSourceIndexValue,
429    ) -> Result<Self, TryFromProtoError> {
430        Ok(ClusterIntrospectionSourceIndexValue {
431            catalog_id: proto.catalog_id.into_rust()?,
432            global_id: proto.global_id.into_rust()?,
433            oid: proto.oid,
434        })
435    }
436}
437
438impl RustType<proto::ClusterReplicaKey> for ClusterReplicaKey {
439    fn into_proto(&self) -> proto::ClusterReplicaKey {
440        proto::ClusterReplicaKey {
441            id: self.id.into_proto(),
442        }
443    }
444
445    fn from_proto(proto: proto::ClusterReplicaKey) -> Result<Self, TryFromProtoError> {
446        Ok(ClusterReplicaKey {
447            id: proto.id.into_rust()?,
448        })
449    }
450}
451
452impl RustType<proto::ClusterReplicaValue> for ClusterReplicaValue {
453    fn into_proto(&self) -> proto::ClusterReplicaValue {
454        proto::ClusterReplicaValue {
455            cluster_id: self.cluster_id.into_proto(),
456            name: self.name.to_string(),
457            config: self.config.into_proto(),
458            owner_id: self.owner_id.into_proto(),
459        }
460    }
461
462    fn from_proto(proto: proto::ClusterReplicaValue) -> Result<Self, TryFromProtoError> {
463        Ok(ClusterReplicaValue {
464            cluster_id: proto.cluster_id.into_rust()?,
465            name: proto.name,
466            config: proto.config.into_rust()?,
467            owner_id: proto.owner_id.into_rust()?,
468        })
469    }
470}
471
472impl RustType<proto::DatabaseKey> for DatabaseKey {
473    fn into_proto(&self) -> proto::DatabaseKey {
474        proto::DatabaseKey {
475            id: self.id.into_proto(),
476        }
477    }
478
479    fn from_proto(proto: proto::DatabaseKey) -> Result<Self, TryFromProtoError> {
480        Ok(DatabaseKey {
481            id: proto.id.into_rust()?,
482        })
483    }
484}
485
486impl RustType<proto::DatabaseValue> for DatabaseValue {
487    fn into_proto(&self) -> proto::DatabaseValue {
488        proto::DatabaseValue {
489            name: self.name.clone(),
490            owner_id: self.owner_id.into_proto(),
491            privileges: self.privileges.into_proto(),
492            oid: self.oid,
493        }
494    }
495
496    fn from_proto(proto: proto::DatabaseValue) -> Result<Self, TryFromProtoError> {
497        Ok(DatabaseValue {
498            name: proto.name,
499            owner_id: proto.owner_id.into_rust()?,
500            privileges: proto.privileges.into_rust()?,
501            oid: proto.oid,
502        })
503    }
504}
505
506impl RustType<proto::SchemaKey> for SchemaKey {
507    fn into_proto(&self) -> proto::SchemaKey {
508        proto::SchemaKey {
509            id: self.id.into_proto(),
510        }
511    }
512
513    fn from_proto(proto: proto::SchemaKey) -> Result<Self, TryFromProtoError> {
514        Ok(SchemaKey {
515            id: proto.id.into_rust()?,
516        })
517    }
518}
519
520impl RustType<proto::SchemaValue> for SchemaValue {
521    fn into_proto(&self) -> proto::SchemaValue {
522        proto::SchemaValue {
523            name: self.name.clone(),
524            database_id: self.database_id.map(|id| id.into_proto()),
525            owner_id: self.owner_id.into_proto(),
526            privileges: self.privileges.into_proto(),
527            oid: self.oid,
528        }
529    }
530
531    fn from_proto(proto: proto::SchemaValue) -> Result<Self, TryFromProtoError> {
532        Ok(SchemaValue {
533            name: proto.name,
534            database_id: proto.database_id.into_rust()?,
535            owner_id: proto.owner_id.into_rust()?,
536            privileges: proto.privileges.into_rust()?,
537            oid: proto.oid,
538        })
539    }
540}
541
542impl RustType<proto::ItemKey> for ItemKey {
543    fn into_proto(&self) -> proto::ItemKey {
544        proto::ItemKey {
545            gid: self.id.into_proto(),
546        }
547    }
548
549    fn from_proto(proto: proto::ItemKey) -> Result<Self, TryFromProtoError> {
550        Ok(ItemKey {
551            id: proto.gid.into_rust()?,
552        })
553    }
554}
555
556impl RustType<proto::ItemValue> for ItemValue {
557    fn into_proto(&self) -> proto::ItemValue {
558        let definition = proto::CatalogItem::V1(proto::CatalogItemV1 {
559            create_sql: self.create_sql.clone(),
560        });
561        proto::ItemValue {
562            schema_id: self.schema_id.into_proto(),
563            name: self.name.to_string(),
564            definition,
565            owner_id: self.owner_id.into_proto(),
566            privileges: self.privileges.into_proto(),
567            oid: self.oid,
568            global_id: self.global_id.into_proto(),
569            extra_versions: self
570                .extra_versions
571                .iter()
572                .map(|(version, global_id)| proto::ItemVersion {
573                    global_id: global_id.into_proto(),
574                    version: version.into_proto(),
575                })
576                .collect(),
577            ephemeral_owner_session: self.ephemeral_owner_session,
578        }
579    }
580
581    fn from_proto(proto: proto::ItemValue) -> Result<Self, TryFromProtoError> {
582        let create_sql = match proto.definition {
583            proto::CatalogItem::V1(c) => c.create_sql,
584        };
585        let extra_versions = proto
586            .extra_versions
587            .into_iter()
588            .map(|item_version| {
589                let version = item_version.version.into_rust()?;
590                let global_id = item_version.global_id.into_rust()?;
591                Ok::<_, TryFromProtoError>((version, global_id))
592            })
593            .collect::<Result<_, _>>()?;
594        Ok(ItemValue {
595            schema_id: proto.schema_id.into_rust()?,
596            name: proto.name,
597            create_sql,
598            owner_id: proto.owner_id.into_rust()?,
599            privileges: proto.privileges.into_rust()?,
600            oid: proto.oid,
601            global_id: proto.global_id.into_rust()?,
602            extra_versions,
603            ephemeral_owner_session: proto.ephemeral_owner_session,
604        })
605    }
606}
607
608impl RustType<proto::ItemColumnsKey> for ItemColumnsKey {
609    fn into_proto(&self) -> proto::ItemColumnsKey {
610        proto::ItemColumnsKey {
611            id: self.id.into_proto(),
612        }
613    }
614
615    fn from_proto(proto: proto::ItemColumnsKey) -> Result<Self, TryFromProtoError> {
616        Ok(ItemColumnsKey {
617            id: proto.id.into_rust()?,
618        })
619    }
620}
621
622impl RustType<proto::ItemColumnsValue> for ItemColumnsValue {
623    fn into_proto(&self) -> proto::ItemColumnsValue {
624        proto::ItemColumnsValue {
625            columns: self.columns.into_proto(),
626        }
627    }
628
629    fn from_proto(proto: proto::ItemColumnsValue) -> Result<Self, TryFromProtoError> {
630        Ok(ItemColumnsValue {
631            columns: proto.columns.into_rust()?,
632        })
633    }
634}
635
636impl RustType<proto::ItemColumn> for ItemColumn {
637    fn into_proto(&self) -> proto::ItemColumn {
638        proto::ItemColumn {
639            name: self.name.to_string(),
640            nullable: self.nullable,
641            type_oid: self.type_oid,
642            type_mod: self.type_mod,
643            custom_type: self.custom_type.into_proto(),
644        }
645    }
646
647    fn from_proto(proto: proto::ItemColumn) -> Result<Self, TryFromProtoError> {
648        Ok(ItemColumn {
649            name: ColumnName::from(proto.name),
650            nullable: proto.nullable,
651            type_oid: proto.type_oid,
652            type_mod: proto.type_mod,
653            custom_type: proto.custom_type.into_rust()?,
654        })
655    }
656}
657
658impl RustType<proto::IndexColumnsKey> for IndexColumnsKey {
659    fn into_proto(&self) -> proto::IndexColumnsKey {
660        proto::IndexColumnsKey {
661            id: self.id.into_proto(),
662        }
663    }
664
665    fn from_proto(proto: proto::IndexColumnsKey) -> Result<Self, TryFromProtoError> {
666        Ok(IndexColumnsKey {
667            id: proto.id.into_rust()?,
668        })
669    }
670}
671
672impl RustType<proto::IndexColumnsValue> for IndexColumnsValue {
673    fn into_proto(&self) -> proto::IndexColumnsValue {
674        proto::IndexColumnsValue {
675            columns: self.columns.into_proto(),
676        }
677    }
678
679    fn from_proto(proto: proto::IndexColumnsValue) -> Result<Self, TryFromProtoError> {
680        Ok(IndexColumnsValue {
681            columns: proto.columns.into_rust()?,
682        })
683    }
684}
685
686impl RustType<proto::IndexColumn> for IndexColumn {
687    fn into_proto(&self) -> proto::IndexColumn {
688        proto::IndexColumn {
689            column: self.column.map(u64::cast_from),
690            nullable: self.nullable,
691        }
692    }
693
694    fn from_proto(proto: proto::IndexColumn) -> Result<Self, TryFromProtoError> {
695        Ok(IndexColumn {
696            column: proto.column.map(usize::cast_from),
697            nullable: proto.nullable,
698        })
699    }
700}
701
702impl RustType<proto::CommentKey> for CommentKey {
703    fn into_proto(&self) -> proto::CommentKey {
704        let sub_component = match &self.sub_component {
705            Some(pos) => Some(proto::CommentSubComponent::ColumnPos(CastFrom::cast_from(
706                *pos,
707            ))),
708            None => None,
709        };
710        proto::CommentKey {
711            object: self.object_id.into_proto(),
712            sub_component,
713        }
714    }
715
716    fn from_proto(proto: proto::CommentKey) -> Result<Self, TryFromProtoError> {
717        let sub_component = match proto.sub_component {
718            Some(proto::CommentSubComponent::ColumnPos(pos)) => Some(CastFrom::cast_from(pos)),
719            None => None,
720        };
721        Ok(CommentKey {
722            object_id: proto.object.into_rust()?,
723            sub_component,
724        })
725    }
726}
727
728impl RustType<proto::CommentValue> for CommentValue {
729    fn into_proto(&self) -> proto::CommentValue {
730        proto::CommentValue {
731            comment: self.comment.clone(),
732        }
733    }
734
735    fn from_proto(proto: proto::CommentValue) -> Result<Self, TryFromProtoError> {
736        Ok(CommentValue {
737            comment: proto.comment,
738        })
739    }
740}
741
742impl RustType<proto::RoleKey> for RoleKey {
743    fn into_proto(&self) -> proto::RoleKey {
744        proto::RoleKey {
745            id: self.id.into_proto(),
746        }
747    }
748
749    fn from_proto(proto: proto::RoleKey) -> Result<Self, TryFromProtoError> {
750        Ok(RoleKey {
751            id: proto.id.into_rust()?,
752        })
753    }
754}
755
756impl RustType<proto::RoleValue> for RoleValue {
757    fn into_proto(&self) -> proto::RoleValue {
758        proto::RoleValue {
759            name: self.name.to_string(),
760            attributes: self.attributes.into_proto(),
761            membership: self.membership.into_proto(),
762            vars: self.vars.into_proto(),
763            oid: self.oid,
764        }
765    }
766
767    fn from_proto(proto: proto::RoleValue) -> Result<Self, TryFromProtoError> {
768        Ok(RoleValue {
769            name: proto.name,
770            attributes: proto.attributes.into_rust()?,
771            membership: proto.membership.into_rust()?,
772            vars: proto.vars.into_rust()?,
773            oid: proto.oid,
774        })
775    }
776}
777
778impl RustType<proto::RoleAuthKey> for RoleAuthKey {
779    fn into_proto(&self) -> proto::RoleAuthKey {
780        proto::RoleAuthKey {
781            id: self.role_id.into_proto(),
782        }
783    }
784
785    fn from_proto(proto: proto::RoleAuthKey) -> Result<Self, TryFromProtoError> {
786        Ok(RoleAuthKey {
787            role_id: proto.id.into_rust()?,
788        })
789    }
790}
791
792impl RustType<proto::RoleAuthValue> for RoleAuthValue {
793    fn into_proto(&self) -> proto::RoleAuthValue {
794        proto::RoleAuthValue {
795            password_hash: self.password_hash.clone(),
796            updated_at: proto::EpochMillis {
797                millis: self.updated_at,
798            },
799        }
800    }
801
802    fn from_proto(proto: proto::RoleAuthValue) -> Result<Self, TryFromProtoError> {
803        Ok(RoleAuthValue {
804            password_hash: proto.password_hash,
805            updated_at: proto.updated_at.into_rust()?,
806        })
807    }
808}
809
810impl RustType<proto::QueryPolicyKey> for QueryPolicyKey {
811    fn into_proto(&self) -> proto::QueryPolicyKey {
812        proto::QueryPolicyKey {
813            id: self.id.into_proto(),
814        }
815    }
816
817    fn from_proto(proto: proto::QueryPolicyKey) -> Result<Self, TryFromProtoError> {
818        Ok(Self {
819            id: proto.id.into_rust()?,
820        })
821    }
822}
823
824impl RustType<proto::QueryPolicyValue> for QueryPolicyValue {
825    fn into_proto(&self) -> proto::QueryPolicyValue {
826        proto::QueryPolicyValue {
827            name: self.name.clone(),
828            mode: self.mode.into_proto(),
829            rules: self.rules.into_proto(),
830            owner_id: self.owner_id.into_proto(),
831            privileges: self.privileges.into_proto(),
832            oid: self.oid,
833        }
834    }
835
836    fn from_proto(proto: proto::QueryPolicyValue) -> Result<Self, TryFromProtoError> {
837        Ok(Self {
838            name: proto.name,
839            mode: proto.mode.into_rust()?,
840            rules: proto.rules.into_rust()?,
841            owner_id: proto.owner_id.into_rust()?,
842            privileges: proto.privileges.into_rust()?,
843            oid: proto.oid,
844        })
845    }
846}
847
848impl RustType<proto::NetworkPolicyKey> for NetworkPolicyKey {
849    fn into_proto(&self) -> proto::NetworkPolicyKey {
850        proto::NetworkPolicyKey {
851            id: self.id.into_proto(),
852        }
853    }
854
855    fn from_proto(proto: proto::NetworkPolicyKey) -> Result<Self, TryFromProtoError> {
856        Ok(NetworkPolicyKey {
857            id: proto.id.into_rust()?,
858        })
859    }
860}
861
862impl RustType<proto::NetworkPolicyValue> for NetworkPolicyValue {
863    fn into_proto(&self) -> proto::NetworkPolicyValue {
864        proto::NetworkPolicyValue {
865            name: self.name.to_string(),
866            rules: self.rules.into_proto(),
867            owner_id: self.owner_id.into_proto(),
868            privileges: self.privileges.into_proto(),
869            oid: self.oid,
870        }
871    }
872
873    fn from_proto(proto: proto::NetworkPolicyValue) -> Result<Self, TryFromProtoError> {
874        Ok(NetworkPolicyValue {
875            name: proto.name,
876            rules: proto.rules.into_rust()?,
877            owner_id: proto.owner_id.into_rust()?,
878            privileges: proto.privileges.into_rust()?,
879            oid: proto.oid,
880        })
881    }
882}
883
884impl RustType<proto::ClusterReplicaSizeKey> for ClusterReplicaSizeKey {
885    fn into_proto(&self) -> proto::ClusterReplicaSizeKey {
886        proto::ClusterReplicaSizeKey {
887            id: self.id.into_proto(),
888        }
889    }
890
891    fn from_proto(proto: proto::ClusterReplicaSizeKey) -> Result<Self, TryFromProtoError> {
892        Ok(ClusterReplicaSizeKey {
893            id: proto.id.into_rust()?,
894        })
895    }
896}
897
898impl RustType<proto::ClusterReplicaSizeValue> for ClusterReplicaSizeValue {
899    fn into_proto(&self) -> proto::ClusterReplicaSizeValue {
900        proto::ClusterReplicaSizeValue {
901            name: self.name.clone(),
902            memory_limit: self.memory_limit,
903            cpu_limit: self.cpu_limit,
904            cpu_request: self.cpu_request,
905            disk_limit: self.disk_limit,
906            scale: self.scale,
907            workers: self.workers,
908            credits_per_hour: self.credits_per_hour.clone(),
909            cpu_exclusive: self.cpu_exclusive,
910            is_cc: self.is_cc,
911            family: self.family.clone(),
912            swap_enabled: self.swap_enabled,
913            disabled: self.disabled,
914            selectors: self.selectors.clone(),
915        }
916    }
917
918    fn from_proto(proto: proto::ClusterReplicaSizeValue) -> Result<Self, TryFromProtoError> {
919        Ok(ClusterReplicaSizeValue {
920            name: proto.name,
921            memory_limit: proto.memory_limit,
922            cpu_limit: proto.cpu_limit,
923            cpu_request: proto.cpu_request,
924            disk_limit: proto.disk_limit,
925            scale: proto.scale,
926            workers: proto.workers,
927            credits_per_hour: proto.credits_per_hour,
928            cpu_exclusive: proto.cpu_exclusive,
929            is_cc: proto.is_cc,
930            family: proto.family,
931            swap_enabled: proto.swap_enabled,
932            disabled: proto.disabled,
933            selectors: proto.selectors,
934        })
935    }
936}
937
938impl RustType<proto::ConfigKey> for ConfigKey {
939    fn into_proto(&self) -> proto::ConfigKey {
940        proto::ConfigKey {
941            key: self.key.to_string(),
942        }
943    }
944
945    fn from_proto(proto: proto::ConfigKey) -> Result<Self, TryFromProtoError> {
946        Ok(ConfigKey { key: proto.key })
947    }
948}
949
950impl RustType<proto::ConfigValue> for ConfigValue {
951    fn into_proto(&self) -> proto::ConfigValue {
952        proto::ConfigValue { value: self.value }
953    }
954
955    fn from_proto(proto: proto::ConfigValue) -> Result<Self, TryFromProtoError> {
956        Ok(ConfigValue { value: proto.value })
957    }
958}
959
960impl RustType<proto::AuditLogKey> for AuditLogKey {
961    fn into_proto(&self) -> proto::AuditLogKey {
962        proto::AuditLogKey {
963            event: self.event.into_proto(),
964        }
965    }
966
967    fn from_proto(proto: proto::AuditLogKey) -> Result<Self, TryFromProtoError> {
968        Ok(AuditLogKey {
969            event: proto.event.into_rust()?,
970        })
971    }
972}
973
974impl RustType<proto::StorageCollectionMetadataKey> for StorageCollectionMetadataKey {
975    fn into_proto(&self) -> proto::StorageCollectionMetadataKey {
976        proto::StorageCollectionMetadataKey {
977            id: self.id.into_proto(),
978        }
979    }
980
981    fn from_proto(proto: proto::StorageCollectionMetadataKey) -> Result<Self, TryFromProtoError> {
982        Ok(StorageCollectionMetadataKey {
983            id: proto.id.into_rust()?,
984        })
985    }
986}
987
988impl RustType<proto::StorageCollectionMetadataValue> for StorageCollectionMetadataValue {
989    fn into_proto(&self) -> proto::StorageCollectionMetadataValue {
990        proto::StorageCollectionMetadataValue {
991            shard: self.shard.to_string(),
992        }
993    }
994
995    fn from_proto(proto: proto::StorageCollectionMetadataValue) -> Result<Self, TryFromProtoError> {
996        Ok(StorageCollectionMetadataValue {
997            shard: proto.shard.into_rust()?,
998        })
999    }
1000}
1001
1002impl RustType<proto::UnfinalizedShardKey> for UnfinalizedShardKey {
1003    fn into_proto(&self) -> proto::UnfinalizedShardKey {
1004        proto::UnfinalizedShardKey {
1005            shard: self.shard.to_string(),
1006        }
1007    }
1008
1009    fn from_proto(proto: proto::UnfinalizedShardKey) -> Result<Self, TryFromProtoError> {
1010        Ok(UnfinalizedShardKey {
1011            shard: proto.shard.into_rust()?,
1012        })
1013    }
1014}
1015
1016impl RustType<proto::TxnWalShardValue> for TxnWalShardValue {
1017    fn into_proto(&self) -> proto::TxnWalShardValue {
1018        proto::TxnWalShardValue {
1019            shard: self.shard.to_string(),
1020        }
1021    }
1022
1023    fn from_proto(proto: proto::TxnWalShardValue) -> Result<Self, TryFromProtoError> {
1024        Ok(TxnWalShardValue {
1025            shard: proto.shard.into_rust()?,
1026        })
1027    }
1028}
1029
1030impl RustType<proto::ServerConfigurationKey> for ServerConfigurationKey {
1031    fn into_proto(&self) -> proto::ServerConfigurationKey {
1032        proto::ServerConfigurationKey {
1033            name: self.name.clone(),
1034        }
1035    }
1036
1037    fn from_proto(proto: proto::ServerConfigurationKey) -> Result<Self, TryFromProtoError> {
1038        Ok(ServerConfigurationKey { name: proto.name })
1039    }
1040}
1041
1042impl RustType<proto::ServerConfigurationValue> for ServerConfigurationValue {
1043    fn into_proto(&self) -> proto::ServerConfigurationValue {
1044        proto::ServerConfigurationValue {
1045            value: self.value.clone(),
1046        }
1047    }
1048
1049    fn from_proto(proto: proto::ServerConfigurationValue) -> Result<Self, TryFromProtoError> {
1050        Ok(ServerConfigurationValue { value: proto.value })
1051    }
1052}
1053
1054impl RustType<proto::ClusterSystemConfigurationKey> for ClusterSystemConfigurationKey {
1055    fn into_proto(&self) -> proto::ClusterSystemConfigurationKey {
1056        proto::ClusterSystemConfigurationKey {
1057            cluster_id: self.cluster_id.into_proto(),
1058            name: self.name.clone(),
1059        }
1060    }
1061
1062    fn from_proto(proto: proto::ClusterSystemConfigurationKey) -> Result<Self, TryFromProtoError> {
1063        Ok(ClusterSystemConfigurationKey {
1064            cluster_id: proto.cluster_id.into_rust()?,
1065            name: proto.name,
1066        })
1067    }
1068}
1069
1070impl RustType<proto::ClusterSystemConfigurationValue> for ClusterSystemConfigurationValue {
1071    fn into_proto(&self) -> proto::ClusterSystemConfigurationValue {
1072        proto::ClusterSystemConfigurationValue {
1073            value: self.value.clone(),
1074        }
1075    }
1076
1077    fn from_proto(
1078        proto: proto::ClusterSystemConfigurationValue,
1079    ) -> Result<Self, TryFromProtoError> {
1080        Ok(ClusterSystemConfigurationValue { value: proto.value })
1081    }
1082}
1083
1084impl RustType<proto::ReplicaSystemConfigurationKey> for ReplicaSystemConfigurationKey {
1085    fn into_proto(&self) -> proto::ReplicaSystemConfigurationKey {
1086        proto::ReplicaSystemConfigurationKey {
1087            replica_id: self.replica_id.into_proto(),
1088            name: self.name.clone(),
1089        }
1090    }
1091
1092    fn from_proto(proto: proto::ReplicaSystemConfigurationKey) -> Result<Self, TryFromProtoError> {
1093        Ok(ReplicaSystemConfigurationKey {
1094            replica_id: proto.replica_id.into_rust()?,
1095            name: proto.name,
1096        })
1097    }
1098}
1099
1100impl RustType<proto::ReplicaSystemConfigurationValue> for ReplicaSystemConfigurationValue {
1101    fn into_proto(&self) -> proto::ReplicaSystemConfigurationValue {
1102        proto::ReplicaSystemConfigurationValue {
1103            value: self.value.clone(),
1104        }
1105    }
1106
1107    fn from_proto(
1108        proto: proto::ReplicaSystemConfigurationValue,
1109    ) -> Result<Self, TryFromProtoError> {
1110        Ok(ReplicaSystemConfigurationValue { value: proto.value })
1111    }
1112}
1113
1114impl RustType<proto::SourceReferencesKey> for SourceReferencesKey {
1115    fn into_proto(&self) -> proto::SourceReferencesKey {
1116        proto::SourceReferencesKey {
1117            source: self.source_id.into_proto(),
1118        }
1119    }
1120    fn from_proto(proto: proto::SourceReferencesKey) -> Result<Self, TryFromProtoError> {
1121        Ok(SourceReferencesKey {
1122            source_id: proto.source.into_rust()?,
1123        })
1124    }
1125}
1126
1127impl RustType<proto::SourceReferencesValue> for SourceReferencesValue {
1128    fn into_proto(&self) -> proto::SourceReferencesValue {
1129        proto::SourceReferencesValue {
1130            updated_at: proto::EpochMillis {
1131                millis: self.updated_at,
1132            },
1133            references: self
1134                .references
1135                .iter()
1136                .map(|reference| reference.into_proto())
1137                .collect(),
1138        }
1139    }
1140    fn from_proto(proto: proto::SourceReferencesValue) -> Result<Self, TryFromProtoError> {
1141        Ok(SourceReferencesValue {
1142            updated_at: proto.updated_at.into_rust()?,
1143            references: proto
1144                .references
1145                .into_iter()
1146                .map(|reference| reference.into_rust())
1147                .collect::<Result<_, _>>()?,
1148        })
1149    }
1150}
1151
1152impl RustType<proto::SourceReference> for SourceReference {
1153    fn into_proto(&self) -> proto::SourceReference {
1154        proto::SourceReference {
1155            name: self.name.clone(),
1156            namespace: self.namespace.clone(),
1157            columns: self.columns.clone(),
1158        }
1159    }
1160    fn from_proto(proto: proto::SourceReference) -> Result<Self, TryFromProtoError> {
1161        Ok(SourceReference {
1162            name: proto.name,
1163            namespace: proto.namespace,
1164            columns: proto.columns,
1165        })
1166    }
1167}
1168
1169impl RustType<proto::DefaultPrivilegesKey> for DefaultPrivilegesKey {
1170    fn into_proto(&self) -> proto::DefaultPrivilegesKey {
1171        proto::DefaultPrivilegesKey {
1172            role_id: self.role_id.into_proto(),
1173            database_id: self.database_id.map(|database_id| database_id.into_proto()),
1174            schema_id: self.schema_id.map(|schema_id| schema_id.into_proto()),
1175            object_type: self.object_type.into_proto(),
1176            grantee: self.grantee.into_proto(),
1177        }
1178    }
1179
1180    fn from_proto(proto: proto::DefaultPrivilegesKey) -> Result<Self, TryFromProtoError> {
1181        Ok(DefaultPrivilegesKey {
1182            role_id: proto.role_id.into_rust()?,
1183            database_id: proto.database_id.into_rust()?,
1184            schema_id: proto.schema_id.into_rust()?,
1185            object_type: proto.object_type.into_rust()?,
1186            grantee: proto.grantee.into_rust()?,
1187        })
1188    }
1189}
1190
1191impl RustType<proto::DefaultPrivilegesValue> for DefaultPrivilegesValue {
1192    fn into_proto(&self) -> proto::DefaultPrivilegesValue {
1193        proto::DefaultPrivilegesValue {
1194            privileges: self.privileges.into_proto(),
1195        }
1196    }
1197
1198    fn from_proto(proto: proto::DefaultPrivilegesValue) -> Result<Self, TryFromProtoError> {
1199        Ok(DefaultPrivilegesValue {
1200            privileges: proto.privileges.into_rust()?,
1201        })
1202    }
1203}
1204
1205impl RustType<proto::SystemPrivilegesKey> for SystemPrivilegesKey {
1206    fn into_proto(&self) -> proto::SystemPrivilegesKey {
1207        proto::SystemPrivilegesKey {
1208            grantee: self.grantee.into_proto(),
1209            grantor: self.grantor.into_proto(),
1210        }
1211    }
1212
1213    fn from_proto(proto: proto::SystemPrivilegesKey) -> Result<Self, TryFromProtoError> {
1214        Ok(SystemPrivilegesKey {
1215            grantee: proto.grantee.into_rust()?,
1216            grantor: proto.grantor.into_rust()?,
1217        })
1218    }
1219}
1220
1221impl RustType<proto::SystemPrivilegesValue> for SystemPrivilegesValue {
1222    fn into_proto(&self) -> proto::SystemPrivilegesValue {
1223        proto::SystemPrivilegesValue {
1224            acl_mode: self.acl_mode.into_proto(),
1225        }
1226    }
1227
1228    fn from_proto(proto: proto::SystemPrivilegesValue) -> Result<Self, TryFromProtoError> {
1229        Ok(SystemPrivilegesValue {
1230            acl_mode: proto.acl_mode.into_rust()?,
1231        })
1232    }
1233}
1234
1235impl RustType<proto::SystemCatalogItemId> for SystemCatalogItemId {
1236    fn into_proto(&self) -> proto::SystemCatalogItemId {
1237        proto::SystemCatalogItemId(self.0)
1238    }
1239
1240    fn from_proto(proto: proto::SystemCatalogItemId) -> Result<Self, TryFromProtoError> {
1241        Ok(SystemCatalogItemId(proto.0))
1242    }
1243}
1244
1245impl RustType<proto::IntrospectionSourceIndexCatalogItemId>
1246    for IntrospectionSourceIndexCatalogItemId
1247{
1248    fn into_proto(&self) -> proto::IntrospectionSourceIndexCatalogItemId {
1249        proto::IntrospectionSourceIndexCatalogItemId(self.0)
1250    }
1251
1252    fn from_proto(
1253        proto: proto::IntrospectionSourceIndexCatalogItemId,
1254    ) -> Result<Self, TryFromProtoError> {
1255        Ok(IntrospectionSourceIndexCatalogItemId(proto.0))
1256    }
1257}
1258
1259impl RustType<proto::SystemGlobalId> for SystemGlobalId {
1260    fn into_proto(&self) -> proto::SystemGlobalId {
1261        proto::SystemGlobalId(self.0)
1262    }
1263
1264    fn from_proto(proto: proto::SystemGlobalId) -> Result<Self, TryFromProtoError> {
1265        Ok(SystemGlobalId(proto.0))
1266    }
1267}
1268
1269impl RustType<proto::IntrospectionSourceIndexGlobalId> for IntrospectionSourceIndexGlobalId {
1270    fn into_proto(&self) -> proto::IntrospectionSourceIndexGlobalId {
1271        proto::IntrospectionSourceIndexGlobalId(self.0)
1272    }
1273
1274    fn from_proto(
1275        proto: proto::IntrospectionSourceIndexGlobalId,
1276    ) -> Result<Self, TryFromProtoError> {
1277        Ok(IntrospectionSourceIndexGlobalId(proto.0))
1278    }
1279}
1280
1281#[cfg(test)]
1282mod tests {
1283    use crate::durable::objects::serialization::RustType;
1284    use mz_audit_log::VersionedEvent;
1285    use proptest::prelude::*;
1286
1287    proptest! {
1288        #[mz_ore::test]
1289        #[cfg_attr(miri, ignore)] // slow
1290        fn proptest_audit_log_roundtrips(event: VersionedEvent) {
1291            let proto = event.into_proto();
1292            let roundtrip = VersionedEvent::from_proto(proto).expect("valid proto");
1293
1294            prop_assert_eq!(event, roundtrip);
1295        }
1296    }
1297}