use std::net::{Ipv4Addr, Ipv6Addr}; use prost_convert::{IntoProto, TryIntoNative}; use prost_convert_derive::ProstConvert; pub mod proto { pub struct NetworkInterface { pub name: Option, pub mac_addr: Option, pub addr: Option, } pub enum Addr { V4(V4IfAddr), V6(V6IfAddr), } pub struct V6IfAddr { pub ip: String, pub broadcast: Option, pub netmask: Option, } pub struct V4IfAddr { pub ip: String, pub broadcast: Option, pub netmask: Option, } } #[derive(Clone, PartialEq, Eq, Default, ProstConvert, Debug)] #[prost_convert(src = "proto::NetworkInterface")] pub struct NetworkInterface { /// Interface's name. pub name: Option, /// Interface's address. pub addr: Option, /// MAC Address. pub mac_addr: Option, } #[derive(Clone, PartialEq, Eq, ProstConvert, Debug)] #[prost_convert(src = "proto::Addr")] pub enum Addr { V4(V4IfAddr), V6(V6IfAddr), } pub type Netmask = Option; #[derive(Clone, PartialEq, Eq, ProstConvert, Debug)] #[prost_convert(src = "proto::V4IfAddr")] pub struct V4IfAddr { pub ip: Ipv4Addr, pub broadcast: Option, pub netmask: Option, } #[derive(Clone, PartialEq, Eq, ProstConvert, Debug)] #[prost_convert(src = "proto::V6IfAddr")] pub struct V6IfAddr { pub ip: Ipv6Addr, pub broadcast: Option, pub netmask: Option, } #[test] fn it_works() { let native = NetworkInterface { name: None, addr: None, mac_addr: None, }; let proto: proto::NetworkInterface = native.clone().into_proto(); assert_eq!(native, proto.try_into_native().unwrap()); }