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