Skip to main content

kafka_client_engine/admin/partitions/
observer.rs

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