From e67c7679a205dbb2a0e049a407a11f3d72f60f6a Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Fri, 20 Oct 2023 23:35:05 +0200 Subject: [PATCH] move definitions to seperate files --- src/definitions.rs | 12 ++++ src/definitions/network.rs | 44 +++++++++++++ src/definitions/object.rs | 54 +++++++++++++++ src/main.rs | 130 +++++-------------------------------- 4 files changed, 128 insertions(+), 112 deletions(-) create mode 100644 src/definitions.rs create mode 100644 src/definitions/network.rs create mode 100644 src/definitions/object.rs diff --git a/src/definitions.rs b/src/definitions.rs new file mode 100644 index 0000000..a1dc9c8 --- /dev/null +++ b/src/definitions.rs @@ -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, +} diff --git a/src/definitions/network.rs b/src/definitions/network.rs new file mode 100644 index 0000000..0a8aa53 --- /dev/null +++ b/src/definitions/network.rs @@ -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, + pub static_routes: Vec, +} + +#[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 }, + Bridge { members: Vec }, +} + +#[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, +} diff --git a/src/definitions/object.rs b/src/definitions/object.rs new file mode 100644 index 0000000..123c581 --- /dev/null +++ b/src/definitions/object.rs @@ -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, + pub services: HashMap, +} + +#[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 }, +} + +#[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, + destination_port: u64, + destination_port_end: Option, + }, + UDP { + source_port: u64, + source_port_end: Option, + destination_port: u64, + destination_port_end: Option, + }, + ICMP { + code: u8, + }, + Group { + children: Vec, + }, +} diff --git a/src/main.rs b/src/main.rs index b6c1939..0a85a13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, - static_routes: Vec, -} - -#[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 }, - Bridge { members: Vec }, -} - -#[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, - services: HashMap, -} - -#[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 }, -} - -#[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, - destination_port: u64, - destination_port_end: Option, - }, - UDP { - source_port: u64, - source_port_end: Option, - destination_port: u64, - destination_port_end: Option, - }, - ICMP { - code: u8, - }, - Group { - children: Vec, - }, -} +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);