Skip to main content

asterust_junos/policy_options/policy_statement/
mod.rs

1//! `policy-options { policy-statement <name> { ... } }`.
2
3mod term;
4
5pub use term::{
6    CommunityAction, CommunityOperation, PolicyAction, PolicyActions, PolicyConditions, PolicyTerm, RouteFilter,
7};
8
9use crate::{active_children, named_children};
10use asterust::config::ConfigLine;
11use term::parse_term;
12
13/// A `policy-statement <name>`. An unnamed term is placed last.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct PolicyStatement {
17    pub name: String,
18    pub terms: Vec<PolicyTerm>,
19}
20
21pub(super) fn parse_policy(name: &str, line: &ConfigLine) -> PolicyStatement {
22    let mut terms: Vec<PolicyTerm> = named_children(line, "term")
23        .map(|(term_name, term)| parse_term(Some(term_name), term))
24        .collect();
25    let has_unnamed_term = active_children(line).any(|c| {
26        ["from", "to", "then"]
27            .iter()
28            .any(|k| c.text == *k || c.arg(k).is_some())
29    });
30    if has_unnamed_term {
31        terms.push(parse_term(None, line));
32    }
33    PolicyStatement {
34        name: name.to_string(),
35        terms,
36    }
37}