cli: Order displayed feature list by status (#21810)

This commit is contained in:
Justin Starry 2021-12-13 07:42:57 -05:00 committed by GitHub
parent 90f41fd9b7
commit 1149c1880d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 3 deletions

View File

@ -45,7 +45,7 @@ pub enum FeatureCliCommand {
}, },
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase", tag = "status", content = "sinceSlot")] #[serde(rename_all = "camelCase", tag = "status", content = "sinceSlot")]
pub enum CliFeatureStatus { pub enum CliFeatureStatus {
Inactive, Inactive,
@ -53,7 +53,29 @@ pub enum CliFeatureStatus {
Active(Slot), Active(Slot),
} }
#[derive(Serialize, Deserialize)] impl PartialOrd for CliFeatureStatus {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CliFeatureStatus {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Self::Inactive, Self::Inactive) => Ordering::Equal,
(Self::Inactive, _) => Ordering::Greater,
(_, Self::Inactive) => Ordering::Less,
(Self::Pending, Self::Pending) => Ordering::Equal,
(Self::Pending, _) => Ordering::Greater,
(_, Self::Pending) => Ordering::Less,
(Self::Active(self_active_slot), Self::Active(other_active_slot)) => {
self_active_slot.cmp(other_active_slot)
}
}
}
}
#[derive(Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CliFeature { pub struct CliFeature {
pub id: String, pub id: String,
@ -62,6 +84,21 @@ pub struct CliFeature {
pub status: CliFeatureStatus, pub status: CliFeatureStatus,
} }
impl PartialOrd for CliFeature {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CliFeature {
fn cmp(&self, other: &Self) -> Ordering {
match self.status.cmp(&other.status) {
Ordering::Equal => self.id.cmp(&other.id),
ordering => ordering,
}
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CliFeatures { pub struct CliFeatures {
@ -93,7 +130,7 @@ impl fmt::Display for CliFeatures {
CliFeatureStatus::Inactive => style("inactive".to_string()).red(), CliFeatureStatus::Inactive => style("inactive".to_string()).red(),
CliFeatureStatus::Pending => style("activation pending".to_string()).yellow(), CliFeatureStatus::Pending => style("activation pending".to_string()).yellow(),
CliFeatureStatus::Active(activation_slot) => CliFeatureStatus::Active(activation_slot) =>
style(format!("active since slot {}", activation_slot)).green(), style(format!("active since slot {:>9}", activation_slot)).green(),
}, },
feature.description, feature.description,
)?; )?;
@ -550,6 +587,8 @@ fn process_status(
}); });
} }
features.sort_unstable();
let feature_activation_allowed = feature_activation_allowed(rpc_client, features.len() <= 1)?; let feature_activation_allowed = feature_activation_allowed(rpc_client, features.len() <= 1)?;
let feature_set = CliFeatures { let feature_set = CliFeatures {
features, features,