shared(prompts): simplify cidr selection code

pull/71/head
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> { 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() .iter()
.filter(|cidr| cidr.name != "innernet-server") .filter(|cidr| cidr.name != "innernet-server")
.map(|cidr| format!("{} ({})", &cidr.name, &cidr.cidr))
.collect(); .collect();
let cidr_index = Select::with_theme(&*THEME) let cidr_index = Select::with_theme(&*THEME)
.with_prompt(text) .with_prompt(text)
.items(&cidr_names) .items(&eligible_cidrs)
.interact()?; .interact()?;
Ok(&cidrs[cidr_index]) Ok(&eligible_cidrs[cidr_index])
} }
pub fn choose_association<'a>( pub fn choose_association<'a>(

View File

@ -199,6 +199,12 @@ pub struct Cidr {
pub contents: CidrContents, 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 { impl Deref for Cidr {
type Target = CidrContents; type Target = CidrContents;