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