Skip to main content

asterust_junos/policy_options/
community.rs

1//! `community`
2//! `policy-options { community <name> members ...; }`.
3//!
4//! # See also
5//!
6//! - [community (Policy Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/community-edit-policy-options.html)
7
8use crate::{active_children, has_leaf, list_values, split_keyword, split_name};
9use asterust::config::ConfigLine;
10
11/// A `community <name> members ...`, which may be a standard, extended or large community.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct Community {
15    pub name: String,
16    pub members: Vec<String>,
17    pub invert_match: bool,
18    /// `dynamic-db`: the community is read from the dynamic database.
19    pub dynamic_db: bool,
20}
21
22/// A `community` statement written either on one line (`community X members 1:1;`) or as a block.
23pub(super) fn parse_community(line: &ConfigLine) -> Option<Community> {
24    let (name, rest) = split_name(line.arg("community")?);
25    let (keyword, members) = split_keyword(rest);
26    let mut community = Community {
27        name: name.to_string(),
28        members: if keyword == "members" {
29            list_values(members)
30        } else {
31            Vec::new()
32        },
33        invert_match: false,
34        dynamic_db: has_leaf(line, "dynamic-db"),
35    };
36    for child in active_children(line) {
37        if let Some(members) = child.arg("members") {
38            community.members.extend(list_values(members));
39        } else if child.text == "invert-match" {
40            community.invert_match = true;
41        }
42    }
43    Some(community)
44}