move definitions to seperate files

This commit is contained in:
Samuel Lorch 2023-10-20 23:35:05 +02:00
parent 4fdfb151de
commit e67c7679a2
4 changed files with 128 additions and 112 deletions

12
src/definitions.rs Normal file
View file

@ -0,0 +1,12 @@
pub mod network;
pub mod object;
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Serialize, Deserialize, Validate, Default, Debug)]
pub struct Config {
pub config_version: u64,
pub network: network::Network,
pub object: object::Object,
}

View file

@ -0,0 +1,44 @@
use ipnet::IpNet;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, net::IpAddr};
use validator::Validate;
#[derive(Serialize, Deserialize, Validate, Default, Debug)]
pub struct Network {
pub interfaces: HashMap<String, NetworkInterface>,
pub static_routes: Vec<StaticRoute>,
}
#[derive(Serialize, Deserialize, Validate, Debug)]
pub struct NetworkInterface {
pub alias: String,
pub comment: String,
pub interface_type: NetworkInterfaceType,
pub addressing_mode: AddressingMode,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum NetworkInterfaceType {
Hardware { device: String },
Vlan { id: i32, parent: String },
Bond { members: Vec<String> },
Bridge { members: Vec<String> },
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum AddressingMode {
None,
Static { address: String },
DHCP,
}
#[derive(Serialize, Deserialize, Validate, Debug)]
pub struct StaticRoute {
pub name: String,
pub interface: String,
pub gateway: IpAddr,
pub destination: IpNet,
pub metric: u64,
}

54
src/definitions/object.rs Normal file
View file

@ -0,0 +1,54 @@
use ipnet::IpNet;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, net::IpAddr};
use validator::Validate;
#[derive(Serialize, Deserialize, Validate, Default, Debug)]
pub struct Object {
pub addresses: HashMap<String, Address>,
pub services: HashMap<String, Service>,
}
#[derive(Serialize, Deserialize, Validate, Debug)]
pub struct Address {
pub address_type: AddressType,
pub comment: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum AddressType {
Host { host: String },
Range { range: IpAddr },
Network { network: IpNet },
Group { children: Vec<String> },
}
#[derive(Serialize, Deserialize, Validate, Debug)]
pub struct Service {
pub service_type: ServiceType,
pub comment: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum ServiceType {
TCP {
source_port: u64,
source_port_end: Option<u64>,
destination_port: u64,
destination_port_end: Option<u64>,
},
UDP {
source_port: u64,
source_port_end: Option<u64>,
destination_port: u64,
destination_port_end: Option<u64>,
},
ICMP {
code: u8,
},
Group {
children: Vec<String>,
},
}

View file

@ -1,105 +1,8 @@
#![allow(dead_code)]
use ipnet::IpNet;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, net::IpAddr};
use validator::Validate;
mod definitions;
#[derive(Serialize, Deserialize, Validate, Default, Debug)]
struct Config {
config_version: u64,
network: Network,
}
#[derive(Serialize, Deserialize, Validate, Default, Debug)]
struct Network {
interfaces: HashMap<String, NetworkInterface>,
static_routes: Vec<StaticRoute>,
}
#[derive(Serialize, Deserialize, Validate, Debug)]
struct NetworkInterface {
alias: String,
comment: String,
interface_type: NetworkInterfaceType,
addressing_mode: AddressingMode,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
enum NetworkInterfaceType {
Hardware { device: String },
Vlan { id: i32, parent: String },
Bond { members: Vec<String> },
Bridge { members: Vec<String> },
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
enum AddressingMode {
None,
Static { address: String },
DHCP,
}
#[derive(Serialize, Deserialize, Validate, Debug)]
struct StaticRoute {
name: String,
interface: String,
gateway: IpAddr,
destination: IpNet,
metric: u64,
}
#[derive(Serialize, Deserialize, Validate, Default, Debug)]
struct Object {
addresses: HashMap<String, Address>,
services: HashMap<String, Service>,
}
#[derive(Serialize, Deserialize, Validate, Debug)]
struct Address {
address_type: AddressType,
comment: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
enum AddressType {
Host { host: String },
Range { range: IpAddr },
Network { network: IpNet },
Group { children: Vec<String> },
}
#[derive(Serialize, Deserialize, Validate, Debug)]
struct Service {
service_type: ServiceType,
comment: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
enum ServiceType {
TCP {
source_port: u64,
source_port_end: Option<u64>,
destination_port: u64,
destination_port_end: Option<u64>,
},
UDP {
source_port: u64,
source_port_end: Option<u64>,
destination_port: u64,
destination_port_end: Option<u64>,
},
ICMP {
code: u8,
},
Group {
children: Vec<String>,
},
}
use definitions::Config;
fn main() {
println!("Hello, world!");
@ -109,37 +12,40 @@ fn main() {
config.network.interfaces.insert(
"inter1".to_string(),
NetworkInterface {
definitions::network::NetworkInterface {
alias: "test".to_owned(),
comment: "test comment".to_owned(),
interface_type: NetworkInterfaceType::Hardware {
interface_type: definitions::network::NetworkInterfaceType::Hardware {
device: "eth0".to_owned(),
},
addressing_mode: AddressingMode::None,
addressing_mode: definitions::network::AddressingMode::None,
},
);
config.network.interfaces.insert(
"inter2".to_string(),
NetworkInterface {
definitions::network::NetworkInterface {
alias: "test2".to_owned(),
comment: "test comment".to_owned(),
interface_type: NetworkInterfaceType::Hardware {
interface_type: definitions::network::NetworkInterfaceType::Hardware {
device: "eth0".to_owned(),
},
addressing_mode: AddressingMode::Static {
addressing_mode: definitions::network::AddressingMode::Static {
address: "192.168.1.1".to_owned(),
},
},
);
config.network.static_routes.push(StaticRoute {
name: "test1".to_string(),
interface: "eth0".to_string(),
gateway: "192.168.1.1".parse().unwrap(),
destination: "10.42.42.0/24".parse().unwrap(),
metric: 0,
});
config
.network
.static_routes
.push(definitions::network::StaticRoute {
name: "test1".to_string(),
interface: "eth0".to_string(),
gateway: "192.168.1.1".parse().unwrap(),
destination: "10.42.42.0/24".parse().unwrap(),
metric: 0,
});
let serialized = serde_json::to_string_pretty(&config).unwrap();
println!("serialized = {}", serialized);