Skip to content

Commit

Permalink
refactor: use hex for debugging and display
Browse files Browse the repository at this point in the history
Everything except the ticket in the examples is now using hex

Ref n0-computer/iroh#2841
  • Loading branch information
dignifiedquire committed Oct 25, 2024
1 parent 2cdbe36 commit b487112
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ iroh-net = { version = "0.27.0", optional = true, default-features = false }
tokio = { version = "1", optional = true, features = ["io-util", "sync", "rt", "macros", "net", "fs"] }
tokio-util = { version = "0.7.12", optional = true, features = ["codec", "rt"] }
tracing = "0.1"
data-encoding = { version = "2.6.0", optional = true }
hex = "0.4.3"

[dev-dependencies]
clap = { version = "4", features = ["derive"] }
Expand All @@ -71,9 +73,11 @@ net = [
"dep:futures-concurrency"
]

examples = ["net", "dep:data-encoding"]

[[example]]
name = "chat"
required-features = ["net"]
required-features = ["examples"]

[package.metadata.docs.rs]
all-features = true
Expand Down
10 changes: 6 additions & 4 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use bytes::Bytes;
use clap::Parser;
use ed25519_dalek::Signature;
use futures_lite::StreamExt;
use iroh_base::base32;
use iroh_gossip::{
net::{Event, Gossip, GossipEvent, GossipReceiver, GOSSIP_ALPN},
proto::TopicId,
Expand Down Expand Up @@ -61,7 +60,7 @@ enum Command {
///
/// If no topic is provided, a new topic will be created.
Open {
/// Optionally set the topic id (32 bytes, as base32 string).
/// Optionally set the topic id (64 bytes, as hex string).
topic: Option<TopicId>,
},
/// Join a chat room from a ticket.
Expand Down Expand Up @@ -297,15 +296,18 @@ impl Ticket {
/// Serializes to base32.
impl fmt::Display for Ticket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", base32::fmt(self.to_bytes()))
let mut text = data_encoding::BASE32_NOPAD.encode(&self.to_bytes()[..]);
text.make_ascii_lowercase();
write!(f, "{}", text)
}
}

/// Deserializes from base32.
impl FromStr for Ticket {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_bytes(&base32::parse_vec(s)?)
let bytes = data_encoding::BASE32_NOPAD.decode(s.to_ascii_uppercase().as_bytes())?;
Self::from_bytes(&bytes)
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/proto/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,22 @@ macro_rules! idbytes_impls {

impl ::std::fmt::Display for $ty {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", ::iroh_base::base32::fmt(&self.0))
write!(f, "{}", ::hex::encode(&self.0))
}
}

impl ::std::fmt::Debug for $ty {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}({})", $name, ::iroh_base::base32::fmt_short(&self.0))
write!(f, "{}({})", $name, ::hex::encode(&self.0))
}
}

impl ::std::str::FromStr for $ty {
type Err = ::anyhow::Error;
fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
Ok(Self::from_bytes(::iroh_base::base32::parse_array(s)?))
let mut bytes = [0u8; 32];
::hex::decode_to_slice(s, &mut bytes)?;
Ok(Self::from_bytes(bytes))
}
}

Expand Down Expand Up @@ -423,17 +425,20 @@ mod test {
}

#[test]
fn base32() {
fn hex() {
#[derive(Eq, PartialEq)]
struct Id([u8; 32]);
idbytes_impls!(Id, "Id");
let id: Id = [1u8; 32].into();
assert_eq!(id, Id::from_str(&format!("{id}")).unwrap());
assert_eq!(
&format!("{id}"),
"aeaqcaibaeaqcaibaeaqcaibaeaqcaibaeaqcaibaeaqcaibaeaq"
"0101010101010101010101010101010101010101010101010101010101010101"
);
assert_eq!(
&format!("{id:?}"),
"Id(0101010101010101010101010101010101010101010101010101010101010101)"
);
assert_eq!(&format!("{id:?}"), "Id(aeaqcaibaeaqcaib)");
assert_eq!(id.as_bytes(), &[1u8; 32]);
}

Expand Down

0 comments on commit b487112

Please sign in to comment.