asterust-junos 0.5.1

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! Bundles one parsed Junos configuration with every supported view as a field.

use crate::class_of_service::{class_of_service, ClassOfService};
use crate::firewall::{firewall, Firewall};
use crate::interfaces::{interfaces, Interface};
use crate::policy_options::{policy_options, PolicyOptions};
use crate::protocols::{protocols, Protocols};
use crate::routing_instances::{routing_instances, RoutingInstance};
use crate::routing_options::{routing_options, RoutingOptions};
use crate::services::{services, Services};
use crate::system::{system, System};
use asterust::config::{juniper, Config};

/// Holds a parsed Junos `show configuration` (brace format) and every supported view of it, read once when the device
/// is built.
///
/// The fields mirror the configuration hierarchy: `protocols { bgp { ... } }` is `device.protocols.bgp`.
///
/// # Examples
///
/// ```
/// use asterust_junos::JunosDevice;
///
/// let device = JunosDevice::parse("system {\n    host-name pe1;\n}\n");
/// assert_eq!(device.system.unwrap().host_name.as_deref(), Some("pe1"));
/// assert!(device.protocols.bgp.is_none());
/// ```
#[derive(Clone, Debug)]
pub struct JunosDevice {
    config: Config,
    pub class_of_service: Option<ClassOfService>,
    pub interfaces: Vec<Interface>,
    pub protocols: Protocols,
    pub routing_instances: Vec<RoutingInstance>,
    pub routing_options: Option<RoutingOptions>,
    pub policy_options: Option<PolicyOptions>,
    pub firewall: Option<Firewall>,
    pub services: Option<Services>,
    pub system: Option<System>,
}

impl JunosDevice {
    /// Parses raw brace-format `show configuration` text.
    pub fn parse(config_text: &str) -> Self {
        Self::from_config(juniper::parse_braces(config_text))
    }

    /// Wraps an already-parsed [`Config`].
    pub fn from_config(config: Config) -> Self {
        Self {
            class_of_service: class_of_service(&config),
            interfaces: interfaces(&config),
            protocols: protocols(&config),
            routing_instances: routing_instances(&config),
            routing_options: routing_options(&config),
            policy_options: policy_options(&config),
            firewall: firewall(&config),
            services: services(&config),
            system: system(&config),
            config,
        }
    }

    /// Returns the underlying generic config tree.
    pub fn config(&self) -> &Config {
        &self.config
    }
}

impl asterust::device::Device for JunosDevice {
    fn parse(config_text: &str) -> Self {
        Self::parse(config_text)
    }

    fn from_config(config: Config) -> Self {
        Self::from_config(config)
    }

    fn config(&self) -> &Config {
        Self::config(self)
    }
}