From 76500b3778310a707b2c71738a4779a72d16c4fe Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Fri, 7 May 2021 15:45:32 +0900 Subject: [PATCH] shared(prompts): simplify cidr selection code --- shared/src/prompts.rs | 7 +++---- shared/src/types.rs | 6 ++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/shared/src/prompts.rs b/shared/src/prompts.rs index 3f4d4f7..630b5e9 100644 --- a/shared/src/prompts.rs +++ b/shared/src/prompts.rs @@ -76,16 +76,15 @@ pub fn add_cidr(cidrs: &[Cidr], request: &AddCidrOpts) -> Result(cidrs: &'a [Cidr], text: &'static str) -> Result<&'a Cidr, Error> { - let cidr_names: Vec<_> = cidrs + let eligible_cidrs: Vec<_> = cidrs .iter() .filter(|cidr| cidr.name != "innernet-server") - .map(|cidr| format!("{} ({})", &cidr.name, &cidr.cidr)) .collect(); let cidr_index = Select::with_theme(&*THEME) .with_prompt(text) - .items(&cidr_names) + .items(&eligible_cidrs) .interact()?; - Ok(&cidrs[cidr_index]) + Ok(&eligible_cidrs[cidr_index]) } pub fn choose_association<'a>( diff --git a/shared/src/types.rs b/shared/src/types.rs index 2072dc3..d7f9d0f 100644 --- a/shared/src/types.rs +++ b/shared/src/types.rs @@ -199,6 +199,12 @@ pub struct Cidr { pub contents: CidrContents, } +impl Display for Cidr { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{} ({})", self.name, self.cidr) + } +} + impl Deref for Cidr { type Target = CidrContents;