Skip to main content

asterust_junos/policy_options/policy_statement/
mod.rs

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