Convert maps to vecs with name field

This commit is contained in:
Samuel Lorch 2023-11-06 02:34:34 +01:00
parent f5eb03cb16
commit 568d8cac5c
15 changed files with 305 additions and 316 deletions

View file

@ -5,3 +5,17 @@ pub mod object;
pub mod service;
pub mod system;
pub mod vpn;
#[macro_export]
macro_rules! get_thing {
($out:ty, $n:ident) => {
pub fn $n(list: Vec<$out>, name: String) -> Option<$out> {
for e in list {
if e.name == name {
return Some(e);
}
}
None
}
};
}

View file

@ -1,22 +1,27 @@
use ipnet::IpNet;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, net::IpAddr};
use std::net::IpAddr;
use validator::Validate;
use crate::get_thing;
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
pub struct Network {
pub interfaces: HashMap<String, NetworkInterface>,
pub interfaces: Vec<NetworkInterface>,
pub static_routes: Vec<StaticRoute>,
}
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
pub struct NetworkInterface {
pub name: String,
pub alias: String,
pub comment: String,
pub interface_type: NetworkInterfaceType,
pub addressing_mode: AddressingMode,
}
get_thing!(NetworkInterface, get_network_interface);
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub enum NetworkInterfaceType {

View file

@ -1,20 +1,25 @@
use ipnet::IpNet;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, net::IpAddr};
use std::net::IpAddr;
use validator::Validate;
use crate::get_thing;
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
pub struct Object {
pub addresses: HashMap<String, Address>,
pub services: HashMap<String, Service>,
pub addresses: Vec<Address>,
pub services: Vec<Service>,
}
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
pub struct Address {
pub name: String,
pub address_type: AddressType,
pub comment: String,
}
get_thing!(Address, get_address);
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub enum AddressType {
@ -26,10 +31,13 @@ pub enum AddressType {
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
pub struct Service {
pub name: String,
pub service_type: ServiceType,
pub comment: String,
}
get_thing!(Service, get_service);
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum ServiceType {

View file

@ -1,14 +1,18 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use validator::Validate;
use crate::get_thing;
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
pub struct System {
pub users: HashMap<String, User>,
pub users: Vec<User>,
}
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
pub struct User {
pub name: String,
pub comment: String,
pub hash: String,
}
get_thing!(User, get_user);

View file

@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use validator::Validate;
use crate::get_thing;
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
pub struct VPN {
pub wireguard: Wireguard,
@ -9,12 +10,13 @@ pub struct VPN {
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
pub struct Wireguard {
pub interfaces: HashMap<String, WireguardInterface>,
pub peers: HashMap<String, WireguardPeer>,
pub interfaces: Vec<WireguardInterface>,
pub peers: Vec<WireguardPeer>,
}
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
pub struct WireguardInterface {
pub name: String,
pub public_key: String,
pub private_key: String,
pub listen_port: u64,
@ -22,8 +24,11 @@ pub struct WireguardInterface {
pub comment: String,
}
get_thing!(WireguardInterface, get_wireguard_interface);
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
pub struct WireguardPeer {
pub name: String,
pub public_key: String,
pub preshared_key: Option<String>,
pub allowed_ips: Vec<String>,
@ -31,3 +36,5 @@ pub struct WireguardPeer {
pub persistent_keepalive: Option<u64>,
pub comment: String,
}
get_thing!(WireguardPeer, get_wireguard_peer);