use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GoalType {
ReplicaDistribution,
LeaderDistribution,
RackAware,
}
impl GoalType {
pub fn to_i32(self) -> i32 {
match self {
Self::ReplicaDistribution => 0,
Self::LeaderDistribution => 1,
Self::RackAware => 2,
}
}
pub fn try_from_i32(value: i32) -> Result<Self> {
match value {
0 => Ok(Self::ReplicaDistribution),
1 => Ok(Self::LeaderDistribution),
2 => Ok(Self::RackAware),
_ => Err(Error::IllegalArgument {
message: format!("Unsupported GoalType: {value}"),
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_goal_type_roundtrip() {
for goal in [
GoalType::ReplicaDistribution,
GoalType::LeaderDistribution,
GoalType::RackAware,
] {
assert_eq!(GoalType::try_from_i32(goal.to_i32()).unwrap(), goal);
}
}
#[test]
fn test_goal_type_unknown() {
assert!(GoalType::try_from_i32(99).is_err());
}
}