Skip to main content

kafka_client_engine/admin/
observer.rs

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