use crate::{arg_of, child_block, has_leaf, list_values, statement_words, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DuplicateProtection {
pub include_relay_session_id: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ShortCycleProtection {
pub lockout_time_min: Option<String>,
pub lockout_time_max: Option<String>,
pub filter: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PppoeUnderlyingOptions {
pub access_concentrator: Option<String>,
pub dynamic_profile: Option<String>,
pub direct_connect: bool,
pub duplicate_protection: Option<DuplicateProtection>,
pub max_sessions: Option<String>,
pub max_sessions_vsa_ignore: bool,
pub service_name_table: Option<String>,
pub short_cycle_protection: Option<ShortCycleProtection>,
}
pub(super) fn parse_pppoe_underlying_options(options: &ConfigLine) -> PppoeUnderlyingOptions {
let value = |keyword| arg_of(options, keyword).map(str::to_string);
PppoeUnderlyingOptions {
access_concentrator: value("access-concentrator"),
dynamic_profile: value("dynamic-profile"),
direct_connect: has_leaf(options, "direct-connect"),
duplicate_protection: crate::active_children(options)
.find(|c| c.text == "duplicate-protection" || c.text.starts_with("duplicate-protection "))
.map(|protection| DuplicateProtection {
include_relay_session_id: child_block(protection, "include")
.is_some_and(|include| has_leaf(include, "relay-session-id")),
}),
max_sessions: value("max-sessions"),
max_sessions_vsa_ignore: has_leaf(options, "max-sessions-vsa-ignore"),
service_name_table: value("service-name-table"),
short_cycle_protection: statement_words(options, "short-cycle-protection").map(|words| ShortCycleProtection {
lockout_time_min: value_after(&words, "lockout-time-min"),
lockout_time_max: value_after(&words, "lockout-time-max"),
filter: words
.iter()
.position(|w| *w == "filter")
.map(|i| list_values(&words[i + 1..].join(" ")))
.unwrap_or_default(),
}),
}
}