Skip to main content

kafka_client_engine/admin/update_features/
model.rs

1//! Engine-owned inert intent for Admin `UpdateFeatures`.
2
3use kafka_client_core::{
4    UpdateFeature as CoreFeature, UpdateFeatureIntent as CoreIntent,
5    UpdateFeaturesPlan as CorePlan, UpdateFeaturesPlanError as CorePlanError,
6};
7
8pub(crate) enum UpdateFeaturesPlanFailure {
9    Invalid,
10    RetainedBytes,
11}
12
13/// Explicit direction and loss policy for one finalized-feature update.
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15pub enum UpdateFeatureIntent {
16    /// Raises or establishes a finalized feature level.
17    Upgrade,
18    /// Permits only a broker-classified lossless downgrade or deletion.
19    SafeDowngrade,
20    /// Explicitly permits a potentially lossy downgrade or deletion.
21    UnsafeDowngrade,
22}
23
24/// One inert finalized-feature target in caller order.
25#[derive(Clone, Debug, Eq, PartialEq)]
26pub struct UpdateFeature {
27    feature: String,
28    max_version_level: i16,
29    intent: UpdateFeatureIntent,
30}
31
32impl UpdateFeature {
33    /// Creates inert update data validated only after deadline capture.
34    pub const fn new(feature: String, max_version_level: i16, intent: UpdateFeatureIntent) -> Self {
35        Self {
36            feature,
37            max_version_level,
38            intent,
39        }
40    }
41
42    /// Returns the exact finalized-feature name.
43    pub fn feature(&self) -> &str {
44        &self.feature
45    }
46
47    /// Returns the requested finalized maximum level.
48    pub const fn max_version_level(&self) -> i16 {
49        self.max_version_level
50    }
51
52    /// Returns the explicit direction and loss policy.
53    pub const fn intent(&self) -> UpdateFeatureIntent {
54        self.intent
55    }
56
57    fn to_core(&self) -> CoreFeature {
58        CoreFeature::new(
59            self.feature.clone().into_boxed_str().into_string(),
60            self.max_version_level,
61            match self.intent {
62                UpdateFeatureIntent::Upgrade => CoreIntent::Upgrade,
63                UpdateFeatureIntent::SafeDowngrade => CoreIntent::SafeDowngrade,
64                UpdateFeatureIntent::UnsafeDowngrade => CoreIntent::UnsafeDowngrade,
65            },
66        )
67    }
68}
69
70/// One inert caller-ordered finalized-feature update request.
71#[derive(Clone, Debug, Eq, PartialEq)]
72pub struct UpdateFeaturesRequest {
73    updates: Vec<UpdateFeature>,
74    validate_only: bool,
75}
76
77impl UpdateFeaturesRequest {
78    /// Creates inert intent. Validation remains deferred until submission.
79    pub const fn new(updates: Vec<UpdateFeature>, validate_only: bool) -> Self {
80        Self {
81            updates,
82            validate_only,
83        }
84    }
85
86    /// Returns finalized-feature updates in caller order.
87    pub fn updates(&self) -> &[UpdateFeature] {
88        &self.updates
89    }
90
91    /// Returns whether Kafka should validate without mutating feature state.
92    pub const fn validate_only(&self) -> bool {
93        self.validate_only
94    }
95
96    pub(crate) fn plan(&self) -> Result<CorePlan, UpdateFeaturesPlanFailure> {
97        let mut updates = Vec::new();
98        updates
99            .try_reserve_exact(self.updates.len())
100            .map_err(|_| UpdateFeaturesPlanFailure::RetainedBytes)?;
101        updates.extend(self.updates.iter().map(UpdateFeature::to_core));
102        CorePlan::new(updates, self.validate_only)
103            .map_err(|_error: CorePlanError| UpdateFeaturesPlanFailure::Invalid)
104    }
105}