Skip to main content

kafka_client_engine/admin/group_offsets/
model.rs

1//! Engine-owned scalar intent for singular and batched consumer-group queries.
2
3use kafka_client_core::{
4    ListConsumerGroupOffsetTarget as CoreTarget, ListConsumerGroupOffsetsPlan,
5    ListConsumerGroupOffsetsPlanError, ListConsumerGroupOffsetsQuery as CoreQuery,
6    ListConsumerGroupOffsetsSelection as CoreSelection,
7};
8
9/// One selected topic-partition whose committed offset should be returned.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct ListConsumerGroupOffsetTarget {
12    topic: String,
13    partition: i32,
14}
15
16impl ListConsumerGroupOffsetTarget {
17    /// Creates one inert target for validation at the submission boundary.
18    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/// Partition selection retained by one inert consumer-group offset query.
38#[derive(Clone, Debug, Eq, PartialEq)]
39pub enum ListConsumerGroupOffsetsSelection {
40    /// Requests every committed offset visible for the group.
41    All,
42    /// Requests exactly the caller-ordered topic-partitions.
43    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/// One explicit group identity and its per-group offset selection.
88#[derive(Clone, Debug, Eq, PartialEq)]
89pub struct ListConsumerGroupOffsetsQuery {
90    group_id: String,
91    selection: ListConsumerGroupOffsetsSelection,
92}
93
94impl ListConsumerGroupOffsetsQuery {
95    /// Creates an all-partition query for one group.
96    pub const fn all(group_id: String) -> Self {
97        Self {
98            group_id,
99            selection: ListConsumerGroupOffsetsSelection::All,
100        }
101    }
102
103    /// Creates an exact caller-ordered partition query for one group.
104    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/// One query for an explicit consumer group.
128#[derive(Clone, Debug, Eq, PartialEq)]
129pub struct ListConsumerGroupOffsetsRequest {
130    query: ListConsumerGroupOffsetsQuery,
131    require_stable: bool,
132}
133
134impl ListConsumerGroupOffsetsRequest {
135    /// Creates one inert all-partition request for validation at admission.
136    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    /// Creates one inert request with an explicit per-group selection.
144    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/// Caller-ordered per-group offset queries for multiple consumer groups.
172#[derive(Clone, Debug, Eq, PartialEq)]
173pub struct ListConsumerGroupsOffsetsRequest {
174    queries: Vec<ListConsumerGroupOffsetsQuery>,
175    require_stable: bool,
176}
177
178impl ListConsumerGroupsOffsetsRequest {
179    /// Creates one inert all-partition batch for validation at admission.
180    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    /// Creates one inert batch retaining each group's exact selection.
191    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}