Skip to main content

kafka_client_engine/
delivery_error.rs

1//! Engine-owned terminal delivery and observer-lifecycle failure vocabulary.
2
3use core::fmt;
4
5use crate::completion::CompletionObserverError;
6
7use super::ProducerDeliveryFailure;
8
9/// Failure in the single-observer completion lifecycle.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum ProducerObserverError {
12    /// The terminal value was already taken by this observer.
13    AlreadyObserved,
14    /// The observer's completion generation is no longer live.
15    Stale,
16    /// A producer terminal reached an observer for another operation kind.
17    TerminalTypeMismatch,
18}
19
20impl ProducerObserverError {
21    pub(crate) const fn from_completion(error: CompletionObserverError) -> Self {
22        match error {
23            CompletionObserverError::AlreadyObserved => Self::AlreadyObserved,
24            CompletionObserverError::Stale => Self::Stale,
25        }
26    }
27}
28
29impl fmt::Display for ProducerObserverError {
30    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
31        formatter.write_str(match self {
32            Self::AlreadyObserved => "producer delivery was already observed",
33            Self::Stale => "producer delivery observer is stale",
34            Self::TerminalTypeMismatch => "producer observer received the wrong terminal type",
35        })
36    }
37}
38
39impl std::error::Error for ProducerObserverError {}
40
41/// Terminal producer failure or failure to observe its completion cell.
42#[derive(Clone, Copy, Debug, Eq, PartialEq)]
43pub enum ProducerDeliveryError {
44    /// Kafka delivery ended with semantic failure and delivery certainty.
45    Failed(ProducerDeliveryFailure),
46    /// The single-observer lifecycle rejected observation.
47    Observer(ProducerObserverError),
48}
49
50impl fmt::Display for ProducerDeliveryError {
51    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            Self::Failed(failure) => {
54                write!(formatter, "producer delivery failed: {:?}", failure.kind())
55            }
56            Self::Observer(error) => error.fmt(formatter),
57        }
58    }
59}
60
61impl std::error::Error for ProducerDeliveryError {
62    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
63        match self {
64            Self::Failed(_) => None,
65            Self::Observer(error) => Some(error),
66        }
67    }
68}