From 2ea55de2ccdbee50b659bbb24ed97d858ca51037 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Tue, 7 Nov 2023 22:24:34 +0100 Subject: [PATCH] Rework referencing --- src/definitions/mod.rs | 30 ++++++++++++++++++++++++++---- src/definitions/network.rs | 3 --- src/definitions/object.rs | 6 ------ src/definitions/system.rs | 3 --- src/definitions/vpn.rs | 6 ------ 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/definitions/mod.rs b/src/definitions/mod.rs index 2676e6f..3b4df82 100644 --- a/src/definitions/mod.rs +++ b/src/definitions/mod.rs @@ -15,7 +15,9 @@ pub trait Referenceable { #[macro_export] macro_rules! impl_referenceable_trait { - ($typ:ty, $ele:ty) => { + ($typ:ident, $ele:ty) => { + pub type $typ = Vec<$ele>; + impl Referenceable<$ele> for $typ { fn named_get(&self, name: String) -> $ele { let index = self.iter().position(|e| *e.name == name); @@ -42,14 +44,34 @@ pub trait References { #[macro_export] macro_rules! impl_references_trait { - ($thing:ty, $referenced:ty, $( $path:ident ).+) => { + ($thing:ident, $referenced:ty, $( $path:ident ).+) => { + + #[derive(Serialize, Deserialize, Clone, Default, Debug)] + #[serde(from = "String")] + #[serde(into = "String")] + pub struct $thing { + pub name: String, + } + + impl Into for $thing { + fn into(self) -> String { + self.name + } + } + + impl From for $thing { + fn from(value: String) -> Self { + $thing { name: value } + } + } + impl References<$referenced> for $thing { fn get_ref(&self, config: Config) -> $referenced { - config.$($path).+.named_get(self.clone()) + config.$($path).+.named_get(self.clone().into()) } fn ref_exists(&self, config: Config) -> bool { - config.$($path).+.named_exists(self.clone()) + config.$($path).+.named_exists(self.clone().into()) } } }; diff --git a/src/definitions/network.rs b/src/definitions/network.rs index 5fea499..72d82d2 100644 --- a/src/definitions/network.rs +++ b/src/definitions/network.rs @@ -16,10 +16,7 @@ pub struct Network { pub static_routes: Vec, } -type NetworkInterfaces = Vec; impl_referenceable_trait!(NetworkInterfaces, NetworkInterface); - -pub type NetworkInterfaceReference = String; impl_references_trait!( NetworkInterfaceReference, NetworkInterface, diff --git a/src/definitions/object.rs b/src/definitions/object.rs index 54291a7..cf5863a 100644 --- a/src/definitions/object.rs +++ b/src/definitions/object.rs @@ -15,10 +15,7 @@ pub struct Object { pub services: Services, } -type Addresses = Vec
; impl_referenceable_trait!(Addresses, Address); - -pub type AddressReference = String; impl_references_trait!(AddressReference, Address, object.addresses); #[derive(Serialize, Deserialize, Clone, Validate, Debug)] @@ -37,10 +34,7 @@ pub enum AddressType { Group { members: Vec }, } -type Services = Vec; impl_referenceable_trait!(Services, Service); - -pub type ServiceReference = String; impl_references_trait!(ServiceReference, Service, object.services); #[derive(Serialize, Deserialize, Clone, Validate, Debug)] diff --git a/src/definitions/system.rs b/src/definitions/system.rs index 3e02ec2..33278ff 100644 --- a/src/definitions/system.rs +++ b/src/definitions/system.rs @@ -12,10 +12,7 @@ pub struct System { pub users: Vec, } -type Users = Vec; impl_referenceable_trait!(Users, User); - -pub type UserReference = String; impl_references_trait!(UserReference, User, system.users); #[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)] diff --git a/src/definitions/vpn.rs b/src/definitions/vpn.rs index af43036..42b007c 100644 --- a/src/definitions/vpn.rs +++ b/src/definitions/vpn.rs @@ -18,10 +18,7 @@ pub struct Wireguard { pub peers: WireguardPeers, } -type WireguardInterfaces = Vec; impl_referenceable_trait!(WireguardInterfaces, WireguardInterface); - -pub type WireguardInterfaceReference = String; impl_references_trait!( WireguardInterfaceReference, WireguardInterface, @@ -38,10 +35,7 @@ pub struct WireguardInterface { pub comment: String, } -pub type WireguardPeers = Vec; impl_referenceable_trait!(WireguardPeers, WireguardPeer); - -type WireguardPeerReference = String; impl_references_trait!(WireguardPeerReference, WireguardPeer, vpn.wireguard.peers); #[derive(Serialize, Deserialize, Clone, Validate, Debug)]