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, PolicyPrefixSegment,
12    PolicySrv6Action, PolicyTerm, RouteFilter, ValidationDatabaseInstance, ValidationDatabaseMatch,
13};
14
15use super::defaults::{parse_defaults, PolicyDefaults};
16use crate::{active_children, has_leaf, named_children};
17use asterust::config::ConfigLine;
18use term::parse_term;
19
20/// A `policy-statement <name>`. An unnamed term is placed last.
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct PolicyStatement {
24    pub name: String,
25    pub terms: Vec<PolicyTerm>,
26    /// The statement's own `defaults`.
27    pub defaults: Option<PolicyDefaults>,
28    /// `dynamic-db`: the policy is read from the dynamic database.
29    pub dynamic_db: bool,
30}
31
32pub(super) fn parse_policy(name: &str, line: &ConfigLine) -> PolicyStatement {
33    let mut terms: Vec<PolicyTerm> = named_children(line, "term")
34        .map(|(term_name, term)| parse_term(Some(term_name), term))
35        .collect();
36    let has_unnamed_term = active_children(line).any(|c| {
37        ["from", "to", "then"]
38            .iter()
39            .any(|k| c.text == *k || c.arg(k).is_some())
40    });
41    if has_unnamed_term {
42        terms.push(parse_term(None, line));
43    }
44    PolicyStatement {
45        name: name.to_string(),
46        terms,
47        defaults: parse_defaults(line),
48        dynamic_db: has_leaf(line, "dynamic-db"),
49    }
50}