mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-13 11:38:21 +00:00
Rework referencing
This commit is contained in:
parent
4f7ab04fce
commit
2ea55de2cc
5 changed files with 26 additions and 22 deletions
|
@ -15,7 +15,9 @@ pub trait Referenceable<T> {
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! impl_referenceable_trait {
|
macro_rules! impl_referenceable_trait {
|
||||||
($typ:ty, $ele:ty) => {
|
($typ:ident, $ele:ty) => {
|
||||||
|
pub type $typ = Vec<$ele>;
|
||||||
|
|
||||||
impl Referenceable<$ele> for $typ {
|
impl Referenceable<$ele> for $typ {
|
||||||
fn named_get(&self, name: String) -> $ele {
|
fn named_get(&self, name: String) -> $ele {
|
||||||
let index = self.iter().position(|e| *e.name == name);
|
let index = self.iter().position(|e| *e.name == name);
|
||||||
|
@ -42,14 +44,34 @@ pub trait References<T> {
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! impl_references_trait {
|
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 {
|
impl References<$referenced> for $thing {
|
||||||
fn get_ref(&self, config: Config) -> $referenced {
|
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 {
|
fn ref_exists(&self, config: Config) -> bool {
|
||||||
config.$($path).+.named_exists(self.clone())
|
config.$($path).+.named_exists(self.clone().into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,10 +16,7 @@ pub struct Network {
|
||||||
pub static_routes: Vec<StaticRoute>,
|
pub static_routes: Vec<StaticRoute>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type NetworkInterfaces = Vec<NetworkInterface>;
|
|
||||||
impl_referenceable_trait!(NetworkInterfaces, NetworkInterface);
|
impl_referenceable_trait!(NetworkInterfaces, NetworkInterface);
|
||||||
|
|
||||||
pub type NetworkInterfaceReference = String;
|
|
||||||
impl_references_trait!(
|
impl_references_trait!(
|
||||||
NetworkInterfaceReference,
|
NetworkInterfaceReference,
|
||||||
NetworkInterface,
|
NetworkInterface,
|
||||||
|
|
|
@ -15,10 +15,7 @@ pub struct Object {
|
||||||
pub services: Services,
|
pub services: Services,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Addresses = Vec<Address>;
|
|
||||||
impl_referenceable_trait!(Addresses, Address);
|
impl_referenceable_trait!(Addresses, Address);
|
||||||
|
|
||||||
pub type AddressReference = String;
|
|
||||||
impl_references_trait!(AddressReference, Address, object.addresses);
|
impl_references_trait!(AddressReference, Address, object.addresses);
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||||
|
@ -37,10 +34,7 @@ pub enum AddressType {
|
||||||
Group { members: Vec<AddressReference> },
|
Group { members: Vec<AddressReference> },
|
||||||
}
|
}
|
||||||
|
|
||||||
type Services = Vec<Service>;
|
|
||||||
impl_referenceable_trait!(Services, Service);
|
impl_referenceable_trait!(Services, Service);
|
||||||
|
|
||||||
pub type ServiceReference = String;
|
|
||||||
impl_references_trait!(ServiceReference, Service, object.services);
|
impl_references_trait!(ServiceReference, Service, object.services);
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||||
|
|
|
@ -12,10 +12,7 @@ pub struct System {
|
||||||
pub users: Vec<User>,
|
pub users: Vec<User>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Users = Vec<User>;
|
|
||||||
impl_referenceable_trait!(Users, User);
|
impl_referenceable_trait!(Users, User);
|
||||||
|
|
||||||
pub type UserReference = String;
|
|
||||||
impl_references_trait!(UserReference, User, system.users);
|
impl_references_trait!(UserReference, User, system.users);
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||||
|
|
|
@ -18,10 +18,7 @@ pub struct Wireguard {
|
||||||
pub peers: WireguardPeers,
|
pub peers: WireguardPeers,
|
||||||
}
|
}
|
||||||
|
|
||||||
type WireguardInterfaces = Vec<WireguardInterface>;
|
|
||||||
impl_referenceable_trait!(WireguardInterfaces, WireguardInterface);
|
impl_referenceable_trait!(WireguardInterfaces, WireguardInterface);
|
||||||
|
|
||||||
pub type WireguardInterfaceReference = String;
|
|
||||||
impl_references_trait!(
|
impl_references_trait!(
|
||||||
WireguardInterfaceReference,
|
WireguardInterfaceReference,
|
||||||
WireguardInterface,
|
WireguardInterface,
|
||||||
|
@ -38,10 +35,7 @@ pub struct WireguardInterface {
|
||||||
pub comment: String,
|
pub comment: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type WireguardPeers = Vec<WireguardPeer>;
|
|
||||||
impl_referenceable_trait!(WireguardPeers, WireguardPeer);
|
impl_referenceable_trait!(WireguardPeers, WireguardPeer);
|
||||||
|
|
||||||
type WireguardPeerReference = String;
|
|
||||||
impl_references_trait!(WireguardPeerReference, WireguardPeer, vpn.wireguard.peers);
|
impl_references_trait!(WireguardPeerReference, WireguardPeer, vpn.wireguard.peers);
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue