diff --git a/Cargo.toml b/Cargo.toml index 6a7ecc7..30bd0e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } @@ -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" diff --git a/src/host.rs b/src/host.rs index 5ef9b39..b20e749 100644 --- a/src/host.rs +++ b/src/host.rs @@ -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, @@ -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, diff --git a/src/key.rs b/src/key.rs index e782326..e317e76 100644 --- a/src/key.rs +++ b/src/key.rs @@ -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, @@ -27,7 +28,6 @@ fn hex_value(char: u8) -> Option { /// WireGuard key representation in binary form. #[derive(Clone, Default)] -// #[serde(try_from = "String")] pub struct Key([u8; KEY_LENGTH]); impl Key { @@ -176,6 +176,7 @@ impl fmt::Display for Key { } } +#[cfg(feature = "serde")] impl Serialize for Key { fn serialize(&self, serializer: S) -> Result where @@ -185,8 +186,10 @@ impl Serialize for Key { } } +#[cfg(feature = "serde")] struct KeyVisitor; +#[cfg(feature = "serde")] impl Visitor<'_> for KeyVisitor { type Value = Key; @@ -202,6 +205,7 @@ impl Visitor<'_> for KeyVisitor { } } +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Key { fn deserialize(deserializer: D) -> Result where @@ -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. @@ -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); diff --git a/src/lib.rs b/src/lib.rs index fea086d..6cf5296 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; @@ -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, diff --git a/src/net.rs b/src/net.rs index 9a20ad6..e4ac646 100644 --- a/src/net.rs +++ b/src/net.rs @@ -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,