Skip to content

Commit

Permalink
feat: serde serialization/deserialization support
Browse files Browse the repository at this point in the history
Provides an optional feature to support `serde` for serializing
and deserializing `NetworkInterface`, `Addr`, `V4IfAddr`, `V6IfAddr`.

Resolves: #19
  • Loading branch information
EstebanBorai committed Aug 30, 2022
1 parent 25e0383 commit 2bda17b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.144", features = ["derive"], optional = true }
thiserror = "1.0"

[target.'cfg(target_os = "linux")'.dependencies]
Expand All @@ -28,3 +29,5 @@ cc = "1.0.73"
libc = "0.2.101"
winapi = {version = "0.3", features = ["ws2def", "ws2ipdef", "netioapi", "iphlpapi", "iptypes", "ntdef"] }

[features]
serde = ["dep:serde"]
7 changes: 7 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
use std::fmt::Debug;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// An alias for an `Option` that wraps either a `Ipv4Addr` or a `Ipv6Addr`
/// representing the IP for a Network Interface netmask
pub type Netmask<T> = Option<T>;

/// A system's network interface
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct NetworkInterface {
/// Interface's name
pub name: String,
Expand All @@ -21,6 +25,7 @@ pub struct NetworkInterface {

/// Network interface address
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum Addr {
/// IPV4 Interface from the AFINET network interface family
V4(V4IfAddr),
Expand All @@ -30,6 +35,7 @@ pub enum Addr {

/// IPV4 Interface from the AFINET network interface family
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct V4IfAddr {
/// The IP address for this network interface
pub ip: Ipv4Addr,
Expand All @@ -41,6 +47,7 @@ pub struct V4IfAddr {

/// IPV6 Interface from the AFINET6 network interface family
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct V6IfAddr {
/// The IP address for this network interface
pub ip: Ipv6Addr,
Expand Down

0 comments on commit 2bda17b

Please sign in to comment.