mirror of
https://github.com/tonarino/innernet.git
synced 2024-11-10 17:04:53 +08:00
shared(prompts): simplify cidr selection code
This commit is contained in:
parent
f27a2426c8
commit
76500b3778
2 changed files with 9 additions and 4 deletions
|
@ -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>(
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue