Skip to main content

asterust_junos/policy_options/
prefix_list.rs

1//! `prefix-list`
2//! `policy-options { prefix-list <name> { ... } }`.
3//!
4//! # See also
5//!
6//! - [prefix-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/prefix-list-edit-policy-options.html)
7
8use crate::{active_children, has_leaf, unquote};
9use asterust::config::ConfigLine;
10
11/// A `prefix-list <name>`.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct PrefixList {
15    pub name: String,
16    pub prefixes: Vec<String>,
17    pub apply_path: Option<String>,
18    /// `dynamic-db`: the list is read from the dynamic database.
19    pub dynamic_db: bool,
20}
21
22pub(super) fn parse_prefix_list(name: &str, line: &ConfigLine) -> PrefixList {
23    let mut list = PrefixList {
24        name: name.to_string(),
25        prefixes: Vec::new(),
26        apply_path: None,
27        dynamic_db: has_leaf(line, "dynamic-db"),
28    };
29    for child in active_children(line) {
30        if let Some(path) = child.arg("apply-path") {
31            list.apply_path = Some(unquote(path).to_string());
32        } else if !child.text.contains(' ') {
33            // Prefixes are the only statements without arguments; `apply-groups` and the like have a space.
34            list.prefixes.push(child.text.clone());
35        }
36    }
37    list
38}