12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- /// A type that can be decoded from a Protocol Buffer raw enum value.
16
- ///
17
- /// Protobuf enums are represented as strings in JSON. A default `Decodable` implementation is
18
- /// provided when conforming to this type.
19
- protocol DecodableProtoEnum : Decodable {
15
+ /// A type that represents a Protocol Buffer raw enum value.
16
+ protocol ProtoEnum {
20
17
/// The type representing the valid values for the protobuf enum.
21
18
///
22
19
/// > Important: This type must conform to `RawRepresentable` with the `RawValue == String`.
@@ -32,8 +29,8 @@ protocol DecodableProtoEnum: Decodable {
32
29
/// ```
33
30
associatedtype Kind : RawRepresentable < String >
34
31
35
- /// Returns the ``VertexLog/MessageCode`` associated with unrecognized (unknown) enum values .
36
- var unrecognizedValueMessageCode : VertexLog . MessageCode { get }
32
+ /// Returns the raw string value of the enum.
33
+ var rawValue : String { get }
37
34
38
35
/// Create a new instance of the specified type from a raw enum value.
39
36
init ( rawValue: String )
@@ -42,14 +39,48 @@ protocol DecodableProtoEnum: Decodable {
42
39
///
43
40
/// > Important: A default implementation is provided.
44
41
init ( kind: Kind )
42
+ }
43
+
44
+ /// A type that can be decoded from a Protocol Buffer raw enum value.
45
+ ///
46
+ /// Protobuf enums are represented as strings in JSON. A default `Decodable` implementation is
47
+ /// provided when conforming to this type.
48
+ protocol DecodableProtoEnum : ProtoEnum , Decodable {
49
+ /// Returns the ``VertexLog/MessageCode`` associated with unrecognized (unknown) enum values.
50
+ var unrecognizedValueMessageCode : VertexLog . MessageCode { get }
45
51
46
52
/// Creates a new instance by decoding from the given decoder.
47
53
///
48
54
/// > Important: A default implementation is provided.
49
- init ( from decoder: Decoder ) throws
55
+ init ( from decoder: any Decoder ) throws
50
56
}
51
57
52
- /// Default `Decodable` implementation for types conforming to `DecodableProtoEnum`.
58
+ /// A type that can be encoded as a Protocol Buffer enum value.
59
+ ///
60
+ /// Protobuf enums are represented as strings in JSON. A default `Encodable` implementation is
61
+ /// provided when conforming to this type.
62
+ protocol EncodableProtoEnum : ProtoEnum , Encodable {
63
+ /// Encodes this value into the given encoder.
64
+ ///
65
+ /// > Important: A default implementation is provided.
66
+ func encode( to encoder: any Encoder ) throws
67
+ }
68
+
69
+ /// A type that can be decoded and encoded from a Protocol Buffer raw enum value.
70
+ ///
71
+ /// See ``ProtoEnum``, ``DecodableProtoEnum`` and ``EncodableProtoEnum`` for more details.
72
+ protocol CodableProtoEnum : DecodableProtoEnum , EncodableProtoEnum { }
73
+
74
+ // MARK: - Default Implementations
75
+
76
+ // Default implementation of `init(kind: Kind)` for types conforming to `ProtoEnum`.
77
+ extension ProtoEnum {
78
+ init ( kind: Kind ) {
79
+ self = Self ( rawValue: kind. rawValue)
80
+ }
81
+ }
82
+
83
+ // Default `Decodable` implementation for types conforming to `DecodableProtoEnum`.
53
84
extension DecodableProtoEnum {
54
85
// Note: Initializer 'init(from:)' must be declared public because it matches a requirement in
55
86
// public protocol 'Decodable'.
@@ -73,14 +104,12 @@ extension DecodableProtoEnum {
73
104
}
74
105
}
75
106
76
- /// Default implementation of `init(kind: Kind)` for types conforming to `DecodableProtoEnum`.
77
- extension DecodableProtoEnum {
78
- init ( kind: Kind ) {
79
- self = Self ( rawValue: kind. rawValue)
107
+ // Default `Encodable` implementation for types conforming to `EncodableProtoEnum`.
108
+ extension EncodableProtoEnum {
109
+ // Note: Method 'encode(to:)' must be declared public because it matches a requirement in public
110
+ // protocol 'Encodable'.
111
+ public func encode( to encoder: any Encoder ) throws {
112
+ var container = encoder. singleValueContainer ( )
113
+ try container. encode ( rawValue)
80
114
}
81
115
}
82
-
83
- /// A type that can be decoded and encoded from a Protocol Buffer raw enum value.
84
- ///
85
- /// See ``DecodableProtoEnum`` for more details.
86
- protocol CodableProtoEnum : DecodableProtoEnum , Encodable { }
0 commit comments