Skip to content

Commit d3cc0a2

Browse files
committed
refactor: Use enum instead of bool
1 parent df8f5cc commit d3cc0a2

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

crates/stackable-operator/src/role_utils.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -415,20 +415,23 @@ where
415415
/// This is the case when all `replicas` are set to [`Some<u16>`], in which case they are simply
416416
/// summed.
417417
///
418-
/// The argument `treat_zero_as_none` is a safety mechanism, which allows the caller to decide
419-
/// if an explicit replica count of `0` should be treated as [`None`]. It also means that
418+
/// The argument `zero_replicas_counting` is a safety mechanism, which allows the caller to
419+
/// decide if an explicit replica count of `0` should be treated as [`None`]. It also means that
420420
/// [`None`] is returned in case no roleGroups are configured at all.
421-
pub fn fixed_replica_count(&self, treat_zero_as_none: bool) -> Option<u32> {
421+
pub fn fixed_replica_count(
422+
&self,
423+
zero_replicas_counting: ZeroReplicasCounting,
424+
) -> Option<u32> {
422425
// An empty role has no fixed replica count when zeros are treated as None.
423-
if treat_zero_as_none && self.role_groups.is_empty() {
426+
if zero_replicas_counting == ZeroReplicasCounting::TreatAsNone && self.role_groups.is_empty() {
424427
return None;
425428
}
426429

427430
self.role_groups
428431
.values()
429432
.map(|rg| match rg.replicas {
430433
None => None,
431-
Some(0) if treat_zero_as_none => None,
434+
Some(0) if zero_replicas_counting == ZeroReplicasCounting::TreatAsNone => None,
432435
// The individual replicas are [`u16`]s, so a [`u32`] sum has plenty of space.
433436
Some(replicas) => Some(u32::from(replicas)),
434437
})
@@ -448,6 +451,15 @@ where
448451
}
449452
}
450453

454+
/// How explicit zero (`0`) replicas on a role group should be counted
455+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
456+
pub enum ZeroReplicasCounting {
457+
/// Treat them as what they are: `Some(0)`.
458+
TreatAsZero,
459+
/// Treat them as if the user configured [`None`].
460+
TreatAsNone,
461+
}
462+
451463
impl<Config, ConfigOverrides, RoleConfig>
452464
Role<Config, ConfigOverrides, RoleConfig, JavaCommonConfig>
453465
where
@@ -704,45 +716,45 @@ mod tests {
704716
fn replica_counts_with_all_replicas_set() {
705717
let role = construct_role_with_replicas([Some(3), Some(2), Some(5)]);
706718

707-
assert_eq!(role.fixed_replica_count(false), Some(10));
708-
assert_eq!(role.fixed_replica_count(true), Some(10));
719+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsZero), Some(10));
720+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsNone), Some(10));
709721
assert_eq!(role.estimated_replica_count(), 10);
710722
}
711723

712724
#[test]
713725
fn replica_counts_with_one_replica_unset() {
714726
let role = construct_role_with_replicas([Some(3), None, Some(2)]);
715727

716-
assert_eq!(role.fixed_replica_count(false), None);
717-
assert_eq!(role.fixed_replica_count(true), None);
728+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsZero), None);
729+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsNone), None);
718730
assert_eq!(role.estimated_replica_count(), 6);
719731
}
720732

721733
#[test]
722734
fn replica_counts_with_a_zero_replica() {
723735
let role = construct_role_with_replicas([Some(3), Some(0)]);
724736

725-
assert_eq!(role.fixed_replica_count(false), Some(3));
737+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsZero), Some(3));
726738
// With treat_zero_as_none the zero turns the whole count into None.
727-
assert_eq!(role.fixed_replica_count(true), None);
739+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsNone), None);
728740
assert_eq!(role.estimated_replica_count(), 3);
729741
}
730742

731743
#[test]
732744
fn replica_counts_with_a_single_zero_role_group() {
733745
let role = construct_role_with_replicas([Some(0)]);
734746

735-
assert_eq!(role.fixed_replica_count(false), Some(0));
736-
assert_eq!(role.fixed_replica_count(true), None);
747+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsZero), Some(0));
748+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsNone), None);
737749
assert_eq!(role.estimated_replica_count(), 0);
738750
}
739751

740752
#[test]
741753
fn replica_counts_without_role_groups() {
742754
let role = construct_role_with_replicas(vec![]);
743755

744-
assert_eq!(role.fixed_replica_count(false), Some(0));
745-
assert_eq!(role.fixed_replica_count(true), None);
756+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsZero), Some(0));
757+
assert_eq!(role.fixed_replica_count(ZeroReplicasCounting::TreatAsNone), None);
746758
assert_eq!(role.estimated_replica_count(), 0);
747759
}
748760

0 commit comments

Comments
 (0)