kafka_api/schemata/metadata_request.rs
1// Copyright 2024 tison <wander4096@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use byteorder::ReadBytesExt;
16
17use crate::codec::*;
18use crate::IoResult;
19
20// In version 0, an empty array indicates "request metadata for all topics." In version 1 and
21// higher, an empty array indicates "request metadata for no topics," and a null array is used to
22// indicate "request metadata for all topics."
23//
24// Version 2 and 3 are the same as version 1.
25//
26// Version 4 adds AllowAutoTopicCreation.
27//
28// Starting in version 8, authorized operations can be requested for cluster and topic resource.
29//
30// Version 9 is the first flexible version.
31//
32// Version 10 adds topicId and allows name field to be null. However, this functionality was not
33// implemented on the server. Versions 10 and 11 should not use the topicId field or set topic name
34// to null.
35//
36// Version 11 deprecates IncludeClusterAuthorizedOperations field. This is now exposed
37// by the DescribeCluster API (KIP-700).
38//
39// Version 12 supports topic ID.
40
41#[derive(Debug, Default, Clone)]
42pub struct MetadataRequest {
43 /// The topics to fetch metadata for.
44 pub topics: Option<Vec<MetadataRequestTopic>>,
45 /// If this is true, the broker may auto-create topics that we requested which do not already
46 /// exist, if it is configured to do so.
47 pub allow_auto_topic_creation: bool,
48 /// Whether to include cluster authorized operations.
49 pub include_cluster_authorized_operations: bool,
50 /// Whether to include topic authorized operations.
51 pub include_topic_authorized_operations: bool,
52 /// Unknown tagged fields.
53 pub unknown_tagged_fields: Vec<RawTaggedField>,
54}
55
56impl Decodable for MetadataRequest {
57 fn read<B: ReadBytesExt>(buf: &mut B, version: i16) -> IoResult<Self> {
58 let mut this = MetadataRequest::default();
59 if version >= 1 {
60 this.topics = NullableArray(Struct(version), version >= 9).decode(buf)?;
61 } else {
62 let topics = NullableArray(Struct(version), version >= 9)
63 .decode(buf)?
64 .ok_or_else(|| err_decode_message_null("topics"))?;
65 this.topics = if topics.is_empty() {
66 None
67 } else {
68 Some(topics)
69 };
70 }
71 if version >= 4 {
72 this.allow_auto_topic_creation = Bool.decode(buf)?;
73 } else {
74 this.allow_auto_topic_creation = true;
75 };
76 if (8..=10).contains(&version) {
77 this.include_cluster_authorized_operations = Bool.decode(buf)?;
78 }
79 if version >= 9 {
80 this.include_topic_authorized_operations = Bool.decode(buf)?;
81 }
82 if version >= 9 {
83 this.unknown_tagged_fields = RawTaggedFieldList.decode(buf)?;
84 }
85 Ok(this)
86 }
87}
88
89#[derive(Debug, Default, Clone)]
90pub struct MetadataRequestTopic {
91 /// The topic id.
92 pub topic_id: uuid::Uuid,
93 /// The topic name.
94 pub name: Option<String>,
95 /// Unknown tagged fields.
96 pub unknown_tagged_fields: Vec<RawTaggedField>,
97}
98
99impl Decodable for MetadataRequestTopic {
100 fn read<B: ReadBytesExt>(buf: &mut B, version: i16) -> IoResult<Self> {
101 if version > 12 {
102 Err(err_decode_message_unsupported(
103 version,
104 "MetadataRequestTopic",
105 ))?
106 }
107 let mut this = MetadataRequestTopic::default();
108 if version >= 10 {
109 this.topic_id = Uuid.decode(buf)?;
110 }
111 this.name = if version >= 10 {
112 NullableString(true).decode(buf)?
113 } else {
114 Some(
115 NullableString(version >= 9)
116 .decode(buf)?
117 .ok_or_else(|| err_decode_message_null("name"))?,
118 )
119 };
120 if version >= 9 {
121 this.unknown_tagged_fields = RawTaggedFieldList.decode(buf)?;
122 }
123 Ok(this)
124 }
125}