confluent_kafka/common/quota/
client_quota_alteration.rs1use crate::common::quota::ClientQuotaEntity;
20
21#[derive(Clone, Debug)]
28#[doc(alias = "org.apache.kafka.common.quota.ClientQuotaAlteration$Op")]
29pub struct Op {
30 key: String,
31 value: Option<f64>,
32}
33
34fn double_to_long_bits(value: f64) -> u64 {
37 if value.is_nan() {
38 f64::NAN.to_bits()
39 } else {
40 value.to_bits()
41 }
42}
43
44impl PartialEq for Op {
45 fn eq(&self, other: &Self) -> bool {
46 self.key == other.key && self.value.map(double_to_long_bits) == other.value.map(double_to_long_bits)
47 }
48}
49
50impl Eq for Op {}
51
52impl std::hash::Hash for Op {
53 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
54 self.key.hash(state);
55 self.value.map(double_to_long_bits).hash(state);
56 }
57}
58
59impl Op {
60 #[doc(alias = "org.apache.kafka.common.quota.ClientQuotaAlteration$Op#Op")]
68 pub fn new(key: impl Into<String>, value: Option<f64>) -> Self {
69 Self { key: key.into(), value }
70 }
71
72 #[doc(alias = "org.apache.kafka.common.quota.ClientQuotaAlteration$Op#key")]
76 pub fn key(&self) -> &str {
77 &self.key
78 }
79
80 #[doc(alias = "org.apache.kafka.common.quota.ClientQuotaAlteration$Op#value")]
85 pub fn value(&self) -> Option<f64> {
86 self.value
87 }
88}
89
90impl std::fmt::Display for Op {
91 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92 match self.value {
93 Some(v) => write!(f, "ClientQuotaAlteration.Op(key={}, value={v})", self.key),
94 None => write!(f, "ClientQuotaAlteration.Op(key={}, value=null)", self.key),
95 }
96 }
97}
98
99#[derive(Clone, Debug)]
101#[doc(alias = "org.apache.kafka.common.quota.ClientQuotaAlteration")]
102pub struct ClientQuotaAlteration {
103 entity: ClientQuotaEntity,
104 ops: Vec<Op>,
105}
106
107impl ClientQuotaAlteration {
108 #[doc(alias = "org.apache.kafka.common.quota.ClientQuotaAlteration#ClientQuotaAlteration")]
115 pub fn new(entity: ClientQuotaEntity, ops: Vec<Op>) -> Self {
116 Self { entity, ops }
117 }
118
119 #[doc(alias = "org.apache.kafka.common.quota.ClientQuotaAlteration#entity")]
123 pub fn entity(&self) -> &ClientQuotaEntity {
124 &self.entity
125 }
126
127 #[doc(alias = "org.apache.kafka.common.quota.ClientQuotaAlteration#ops")]
131 pub fn ops(&self) -> &[Op] {
132 &self.ops
133 }
134}
135
136impl std::fmt::Display for ClientQuotaAlteration {
137 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
138 write!(f, "ClientQuotaAlteration(entity={}, ops={:?})", self.entity, self.ops)
139 }
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145 use std::collections::HashMap;
146
147 fn entity() -> ClientQuotaEntity {
148 let mut m = HashMap::new();
149 m.insert(ClientQuotaEntity::USER.to_string(), Some("u1".to_string()));
150 ClientQuotaEntity::new(m)
151 }
152
153 #[test]
156 fn op_set_vs_remove() {
157 let set = Op::new("consumer_byte_rate", Some(10000.0));
158 assert_eq!(set.key(), "consumer_byte_rate");
159 assert_eq!(set.value(), Some(10000.0));
160
161 let remove = Op::new("producer_byte_rate", None);
163 assert_eq!(remove.value(), None);
164 }
165
166 #[test]
168 fn op_equality() {
169 assert_eq!(Op::new("k", Some(1.0)), Op::new("k", Some(1.0)));
170 assert_ne!(Op::new("k", Some(1.0)), Op::new("k", None));
171 }
172
173 #[test]
176 fn op_compares_its_value_as_java_double_equals() {
177 use std::hash::BuildHasher;
178 let other_nan = f64::from_bits(f64::NAN.to_bits() ^ 1);
179 assert!(other_nan.is_nan());
180 let a = Op::new("k", Some(f64::NAN));
181 let b = Op::new("k", Some(other_nan));
182 assert_eq!(a, b);
183 let state = std::collections::hash_map::RandomState::new();
184 assert_eq!(state.hash_one(&a), state.hash_one(&b));
185 assert_ne!(Op::new("k", Some(0.0)), Op::new("k", Some(-0.0)));
186 }
187
188 #[test]
190 fn alteration_accessors() {
191 let alt = ClientQuotaAlteration::new(entity(), vec![Op::new("consumer_byte_rate", Some(1.0))]);
192 assert_eq!(alt.entity(), &entity());
193 assert_eq!(alt.ops().len(), 1);
194 }
195}