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