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