shared(types): CidrTree: re-add with_root, rewrite leaves()

pull/121/head
Jake McGinty 2021-06-23 20:47:53 +09:00
parent 62e6464064
commit 4afa8a8a4e
1 changed files with 8 additions and 6 deletions

View File

@ -237,6 +237,10 @@ impl<'a> CidrTree<'a> {
.iter() .iter()
.min_by_key(|c| c.cidr.prefix()) .min_by_key(|c| c.cidr.prefix())
.expect("failed to find root CIDR"); .expect("failed to find root CIDR");
Self::with_root(cidrs, root)
}
pub fn with_root(cidrs: &'a [Cidr], root: &'a Cidr) -> Self {
Self { Self {
cidrs, cidrs,
contents: root, contents: root,
@ -254,14 +258,12 @@ impl<'a> CidrTree<'a> {
} }
pub fn leaves(&self) -> Vec<Cidr> { pub fn leaves(&self) -> Vec<Cidr> {
let mut leaves = vec![]; if !self.cidrs.iter().any(|cidr| cidr.parent == Some(self.id)) {
for cidr in self.cidrs { vec![self.contents.clone()]
if !self.cidrs.iter().any(|c| c.parent == Some(cidr.id)) { } else {
leaves.push(cidr.clone()); self.children().flat_map(|child| child.leaves()).collect()
} }
} }
leaves
}
} }
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]