Skip to content

Commit

Permalink
Make serde a feature (default on)
Browse files Browse the repository at this point in the history
  • Loading branch information
moubctez committed Dec 11, 2024
1 parent a7245c4 commit 8df5a1a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["network-programming"]
[dependencies]
base64 = "0.22"
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"], optional = true }
thiserror = "2.0"
x25519-dalek = { version = "2.0", features = ["getrandom", "static_secrets"] }

Expand Down Expand Up @@ -42,6 +42,10 @@ netlink-packet-utils = "0.5"
netlink-packet-wireguard = "0.2"
netlink-sys = "0.8"

[features]
default = ["serde"]
serde = ["dep:serde"]

[profile.release]
lto = "thin"
strip = "symbols"
7 changes: 5 additions & 2 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use netlink_packet_wireguard::{
constants::{WGDEVICE_F_REPLACE_PEERS, WGPEER_F_REPLACE_ALLOWEDIPS},
nlas::{WgAllowedIpAttrs, WgDeviceAttrs, WgPeer, WgPeerAttrs},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{error::WireguardInterfaceError, key::Key, net::IpAddrMask, utils::resolve};

/// WireGuard peer representation.
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Peer {
pub public_key: Key,
pub preshared_key: Option<Key>,
Expand Down Expand Up @@ -171,7 +173,8 @@ impl Peer {
}

/// WireGuard host representation.
#[derive(Default, Clone, Serialize, Deserialize)]
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Host {
pub listen_port: u16,
pub private_key: Option<Key>,
Expand Down
8 changes: 7 additions & 1 deletion src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

use base64::{prelude::BASE64_STANDARD, DecodeError, Engine};
#[cfg(feature = "serde")]
use serde::{
de::{Unexpected, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
Expand All @@ -27,7 +28,6 @@ fn hex_value(char: u8) -> Option<u8> {

/// WireGuard key representation in binary form.
#[derive(Clone, Default)]
// #[serde(try_from = "String")]
pub struct Key([u8; KEY_LENGTH]);

impl Key {
Expand Down Expand Up @@ -176,6 +176,7 @@ impl fmt::Display for Key {
}
}

#[cfg(feature = "serde")]
impl Serialize for Key {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -185,8 +186,10 @@ impl Serialize for Key {
}
}

#[cfg(feature = "serde")]
struct KeyVisitor;

#[cfg(feature = "serde")]
impl Visitor<'_> for KeyVisitor {
type Value = Key;

Expand All @@ -202,6 +205,7 @@ impl Visitor<'_> for KeyVisitor {
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Key {
fn deserialize<D>(deserializer: D) -> Result<Key, D::Error>
where
Expand All @@ -215,6 +219,7 @@ impl<'de> Deserialize<'de> for Key {
mod tests {
use super::*;

#[cfg(feature = "serde")]
use serde_test::{assert_tokens, Token};

// Same `Key` in different representations.
Expand Down Expand Up @@ -242,6 +247,7 @@ mod tests {
assert_eq!(key.to_string(), KEY_B64);
}

#[cfg(feature = "serde")]
#[test]
fn serialize_key() {
let key = Key(KEY_BUF);
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ extern crate log;

use std::{fmt, process::Output};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
// public re-exports
pub use wgapi::{Kernel, Userspace, WGApi};
Expand All @@ -97,7 +98,8 @@ pub enum IpVersion {
}

/// Host WireGuard interface configuration
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct InterfaceConfiguration {
pub name: String,
pub prvkey: String,
Expand Down
4 changes: 3 additions & 1 deletion src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use netlink_packet_wireguard::{
constants::{AF_INET, AF_INET6},
nlas::{WgAllowedIp, WgAllowedIpAttrs},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// IP address with CIDR.
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq, Hash)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct IpAddrMask {
// IP v4 or v6
pub ip: IpAddr,
Expand Down

0 comments on commit 8df5a1a

Please sign in to comment.