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