Skip to main content

asterust_junos/firewall/
policer.rs

1//! `policer`
2//! `firewall { policer <name> { ... } }`.
3//!
4//! # See also
5//!
6//! - [policer (Configuring)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/policer-edit-firewall.html)
7
8use crate::{arg_of, child_block, split_keyword, statements, unquote};
9use asterust::config::ConfigLine;
10
11/// A `policer <name>`.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct Policer {
15    pub name: String,
16    pub bandwidth_limit: Option<String>,
17    pub bandwidth_percent: Option<String>,
18    pub burst_size_limit: Option<String>,
19    /// `then discard`.
20    pub discard: bool,
21    pub forwarding_class: Option<String>,
22    pub loss_priority: Option<String>,
23}
24
25pub(super) fn parse_policer(name: &str, line: &ConfigLine) -> Policer {
26    let limit = |keyword: &str| {
27        child_block(line, "if-exceeding")
28            .and_then(|block| arg_of(block, keyword))
29            .map(str::to_string)
30    };
31    let mut policer = Policer {
32        name: name.to_string(),
33        bandwidth_limit: limit("bandwidth-limit"),
34        bandwidth_percent: limit("bandwidth-percent"),
35        burst_size_limit: limit("burst-size-limit"),
36        discard: false,
37        forwarding_class: None,
38        loss_priority: None,
39    };
40    for statement in statements(line, "then") {
41        let (action, arg) = split_keyword(statement);
42        match action {
43            "discard" => policer.discard = true,
44            "forwarding-class" => policer.forwarding_class = Some(unquote(arg).to_string()),
45            "loss-priority" => policer.loss_priority = Some(unquote(arg).to_string()),
46            _ => {}
47        }
48    }
49    policer
50}