Skip to main content

asterust_junos/routing_options/static_routes/
route.rs

1//! `route`
2//! `routing-options { static { route <prefix> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [static (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-edit-routing-options.html)
7//! - [discard (routing-options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/discard-edit-routing-options.html)
8
9use super::qualified_next_hop::{parse_qualified_next_hop, QualifiedNextHop};
10use crate::routing_options::route_attributes::{parse_route_attributes, RouteAttributes};
11use crate::{active_children, list_values, split_keyword, split_name, unquote};
12use asterust::config::ConfigLine;
13
14/// A static `route <prefix>`. The next hop is one of `next_hops`, `discard`, `reject`, `receive`, `next_table` or
15/// `qualified_next_hops`.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct StaticRoute {
19    pub prefix: String,
20    pub next_hops: Vec<String>,
21    pub discard: bool,
22    pub reject: bool,
23    pub receive: bool,
24    pub next_table: Option<String>,
25    pub qualified_next_hops: Vec<QualifiedNextHop>,
26    pub attributes: RouteAttributes,
27}
28
29/// Reads a route written on one line (`route 10.0.0.0/8 discard;`) or as a block.
30pub(super) fn parse_route(arg: &str, line: &ConfigLine) -> StaticRoute {
31    let (prefix, inline) = split_name(arg);
32    let children: Vec<&ConfigLine> = active_children(line).collect();
33
34    // `route <prefix> qualified-next-hop <next-hop> { ... }` keeps the settings in the route's own block.
35    let mut statements: Vec<(&str, Vec<&ConfigLine>)> = Vec::new();
36    if inline.starts_with("qualified-next-hop") {
37        statements.push((inline, children));
38    } else {
39        if !inline.is_empty() {
40            statements.push((inline, Vec::new()));
41        }
42        statements.extend(
43            children
44                .into_iter()
45                .map(|child| (child.text.as_str(), active_children(child).collect())),
46        );
47    }
48
49    let mut route = StaticRoute {
50        prefix: prefix.to_string(),
51        next_hops: Vec::new(),
52        discard: false,
53        reject: false,
54        receive: false,
55        next_table: None,
56        qualified_next_hops: Vec::new(),
57        attributes: RouteAttributes::default(),
58    };
59    let mut attribute_statements = Vec::new();
60    for (text, children) in &statements {
61        let (keyword, rest) = split_keyword(text);
62        match keyword {
63            "next-hop" => route.next_hops.extend(list_values(rest)),
64            "discard" => route.discard = true,
65            "reject" => route.reject = true,
66            "receive" => route.receive = true,
67            "next-table" => route.next_table = Some(unquote(rest).to_string()),
68            "qualified-next-hop" => route.qualified_next_hops.push(parse_qualified_next_hop(rest, children)),
69            _ => attribute_statements.push(*text),
70        }
71    }
72    route.attributes = parse_route_attributes(attribute_statements);
73    route
74}