network_device/
operation.rs1use crate::{InterfaceName, StaticRoute};
4use std::error::Error;
5use std::future::Future;
6
7pub trait Driver: Send + Sync {
9 type Error: Error + Send + Sync + 'static;
11}
12
13pub trait Execute<Operation: Send>: Driver {
18 type Output: Send;
20
21 #[allow(clippy::manual_async_fn)]
23 fn execute<'a>(
24 &'a self,
25 operation: Operation,
26 ) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send + 'a;
27}
28
29#[derive(Clone, Debug, Eq, PartialEq)]
31pub struct OperationStep<Operation> {
32 operation: Operation,
33}
34
35pub const fn operation<Operation>(operation: Operation) -> OperationStep<Operation> {
37 OperationStep { operation }
38}
39
40impl<Operation> OperationStep<Operation> {
41 pub fn then<Next>(self, next: Next) -> Sequence<Self, Next> {
43 Sequence {
44 first: self,
45 second: next,
46 }
47 }
48}
49
50#[derive(Clone, Debug, Eq, PartialEq)]
52pub struct Sequence<First, Second> {
53 first: First,
54 second: Second,
55}
56
57impl<First, Second> Sequence<First, Second> {
58 pub fn then<Next>(self, next: Next) -> Sequence<Self, Next> {
60 Sequence {
61 first: self,
62 second: next,
63 }
64 }
65}
66
67pub trait Run<DriverType: Driver>: Send {
69 type Output: Send;
71
72 #[allow(clippy::manual_async_fn)]
74 fn run<'a>(
75 self,
76 driver: &'a DriverType,
77 ) -> impl Future<Output = Result<Self::Output, DriverType::Error>> + Send + 'a
78 where
79 Self: 'a,
80 DriverType: 'a;
81}
82
83impl<DriverType, Operation> Run<DriverType> for OperationStep<Operation>
84where
85 DriverType: Execute<Operation>,
86 Operation: Send,
87{
88 type Output = <DriverType as Execute<Operation>>::Output;
89
90 #[allow(clippy::manual_async_fn)]
91 fn run<'a>(
92 self,
93 driver: &'a DriverType,
94 ) -> impl Future<Output = Result<Self::Output, DriverType::Error>> + Send + 'a
95 where
96 Self: 'a,
97 DriverType: 'a,
98 {
99 async move { driver.execute(self.operation).await }
100 }
101}
102
103impl<DriverType, First, Second> Run<DriverType> for Sequence<First, Second>
104where
105 DriverType: Driver,
106 First: Run<DriverType>,
107 Second: Run<DriverType>,
108{
109 type Output = Second::Output;
110
111 #[allow(clippy::manual_async_fn)]
112 fn run<'a>(
113 self,
114 driver: &'a DriverType,
115 ) -> impl Future<Output = Result<Self::Output, DriverType::Error>> + Send + 'a
116 where
117 Self: 'a,
118 DriverType: 'a,
119 {
120 async move {
121 self.first.run(driver).await?;
122 self.second.run(driver).await
123 }
124 }
125}
126
127#[derive(Clone, Copy, Debug, Eq, PartialEq)]
129pub enum AdministrativeState {
130 Enabled,
132 Disabled,
134}
135
136#[derive(Clone, Debug, Eq, PartialEq)]
138pub struct SetInterfaceState {
139 pub interface: InterfaceName,
141 pub state: AdministrativeState,
143}
144
145impl SetInterfaceState {
146 pub fn new(interface: InterfaceName, state: AdministrativeState) -> Self {
148 Self { interface, state }
149 }
150}
151
152#[derive(Clone, Debug, Eq, PartialEq)]
154pub struct SetInterfaceDescription {
155 pub interface: InterfaceName,
157 pub description: String,
159}
160
161impl SetInterfaceDescription {
162 pub fn new(interface: InterfaceName, description: impl Into<String>) -> Self {
164 Self {
165 interface,
166 description: description.into(),
167 }
168 }
169}
170
171#[derive(Clone, Copy, Debug, Eq, PartialEq)]
173pub struct EnsureStaticRoute {
174 pub route: StaticRoute,
176}
177
178impl EnsureStaticRoute {
179 pub const fn new(route: StaticRoute) -> Self {
181 Self { route }
182 }
183}
184
185#[derive(Clone, Copy, Debug, Eq, PartialEq)]
187pub struct RemoveStaticRoute {
188 pub route: StaticRoute,
190}
191
192impl RemoveStaticRoute {
193 pub const fn new(route: StaticRoute) -> Self {
195 Self { route }
196 }
197}