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        }
567    }
568
569    fn from_proto(proto: proto::ItemValue) -> Result<Self, TryFromProtoError> {
570        let create_sql = match proto.definition {
571            proto::CatalogItem::V1(c) => c.create_sql,
572        };
573        let extra_versions = proto
574            .extra_versions
575            .into_iter()
576            .map(|item_version| {
577                let version = item_version.version.into_rust()?;
578                let global_id = item_version.global_id.into_rust()?;
579                Ok::<_, TryFromProtoError>((version, global_id))
580            })
581            .collect::<Result<_, _>>()?;
582        Ok(ItemValue {
583            schema_id: proto.schema_id.into_rust()?,
584            name: proto.name,
585            create_sql,
586            owner_id: proto.owner_id.into_rust()?,
587            privileges: proto.privileges.into_rust()?,
588            oid: proto.oid,
589            global_id: proto.global_id.into_rust()?,
590            extra_versions,
591        })
592    }
593}
594
595impl RustType<proto::CommentKey> for CommentKey {
596    fn into_proto(&self) -> proto::CommentKey {
597        let sub_component = match &self.sub_component {
598            Some(pos) => Some(proto::CommentSubComponent::ColumnPos(CastFrom::cast_from(
599                *pos,
600            ))),
601            None => None,
602        };
603        proto::CommentKey {
604            object: self.object_id.into_proto(),
605            sub_component,
606        }
607    }
608
609    fn from_proto(proto: proto::CommentKey) -> Result<Self, TryFromProtoError> {
610        let sub_component = match proto.sub_component {
611            Some(proto::CommentSubComponent::ColumnPos(pos)) => Some(CastFrom::cast_from(pos)),
612            None => None,
613        };
614        Ok(CommentKey {
615            object_id: proto.object.into_rust()?,
616            sub_component,
617        })
618    }
619}
620
621impl RustType<proto::CommentValue> for CommentValue {
622    fn into_proto(&self) -> proto::CommentValue {
623        proto::CommentValue {
624            comment: self.comment.clone(),
625        }
626    }
627
628    fn from_proto(proto: proto::CommentValue) -> Result<Self, TryFromProtoError> {
629        Ok(CommentValue {
630            comment: proto.comment,
631        })
632    }
633}
634
635impl RustType<proto::RoleKey> for RoleKey {
636    fn into_proto(&self) -> proto::RoleKey {
637        proto::RoleKey {
638            id: self.id.into_proto(),
639        }
640    }
641
642    fn from_proto(proto: proto::RoleKey) -> Result<Self, TryFromProtoError> {
643        Ok(RoleKey {
644            id: proto.id.into_rust()?,
645        })
646    }
647}
648
649impl RustType<proto::RoleValue> for RoleValue {
650    fn into_proto(&self) -> proto::RoleValue {
651        proto::RoleValue {
652            name: self.name.to_string(),
653            attributes: self.attributes.into_proto(),
654            membership: self.membership.into_proto(),
655            vars: self.vars.into_proto(),
656            oid: self.oid,
657        }
658    }
659
660    fn from_proto(proto: proto::RoleValue) -> Result<Self, TryFromProtoError> {
661        Ok(RoleValue {
662            name: proto.name,
663            attributes: proto.attributes.into_rust()?,
664            membership: proto.membership.into_rust()?,
665            vars: proto.vars.into_rust()?,
666            oid: proto.oid,
667        })
668    }
669}
670
671impl RustType<proto::RoleAuthKey> for RoleAuthKey {
672    fn into_proto(&self) -> proto::RoleAuthKey {
673        proto::RoleAuthKey {
674            id: self.role_id.into_proto(),
675        }
676    }
677
678    fn from_proto(proto: proto::RoleAuthKey) -> Result<Self, TryFromProtoError> {
679        Ok(RoleAuthKey {
680            role_id: proto.id.into_rust()?,
681        })
682    }
683}
684
685impl RustType<proto::RoleAuthValue> for RoleAuthValue {
686    fn into_proto(&self) -> proto::RoleAuthValue {
687        proto::RoleAuthValue {
688            password_hash: self.password_hash.clone(),
689            updated_at: proto::EpochMillis {
690                millis: self.updated_at,
691            },
692        }
693    }
694
695    fn from_proto(proto: proto::RoleAuthValue) -> Result<Self, TryFromProtoError> {
696        Ok(RoleAuthValue {
697            password_hash: proto.password_hash,
698            updated_at: proto.updated_at.into_rust()?,
699        })
700    }
701}
702
703impl RustType<proto::NetworkPolicyKey> for NetworkPolicyKey {
704    fn into_proto(&self) -> proto::NetworkPolicyKey {
705        proto::NetworkPolicyKey {
706            id: self.id.into_proto(),
707        }
708    }
709
710    fn from_proto(proto: proto::NetworkPolicyKey) -> Result<Self, TryFromProtoError> {
711        Ok(NetworkPolicyKey {
712            id: proto.id.into_rust()?,
713        })
714    }
715}
716
717impl RustType<proto::NetworkPolicyValue> for NetworkPolicyValue {
718    fn into_proto(&self) -> proto::NetworkPolicyValue {
719        proto::NetworkPolicyValue {
720            name: self.name.to_string(),
721            rules: self.rules.into_proto(),
722            owner_id: self.owner_id.into_proto(),
723            privileges: self.privileges.into_proto(),
724            oid: self.oid,
725        }
726    }
727
728    fn from_proto(proto: proto::NetworkPolicyValue) -> Result<Self, TryFromProtoError> {
729        Ok(NetworkPolicyValue {
730            name: proto.name,
731            rules: proto.rules.into_rust()?,
732            owner_id: proto.owner_id.into_rust()?,
733            privileges: proto.privileges.into_rust()?,
734            oid: proto.oid,
735        })
736    }
737}
738
739impl RustType<proto::ConfigKey> for ConfigKey {
740    fn into_proto(&self) -> proto::ConfigKey {
741        proto::ConfigKey {
742            key: self.key.to_string(),
743        }
744    }
745
746    fn from_proto(proto: proto::ConfigKey) -> Result<Self, TryFromProtoError> {
747        Ok(ConfigKey { key: proto.key })
748    }
749}
750
751impl RustType<proto::ConfigValue> for ConfigValue {
752    fn into_proto(&self) -> proto::ConfigValue {
753        proto::ConfigValue { value: self.value }
754    }
755
756    fn from_proto(proto: proto::ConfigValue) -> Result<Self, TryFromProtoError> {
757        Ok(ConfigValue { value: proto.value })
758    }
759}
760
761impl RustType<proto::AuditLogKey> for AuditLogKey {
762    fn into_proto(&self) -> proto::AuditLogKey {
763        proto::AuditLogKey {
764            event: self.event.into_proto(),
765        }
766    }
767
768    fn from_proto(proto: proto::AuditLogKey) -> Result<Self, TryFromProtoError> {
769        Ok(AuditLogKey {
770            event: proto.event.into_rust()?,
771        })
772    }
773}
774
775impl RustType<proto::StorageCollectionMetadataKey> for StorageCollectionMetadataKey {
776    fn into_proto(&self) -> proto::StorageCollectionMetadataKey {
777        proto::StorageCollectionMetadataKey {
778            id: self.id.into_proto(),
779        }
780    }
781
782    fn from_proto(proto: proto::StorageCollectionMetadataKey) -> Result<Self, TryFromProtoError> {
783        Ok(StorageCollectionMetadataKey {
784            id: proto.id.into_rust()?,
785        })
786    }
787}
788
789impl RustType<proto::StorageCollectionMetadataValue> for StorageCollectionMetadataValue {
790    fn into_proto(&self) -> proto::StorageCollectionMetadataValue {
791        proto::StorageCollectionMetadataValue {
792            shard: self.shard.to_string(),
793        }
794    }
795
796    fn from_proto(proto: proto::StorageCollectionMetadataValue) -> Result<Self, TryFromProtoError> {
797        Ok(StorageCollectionMetadataValue {
798            shard: proto.shard.into_rust()?,
799        })
800    }
801}
802
803impl RustType<proto::UnfinalizedShardKey> for UnfinalizedShardKey {
804    fn into_proto(&self) -> proto::UnfinalizedShardKey {
805        proto::UnfinalizedShardKey {
806            shard: self.shard.to_string(),
807        }
808    }
809
810    fn from_proto(proto: proto::UnfinalizedShardKey) -> Result<Self, TryFromProtoError> {
811        Ok(UnfinalizedShardKey {
812            shard: proto.shard.into_rust()?,
813        })
814    }
815}
816
817impl RustType<proto::TxnWalShardValue> for TxnWalShardValue {
818    fn into_proto(&self) -> proto::TxnWalShardValue {
819        proto::TxnWalShardValue {
820            shard: self.shard.to_string(),
821        }
822    }
823
824    fn from_proto(proto: proto::TxnWalShardValue) -> Result<Self, TryFromProtoError> {
825        Ok(TxnWalShardValue {
826            shard: proto.shard.into_rust()?,
827        })
828    }
829}
830
831impl RustType<proto::ServerConfigurationKey> for ServerConfigurationKey {
832    fn into_proto(&self) -> proto::ServerConfigurationKey {
833        proto::ServerConfigurationKey {
834            name: self.name.clone(),
835        }
836    }
837
838    fn from_proto(proto: proto::ServerConfigurationKey) -> Result<Self, TryFromProtoError> {
839        Ok(ServerConfigurationKey { name: proto.name })
840    }
841}
842
843impl RustType<proto::ServerConfigurationValue> for ServerConfigurationValue {
844    fn into_proto(&self) -> proto::ServerConfigurationValue {
845        proto::ServerConfigurationValue {
846            value: self.value.clone(),
847        }
848    }
849
850    fn from_proto(proto: proto::ServerConfigurationValue) -> Result<Self, TryFromProtoError> {
851        Ok(ServerConfigurationValue { value: proto.value })
852    }
853}
854
855impl RustType<proto::ClusterSystemConfigurationKey> for ClusterSystemConfigurationKey {
856    fn into_proto(&self) -> proto::ClusterSystemConfigurationKey {
857        proto::ClusterSystemConfigurationKey {
858            cluster_id: self.cluster_id.into_proto(),
859            name: self.name.clone(),
860        }
861    }
862
863    fn from_proto(proto: proto::ClusterSystemConfigurationKey) -> Result<Self, TryFromProtoError> {
864        Ok(ClusterSystemConfigurationKey {
865            cluster_id: proto.cluster_id.into_rust()?,
866            name: proto.name,
867        })
868    }
869}
870
871impl RustType<proto::ClusterSystemConfigurationValue> for ClusterSystemConfigurationValue {
872    fn into_proto(&self) -> proto::ClusterSystemConfigurationValue {
873        proto::ClusterSystemConfigurationValue {
874            value: self.value.clone(),
875        }
876    }
877
878    fn from_proto(
879        proto: proto::ClusterSystemConfigurationValue,
880    ) -> Result<Self, TryFromProtoError> {
881        Ok(ClusterSystemConfigurationValue { value: proto.value })
882    }
883}
884
885impl RustType<proto::ReplicaSystemConfigurationKey> for ReplicaSystemConfigurationKey {
886    fn into_proto(&self) -> proto::ReplicaSystemConfigurationKey {
887        proto::ReplicaSystemConfigurationKey {
888            replica_id: self.replica_id.into_proto(),
889            name: self.name.clone(),
890        }
891    }
892
893    fn from_proto(proto: proto::ReplicaSystemConfigurationKey) -> Result<Self, TryFromProtoError> {
894        Ok(ReplicaSystemConfigurationKey {
895            replica_id: proto.replica_id.into_rust()?,
896            name: proto.name,
897        })
898    }
899}
900
901impl RustType<proto::ReplicaSystemConfigurationValue> for ReplicaSystemConfigurationValue {
902    fn into_proto(&self) -> proto::ReplicaSystemConfigurationValue {
903        proto::ReplicaSystemConfigurationValue {
904            value: self.value.clone(),
905        }
906    }
907
908    fn from_proto(
909        proto: proto::ReplicaSystemConfigurationValue,
910    ) -> Result<Self, TryFromProtoError> {
911        Ok(ReplicaSystemConfigurationValue { value: proto.value })
912    }
913}
914
915impl RustType<proto::SourceReferencesKey> for SourceReferencesKey {
916    fn into_proto(&self) -> proto::SourceReferencesKey {
917        proto::SourceReferencesKey {
918            source: self.source_id.into_proto(),
919        }
920    }
921    fn from_proto(proto: proto::SourceReferencesKey) -> Result<Self, TryFromProtoError> {
922        Ok(SourceReferencesKey {
923            source_id: proto.source.into_rust()?,
924        })
925    }
926}
927
928impl RustType<proto::SourceReferencesValue> for SourceReferencesValue {
929    fn into_proto(&self) -> proto::SourceReferencesValue {
930        proto::SourceReferencesValue {
931            updated_at: proto::EpochMillis {
932                millis: self.updated_at,
933            },
934            references: self
935                .references
936                .iter()
937                .map(|reference| reference.into_proto())
938                .collect(),
939        }
940    }
941    fn from_proto(proto: proto::SourceReferencesValue) -> Result<Self, TryFromProtoError> {
942        Ok(SourceReferencesValue {
943            updated_at: proto.updated_at.into_rust()?,
944            references: proto
945                .references
946                .into_iter()
947                .map(|reference| reference.into_rust())
948                .collect::<Result<_, _>>()?,
949        })
950    }
951}
952
953impl RustType<proto::SourceReference> for SourceReference {
954    fn into_proto(&self) -> proto::SourceReference {
955        proto::SourceReference {
956            name: self.name.clone(),
957            namespace: self.namespace.clone(),
958            columns: self.columns.clone(),
959        }
960    }
961    fn from_proto(proto: proto::SourceReference) -> Result<Self, TryFromProtoError> {
962        Ok(SourceReference {
963            name: proto.name,
964            namespace: proto.namespace,
965            columns: proto.columns,
966        })
967    }
968}
969
970impl RustType<proto::DefaultPrivilegesKey> for DefaultPrivilegesKey {
971    fn into_proto(&self) -> proto::DefaultPrivilegesKey {
972        proto::DefaultPrivilegesKey {
973            role_id: self.role_id.into_proto(),
974            database_id: self.database_id.map(|database_id| database_id.into_proto()),
975            schema_id: self.schema_id.map(|schema_id| schema_id.into_proto()),
976            object_type: self.object_type.into_proto(),
977            grantee: self.grantee.into_proto(),
978        }
979    }
980
981    fn from_proto(proto: proto::DefaultPrivilegesKey) -> Result<Self, TryFromProtoError> {
982        Ok(DefaultPrivilegesKey {
983            role_id: proto.role_id.into_rust()?,
984            database_id: proto.database_id.into_rust()?,
985            schema_id: proto.schema_id.into_rust()?,
986            object_type: proto.object_type.into_rust()?,
987            grantee: proto.grantee.into_rust()?,
988        })
989    }
990}
991
992impl RustType<proto::DefaultPrivilegesValue> for DefaultPrivilegesValue {
993    fn into_proto(&self) -> proto::DefaultPrivilegesValue {
994        proto::DefaultPrivilegesValue {
995            privileges: self.privileges.into_proto(),
996        }
997    }
998
999    fn from_proto(proto: proto::DefaultPrivilegesValue) -> Result<Self, TryFromProtoError> {
1000        Ok(DefaultPrivilegesValue {
1001            privileges: proto.privileges.into_rust()?,
1002        })
1003    }
1004}
1005
1006impl RustType<proto::SystemPrivilegesKey> for SystemPrivilegesKey {
1007    fn into_proto(&self) -> proto::SystemPrivilegesKey {
1008        proto::SystemPrivilegesKey {
1009            grantee: self.grantee.into_proto(),
1010            grantor: self.grantor.into_proto(),
1011        }
1012    }
1013
1014    fn from_proto(proto: proto::SystemPrivilegesKey) -> Result<Self, TryFromProtoError> {
1015        Ok(SystemPrivilegesKey {
1016            grantee: proto.grantee.into_rust()?,
1017            grantor: proto.grantor.into_rust()?,
1018        })
1019    }
1020}
1021
1022impl RustType<proto::SystemPrivilegesValue> for SystemPrivilegesValue {
1023    fn into_proto(&self) -> proto::SystemPrivilegesValue {
1024        proto::SystemPrivilegesValue {
1025            acl_mode: self.acl_mode.into_proto(),
1026        }
1027    }
1028
1029    fn from_proto(proto: proto::SystemPrivilegesValue) -> Result<Self, TryFromProtoError> {
1030        Ok(SystemPrivilegesValue {
1031            acl_mode: proto.acl_mode.into_rust()?,
1032        })
1033    }
1034}
1035
1036impl RustType<proto::SystemCatalogItemId> for SystemCatalogItemId {
1037    fn into_proto(&self) -> proto::SystemCatalogItemId {
1038        proto::SystemCatalogItemId(self.0)
1039    }
1040
1041    fn from_proto(proto: proto::SystemCatalogItemId) -> Result<Self, TryFromProtoError> {
1042        Ok(SystemCatalogItemId(proto.0))
1043    }
1044}
1045
1046impl RustType<proto::IntrospectionSourceIndexCatalogItemId>
1047    for IntrospectionSourceIndexCatalogItemId
1048{
1049    fn into_proto(&self) -> proto::IntrospectionSourceIndexCatalogItemId {
1050        proto::IntrospectionSourceIndexCatalogItemId(self.0)
1051    }
1052
1053    fn from_proto(
1054        proto: proto::IntrospectionSourceIndexCatalogItemId,
1055    ) -> Result<Self, TryFromProtoError> {
1056        Ok(IntrospectionSourceIndexCatalogItemId(proto.0))
1057    }
1058}
1059
1060impl RustType<proto::SystemGlobalId> for SystemGlobalId {
1061    fn into_proto(&self) -> proto::SystemGlobalId {
1062        proto::SystemGlobalId(self.0)
1063    }
1064
1065    fn from_proto(proto: proto::SystemGlobalId) -> Result<Self, TryFromProtoError> {
1066        Ok(SystemGlobalId(proto.0))
1067    }
1068}
1069
1070impl RustType<proto::IntrospectionSourceIndexGlobalId> for IntrospectionSourceIndexGlobalId {
1071    fn into_proto(&self) -> proto::IntrospectionSourceIndexGlobalId {
1072        proto::IntrospectionSourceIndexGlobalId(self.0)
1073    }
1074
1075    fn from_proto(
1076        proto: proto::IntrospectionSourceIndexGlobalId,
1077    ) -> Result<Self, TryFromProtoError> {
1078        Ok(IntrospectionSourceIndexGlobalId(proto.0))
1079    }
1080}
1081
1082#[cfg(test)]
1083mod tests {
1084    use mz_audit_log::VersionedEvent;
1085    use mz_proto::RustType;
1086    use proptest::prelude::*;
1087
1088    proptest! {
1089        #[mz_ore::test]
1090        #[cfg_attr(miri, ignore)] // slow
1091        fn proptest_audit_log_roundtrips(event: VersionedEvent) {
1092            let proto = event.into_proto();
1093            let roundtrip = VersionedEvent::from_proto(proto).expect("valid proto");
1094
1095            prop_assert_eq!(event, roundtrip);
1096        }
1097    }
1098}