Rework referencing

This commit is contained in:
Samuel Lorch 2023-11-07 22:24:34 +01:00
parent 4f7ab04fce
commit 2ea55de2cc
5 changed files with 26 additions and 22 deletions

View file

@ -15,7 +15,9 @@ pub trait Referenceable<T> {
#[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<T> {
#[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<String> for $thing {
fn into(self) -> String {
self.name
}
}
impl From<String> 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())
}
}
};

View file

@ -16,10 +16,7 @@ pub struct Network {
pub static_routes: Vec<StaticRoute>,
}
type NetworkInterfaces = Vec<NetworkInterface>;
impl_referenceable_trait!(NetworkInterfaces, NetworkInterface);
pub type NetworkInterfaceReference = String;
impl_references_trait!(
NetworkInterfaceReference,
NetworkInterface,

View file

@ -15,10 +15,7 @@ pub struct Object {
pub services: Services,
}
type Addresses = Vec<Address>;
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<AddressReference> },
}
type Services = Vec<Service>;
impl_referenceable_trait!(Services, Service);
pub type ServiceReference = String;
impl_references_trait!(ServiceReference, Service, object.services);
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]

View file

@ -12,10 +12,7 @@ pub struct System {
pub users: Vec<User>,
}
type Users = Vec<User>;
impl_referenceable_trait!(Users, User);
pub type UserReference = String;
impl_references_trait!(UserReference, User, system.users);
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]

View file

@ -18,10 +18,7 @@ pub struct Wireguard {
pub peers: WireguardPeers,
}
type WireguardInterfaces = Vec<WireguardInterface>;
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<WireguardPeer>;
impl_referenceable_trait!(WireguardPeers, WireguardPeer);
type WireguardPeerReference = String;
impl_references_trait!(WireguardPeerReference, WireguardPeer, vpn.wireguard.peers);
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]