kafka_client_engine/admin/group_offsets/
model.rs1use kafka_client_core::{
4 ListConsumerGroupOffsetTarget as CoreTarget, ListConsumerGroupOffsetsPlan,
5 ListConsumerGroupOffsetsPlanError, ListConsumerGroupOffsetsQuery as CoreQuery,
6 ListConsumerGroupOffsetsSelection as CoreSelection,
7};
8
9#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct ListConsumerGroupOffsetTarget {
12 topic: String,
13 partition: i32,
14}
15
16impl ListConsumerGroupOffsetTarget {
17 pub const fn new(topic: String, partition: i32) -> Self {
19 Self { topic, partition }
20 }
21
22 fn canonicalize(mut self) -> Self {
23 self.topic = self.topic.into_boxed_str().into_string();
24 self
25 }
26
27 fn into_core(self) -> CoreTarget {
28 CoreTarget::new(self.topic, self.partition)
29 }
30
31 #[cfg(test)]
32 fn storage_is_canonical(&self) -> bool {
33 self.topic.capacity() == self.topic.len()
34 }
35}
36
37#[derive(Clone, Debug, Eq, PartialEq)]
39pub enum ListConsumerGroupOffsetsSelection {
40 All,
42 Selected(Vec<ListConsumerGroupOffsetTarget>),
44}
45
46impl ListConsumerGroupOffsetsSelection {
47 fn canonicalize(self) -> Self {
48 match self {
49 Self::All => Self::All,
50 Self::Selected(targets) => Self::Selected(
51 targets
52 .into_iter()
53 .map(ListConsumerGroupOffsetTarget::canonicalize)
54 .collect::<Vec<_>>()
55 .into_boxed_slice()
56 .into_vec(),
57 ),
58 }
59 }
60
61 fn into_core(self) -> CoreSelection {
62 match self {
63 Self::All => CoreSelection::All,
64 Self::Selected(targets) => CoreSelection::Selected(
65 targets
66 .into_iter()
67 .map(ListConsumerGroupOffsetTarget::into_core)
68 .collect(),
69 ),
70 }
71 }
72
73 #[cfg(test)]
74 fn storage_is_canonical(&self) -> bool {
75 match self {
76 Self::All => true,
77 Self::Selected(targets) => {
78 targets.capacity() == targets.len()
79 && targets
80 .iter()
81 .all(ListConsumerGroupOffsetTarget::storage_is_canonical)
82 }
83 }
84 }
85}
86
87#[derive(Clone, Debug, Eq, PartialEq)]
89pub struct ListConsumerGroupOffsetsQuery {
90 group_id: String,
91 selection: ListConsumerGroupOffsetsSelection,
92}
93
94impl ListConsumerGroupOffsetsQuery {
95 pub const fn all(group_id: String) -> Self {
97 Self {
98 group_id,
99 selection: ListConsumerGroupOffsetsSelection::All,
100 }
101 }
102
103 pub const fn selected(group_id: String, targets: Vec<ListConsumerGroupOffsetTarget>) -> Self {
105 Self {
106 group_id,
107 selection: ListConsumerGroupOffsetsSelection::Selected(targets),
108 }
109 }
110
111 fn canonicalize(mut self) -> Self {
112 self.group_id = self.group_id.into_boxed_str().into_string();
113 self.selection = self.selection.canonicalize();
114 self
115 }
116
117 fn into_core(self) -> Result<CoreQuery, ListConsumerGroupOffsetsPlanError> {
118 CoreQuery::new(self.group_id, self.selection.into_core())
119 }
120
121 #[cfg(test)]
122 fn storage_is_canonical(&self) -> bool {
123 self.group_id.capacity() == self.group_id.len() && self.selection.storage_is_canonical()
124 }
125}
126
127#[derive(Clone, Debug, Eq, PartialEq)]
129pub struct ListConsumerGroupOffsetsRequest {
130 query: ListConsumerGroupOffsetsQuery,
131 require_stable: bool,
132}
133
134impl ListConsumerGroupOffsetsRequest {
135 pub const fn new(group_id: String, require_stable: bool) -> Self {
137 Self {
138 query: ListConsumerGroupOffsetsQuery::all(group_id),
139 require_stable,
140 }
141 }
142
143 pub const fn from_query(query: ListConsumerGroupOffsetsQuery, require_stable: bool) -> Self {
145 Self {
146 query,
147 require_stable,
148 }
149 }
150
151 pub(crate) fn canonicalize(mut self) -> Self {
152 self.query = self.query.canonicalize();
153 self
154 }
155
156 pub(crate) fn into_plan(
157 self,
158 ) -> Result<ListConsumerGroupOffsetsPlan, ListConsumerGroupOffsetsPlanError> {
159 Ok(ListConsumerGroupOffsetsPlan::from_query(
160 self.query.into_core()?,
161 self.require_stable,
162 ))
163 }
164
165 #[cfg(test)]
166 pub(super) fn storage_is_canonical(&self) -> bool {
167 self.query.storage_is_canonical()
168 }
169}
170
171#[derive(Clone, Debug, Eq, PartialEq)]
173pub struct ListConsumerGroupsOffsetsRequest {
174 queries: Vec<ListConsumerGroupOffsetsQuery>,
175 require_stable: bool,
176}
177
178impl ListConsumerGroupsOffsetsRequest {
179 pub fn new(group_ids: Vec<String>, require_stable: bool) -> Self {
181 Self {
182 queries: group_ids
183 .into_iter()
184 .map(ListConsumerGroupOffsetsQuery::all)
185 .collect(),
186 require_stable,
187 }
188 }
189
190 pub const fn from_queries(
192 queries: Vec<ListConsumerGroupOffsetsQuery>,
193 require_stable: bool,
194 ) -> Self {
195 Self {
196 queries,
197 require_stable,
198 }
199 }
200
201 pub(crate) fn canonicalize(mut self) -> Self {
202 self.queries = self
203 .queries
204 .into_iter()
205 .map(ListConsumerGroupOffsetsQuery::canonicalize)
206 .collect::<Vec<_>>()
207 .into_boxed_slice()
208 .into_vec();
209 self
210 }
211
212 pub(crate) fn into_plan(
213 self,
214 ) -> Result<ListConsumerGroupOffsetsPlan, ListConsumerGroupOffsetsPlanError> {
215 ListConsumerGroupOffsetsPlan::new_query_batch(
216 self.queries
217 .into_iter()
218 .map(ListConsumerGroupOffsetsQuery::into_core)
219 .collect::<Result<Vec<_>, _>>()?,
220 self.require_stable,
221 )
222 }
223
224 #[cfg(test)]
225 pub(super) fn storage_is_canonical(&self) -> bool {
226 self.queries.capacity() == self.queries.len()
227 && self
228 .queries
229 .iter()
230 .all(ListConsumerGroupOffsetsQuery::storage_is_canonical)
231 }
232}