use crate::{active_children, list_values, split_keyword, unquote};
use asterust::config::ConfigLine;
pub(super) fn route_statements<'a>(inline: &'a str, line: &'a ConfigLine) -> Vec<&'a str> {
let mut statements = Vec::new();
if !inline.is_empty() {
statements.push(inline);
}
statements.extend(active_children(line).map(|child| child.text.as_str()));
statements
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RouteAttributes {
pub active: Option<bool>,
pub install: Option<bool>,
pub readvertise: Option<bool>,
pub resolve: Option<bool>,
pub retain: Option<bool>,
pub longest_match: Option<bool>,
pub preference: Option<String>,
pub preference2: Option<String>,
pub metric: Option<String>,
pub metric2: Option<String>,
pub tag: Option<String>,
pub tag2: Option<String>,
pub color: Option<String>,
pub color2: Option<String>,
pub community: Vec<String>,
pub description: Option<String>,
pub priority: Option<String>,
pub backup_pe_group: Option<String>,
}
pub(super) fn parse_route_attributes<'a>(statements: impl IntoIterator<Item = &'a str>) -> RouteAttributes {
let mut attributes = RouteAttributes::default();
for statement in statements {
let (keyword, arg) = split_keyword(statement);
let value = || Some(arg.to_string());
match keyword {
"active" => attributes.active = Some(true),
"passive" => attributes.active = Some(false),
"install" => attributes.install = Some(true),
"no-install" => attributes.install = Some(false),
"readvertise" => attributes.readvertise = Some(true),
"no-readvertise" => attributes.readvertise = Some(false),
"resolve" => attributes.resolve = Some(true),
"no-resolve" => attributes.resolve = Some(false),
"retain" => attributes.retain = Some(true),
"no-retain" => attributes.retain = Some(false),
"longest-match" => attributes.longest_match = Some(true),
"no-longest-match" => attributes.longest_match = Some(false),
"preference" => attributes.preference = value(),
"preference2" => attributes.preference2 = value(),
"metric" => attributes.metric = value(),
"metric2" => attributes.metric2 = value(),
"tag" => attributes.tag = value(),
"tag2" => attributes.tag2 = value(),
"color" => attributes.color = value(),
"color2" => attributes.color2 = value(),
"community" => attributes.community.extend(list_values(arg)),
"description" => attributes.description = Some(unquote(arg).to_string()),
"priority" => attributes.priority = value(),
"backup-pe-group" => attributes.backup_pe_group = value(),
_ => {}
}
}
attributes
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_flags_pairs_and_values() {
let attributes = parse_route_attributes([
"passive",
"no-install",
"readvertise",
"no-resolve",
"retain",
"no-longest-match",
"preference 5 type 2",
"metric 3",
"tag 7",
"color 100",
"community [ 65000:1 no-export ]",
"description \"to branch\"",
"priority high",
]);
assert_eq!(attributes.active, Some(false));
assert_eq!(attributes.install, Some(false));
assert_eq!(attributes.readvertise, Some(true));
assert_eq!(attributes.resolve, Some(false));
assert_eq!(attributes.retain, Some(true));
assert_eq!(attributes.longest_match, Some(false));
assert_eq!(attributes.preference.as_deref(), Some("5 type 2"));
assert_eq!(attributes.metric.as_deref(), Some("3"));
assert_eq!(attributes.tag.as_deref(), Some("7"));
assert_eq!(attributes.color.as_deref(), Some("100"));
assert_eq!(attributes.community, ["65000:1", "no-export"]);
assert_eq!(attributes.description.as_deref(), Some("to branch"));
assert_eq!(attributes.priority.as_deref(), Some("high"));
}
#[test]
fn ignores_statements_that_are_not_attributes() {
let attributes = parse_route_attributes(["next-hop 192.0.2.1", "discard"]);
assert_eq!(attributes, RouteAttributes::default());
}
}