Skip to main content

kafka_client_engine/admin/list_offsets/
observer.rs

1//! Named runtime-neutral observation of one Admin `ListOffsets` terminal.
2
3use std::{
4    fmt,
5    future::Future,
6    pin::Pin,
7    task::{Context, Poll},
8};
9
10use kafka_client_core::AdminListOffsetsTerminal;
11
12use crate::completion::{CompletionObserver, CompletionObserverError};
13
14use super::{AdminListOffsetsObserverError, AdminListOffsetsOutcome, outcome::translate_terminal};
15
16/// Single observer for one accepted Admin `ListOffsets` query.
17#[must_use = "dropping abandons observation without cancelling accepted admin work"]
18pub struct AdminListOffsetsObserver {
19    inner: CompletionObserver<AdminListOffsetsTerminal>,
20}
21
22impl AdminListOffsetsObserver {
23    pub(crate) const fn from_completion(
24        inner: CompletionObserver<AdminListOffsetsTerminal>,
25    ) -> Self {
26        Self { inner }
27    }
28
29    /// Blocks on the same terminal cell used by `Future::poll`.
30    pub fn wait(self) -> Result<AdminListOffsetsOutcome, AdminListOffsetsObserverError> {
31        self.inner
32            .wait()
33            .map(translate_terminal)
34            .map_err(observer_error)
35    }
36}
37
38impl Future for AdminListOffsetsObserver {
39    type Output = Result<AdminListOffsetsOutcome, AdminListOffsetsObserverError>;
40
41    fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
42        let this = self.get_mut();
43        Pin::new(&mut this.inner)
44            .poll(context)
45            .map(|result| result.map(translate_terminal).map_err(observer_error))
46    }
47}
48
49impl fmt::Debug for AdminListOffsetsObserver {
50    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
51        formatter
52            .debug_struct("AdminListOffsetsObserver")
53            .finish_non_exhaustive()
54    }
55}
56
57const fn observer_error(error: CompletionObserverError) -> AdminListOffsetsObserverError {
58    match error {
59        CompletionObserverError::AlreadyObserved => AdminListOffsetsObserverError::AlreadyObserved,
60        CompletionObserverError::Stale => AdminListOffsetsObserverError::Stale,
61    }
62}