Implement Debug

This commit is contained in:
Adam Ierymenko
2023-10-17 16:36:03 -04:00
parent 96b1df85b7
commit 830393c1ec
3 changed files with 60 additions and 8 deletions
+4 -2
View File
@@ -6,6 +6,7 @@
* https://www.zerotier.com/
*/
use std::fmt::Debug;
use std::hash::Hash;
use std::str::FromStr;
@@ -19,6 +20,7 @@ use zerotier_common_utils::tofrombytes::ToFromBytes;
pub trait Address:
ToString
+ FromStr
+ Debug
+ ToFromBytes
+ Sync
+ Send
@@ -37,7 +39,7 @@ pub trait Address:
/// A bundle of public key(s) securely identifying a participant on the network.
pub trait Identity:
ToString + FromStr + ToFromBytes + Sync + Send + Clone + PartialEq + Eq + Hash + PartialOrd + Ord + 'static
ToString + FromStr + Debug + ToFromBytes + Sync + Send + Clone + PartialEq + Eq + Hash + PartialOrd + Ord + 'static
{
/// Number of bytes in this identity's byte serialized representation.
const SIZE: usize;
@@ -83,6 +85,6 @@ pub(crate) const IDENTITY_ERR: InvalidParameterError = InvalidParameterError("in
pub use serde;
pub use serde_cbor;
pub use zeroize;
pub use zerotier_crypto_glue;
pub use zerotier_common_utils;
pub use zerotier_crypto_glue;
pub use zerotier_crypto_glue::zssp;
+38 -6
View File
@@ -6,6 +6,7 @@
* https://www.zerotier.com/
*/
use std::fmt::Debug;
use std::hash::Hash;
use std::io::Write;
use std::mem::{size_of, transmute};
@@ -181,6 +182,20 @@ impl ToString for ShortAddress {
}
}
impl Debug for Address {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.to_string().as_str())
}
}
impl Debug for ShortAddress {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.to_string().as_str())
}
}
impl FromStr for Address {
type Err = InvalidParameterError;
@@ -430,6 +445,19 @@ impl ToString for Identity {
}
}
impl Debug for Identity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("x25519::Identity")
.field("address", &self.address)
.field("master_signing_key", self.master_signing_key.as_bytes())
.field("timestamp", &self.timestamp)
.field("ecdh", self.ecdh.as_bytes())
.field("ecdsa", self.ecdsa.as_bytes())
.field("master_signature", &self.master_signature)
.finish()
}
}
impl FromStr for Identity {
type Err = InvalidParameterError;
@@ -486,7 +514,6 @@ impl ToFromBytes for Identity {
}
}
impl Serialize for Identity {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -496,7 +523,12 @@ impl Serialize for Identity {
if serializer.is_human_readable() {
self.to_string().serialize(serializer)
} else {
serializer.serialize_bytes(self.to_bytes_on_stack::<{P384_PUBLIC_KEY_SIZE + 8 + P384_PUBLIC_KEY_SIZE + P384_PUBLIC_KEY_SIZE + P384_ECDSA_SIGNATURE_SIZE}>().as_ref())
serializer.serialize_bytes(
self.to_bytes_on_stack::<{
P384_PUBLIC_KEY_SIZE + 8 + P384_PUBLIC_KEY_SIZE + P384_PUBLIC_KEY_SIZE + P384_ECDSA_SIGNATURE_SIZE
}>()
.as_ref(),
)
}
}
}
@@ -508,8 +540,7 @@ impl<'de> Deserialize<'de> for Identity {
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
Identity::from_str(<&str>::deserialize(deserializer)?)
.map_err(|_| serde::de::Error::custom(IDENTITY_ERR.0))
Identity::from_str(<&str>::deserialize(deserializer)?).map_err(|_| serde::de::Error::custom(IDENTITY_ERR.0))
} else {
struct Visitor;
@@ -521,8 +552,9 @@ impl<'de> Deserialize<'de> for Identity {
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: serde::de::Error, {
where
E: serde::de::Error,
{
Identity::from_bytes(v).map_err(|_| serde::de::Error::custom(IDENTITY_ERR.0))
}
}
+18
View File
@@ -7,6 +7,7 @@
*/
use std::alloc::{alloc, Layout};
use std::fmt::Debug;
use std::hash::Hash;
use std::mem::transmute_copy;
use std::ptr::copy_nonoverlapping;
@@ -72,6 +73,13 @@ impl ToString for Address {
}
}
impl Debug for Address {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.to_string().as_str())
}
}
impl FromStr for Address {
type Err = InvalidParameterError;
@@ -180,6 +188,16 @@ impl ToString for Identity {
}
}
impl Debug for Identity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("x25519::Identity")
.field("address", &self.address)
.field("ecdh", &self.ecdh)
.field("eddsa", &self.eddsa)
.finish()
}
}
impl FromStr for Identity {
type Err = InvalidParameterError;