1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Protobuf structs mirroring `crate::id`.

use bytes::BufMut;
use prost::Message;

use crate::id::PartitionId;
use crate::{Id, LocalId};
use mz_repr::proto::global_id::ProtoGlobalId;
use mz_repr::proto::TryFromProtoError;

include!(concat!(env!("OUT_DIR"), "/id.rs"));

impl From<&Id> for ProtoId {
    fn from(x: &Id) -> Self {
        ProtoId {
            kind: Some(match x {
                Id::Global(g) => proto_id::Kind::Global(ProtoGlobalId::from(g)),
                Id::Local(l) => proto_id::Kind::Local(l.into()),
            }),
        }
    }
}

impl TryFrom<ProtoId> for Id {
    type Error = TryFromProtoError;

    fn try_from(x: ProtoId) -> Result<Self, Self::Error> {
        match x.kind {
            Some(proto_id::Kind::Global(x)) => Ok(Id::Global(x.try_into()?)),
            Some(proto_id::Kind::Local(x)) => Ok(Id::Local(x.try_into()?)),
            None => Err(TryFromProtoError::missing_field("ProtoId::kind")),
        }
    }
}

impl From<&LocalId> for ProtoLocalId {
    fn from(x: &LocalId) -> Self {
        ProtoLocalId { value: x.0 }
    }
}

impl TryFrom<ProtoLocalId> for LocalId {
    type Error = TryFromProtoError;

    fn try_from(x: ProtoLocalId) -> Result<Self, Self::Error> {
        Ok(LocalId::new(x.value))
    }
}

impl From<&PartitionId> for ProtoPartitionId {
    fn from(x: &PartitionId) -> Self {
        ProtoPartitionId {
            kind: Some(match x {
                PartitionId::Kafka(x) => proto_partition_id::Kind::Kafka(*x),
                PartitionId::None => proto_partition_id::Kind::None(()),
            }),
        }
    }
}

impl TryFrom<ProtoPartitionId> for PartitionId {
    type Error = TryFromProtoError;

    fn try_from(x: ProtoPartitionId) -> Result<Self, Self::Error> {
        match x.kind {
            Some(proto_partition_id::Kind::Kafka(x)) => Ok(PartitionId::Kafka(x)),
            Some(proto_partition_id::Kind::None(_)) => Ok(PartitionId::None),
            None => Err(TryFromProtoError::missing_field("ProtoPartitionId::kind")),
        }
    }
}

impl mz_persist_types::Codec for PartitionId {
    fn codec_name() -> String {
        "protobuf[PartitionId]".into()
    }

    fn encode<B: BufMut>(&self, buf: &mut B) {
        ProtoPartitionId::from(self)
            .encode(buf)
            .expect("provided buffer had sufficient capacity")
    }

    fn decode<'a>(buf: &'a [u8]) -> Result<Self, String> {
        ProtoPartitionId::decode(buf)
            .map_err(|err| err.to_string())?
            .try_into()
            .map_err(|err: TryFromProtoError| err.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mz_repr::proto::protobuf_roundtrip;
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn id_protobuf_roundtrip(expect in any::<Id>()) {
            let actual = protobuf_roundtrip::<_, ProtoId>(&expect);
            assert!(actual.is_ok());
            assert_eq!(actual.unwrap(), expect);
        }
    }
}