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, 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}
19
20/// A `community` statement written either on one line (`community X members 1:1;`) or as a block.
21pub(super) fn parse_community(line: &ConfigLine) -> Option<Community> {
22    let (name, rest) = split_name(line.arg("community")?);
23    let (keyword, members) = split_keyword(rest);
24    let mut community = Community {
25        name: name.to_string(),
26        members: if keyword == "members" {
27            list_values(members)
28        } else {
29            Vec::new()
30        },
31        invert_match: false,
32    };
33    for child in active_children(line) {
34        if let Some(members) = child.arg("members") {
35            community.members.extend(list_values(members));
36        } else if child.text == "invert-match" {
37            community.invert_match = true;
38        }
39    }
40    Some(community)
41}