Skip to main content

network_device/
iac.rs

1//! Declarative desired-state resources and IaC backend contracts.
2
3use crate::{AdministrativeState, InterfaceName, StaticRoute};
4use std::error::Error;
5use std::future::Future;
6
7/// A declarative desired configuration for an interface.
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub struct InterfaceConfiguration {
10    /// Interface to configure.
11    pub name: InterfaceName,
12    /// Desired administrative state, when specified.
13    pub administrative_state: Option<AdministrativeState>,
14    /// Desired description, when specified.
15    pub description: Option<String>,
16}
17
18impl InterfaceConfiguration {
19    /// Starts a desired interface configuration.
20    pub fn new(name: InterfaceName) -> Self {
21        Self {
22            name,
23            administrative_state: None,
24            description: None,
25        }
26    }
27
28    /// Sets the desired administrative state.
29    pub fn administrative_state(mut self, state: AdministrativeState) -> Self {
30        self.administrative_state = Some(state);
31        self
32    }
33
34    /// Sets the desired description.
35    pub fn description(mut self, description: impl Into<String>) -> Self {
36        self.description = Some(description.into());
37        self
38    }
39}
40
41/// A declarative desired configuration for a static route.
42#[derive(Clone, Copy, Debug, Eq, PartialEq)]
43pub struct StaticRouteConfiguration {
44    /// Desired route.
45    pub route: StaticRoute,
46}
47
48impl StaticRouteConfiguration {
49    /// Creates the desired state for a static route.
50    pub const fn new(route: StaticRoute) -> Self {
51        Self { route }
52    }
53}
54
55/// Plans and applies a resource's declarative desired state.
56///
57/// Each resource type has its own implementation, keeping this contract open
58/// to resources added by downstream crates. It intentionally promises only
59/// plan and apply; backend-specific concepts remain in backend crates.
60pub trait IacBackend<Resource: Sync>: Send + Sync {
61    /// Error type returned by this backend.
62    type Error: Error + Send + Sync + 'static;
63    /// Backend-specific plan representation.
64    type Plan: Send;
65    /// Backend-specific apply result.
66    type ApplyResult: Send;
67
68    /// Computes a plan for the desired resource.
69    #[allow(clippy::manual_async_fn)]
70    fn plan<'a>(
71        &'a self,
72        desired: &'a Resource,
73    ) -> impl Future<Output = Result<Self::Plan, Self::Error>> + Send + 'a;
74
75    /// Applies the desired resource.
76    #[allow(clippy::manual_async_fn)]
77    fn apply<'a>(
78        &'a self,
79        desired: &'a Resource,
80    ) -> impl Future<Output = Result<Self::ApplyResult, Self::Error>> + Send + 'a;
81}