shared(prompts): simplify cidr selection code

This commit is contained in:
Jake McGinty 2021-05-07 15:45:32 +09:00
parent f27a2426c8
commit 76500b3778
2 changed files with 9 additions and 4 deletions

View file

@ -76,16 +76,15 @@ pub fn add_cidr(cidrs: &[Cidr], request: &AddCidrOpts) -> Result<Option<CidrCont
}
pub fn choose_cidr<'a>(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>(

View file

@ -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;