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