Skip to content

impl fmt::Display for NetAddress #2183

Closed
@benthecarman

Description

@benthecarman

tried to do this one myself but don't really know how the tor addresses are meant to be displayed.

ChatGPT gave me this but it's really ugly so didn't want to commit it

use core::fmt;

impl fmt::Display for NetAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            NetAddress::IPv4 { addr, port } => {
                write!(
                    f,
                    "{}.{}.{}.{}:{}",
                    addr[0], addr[1], addr[2], addr[3], port
                )
            }
            NetAddress::IPv6 { addr, port } => {
                write!(
                    f,
                    "{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:%{}",
                    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
                    addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15],
                    port
                )
            }
            NetAddress::OnionV2(addr) => {
                let mut onion = [0u8; 12];
                onion.copy_from_slice(addr);
                let mut base32 = String::new();
                // Implement your base32 encoding function here
                write!(f, "{}.onion", base32)
            }
            NetAddress::OnionV3 { ed25519_pubkey, checksum, version, port } => {
                // Implement your base32 encoding function here
                let mut onion = String::new();
                write!(f, "{}.onion:{}", onion, port)
            }
            NetAddress::Hostname { hostname, port } => {
                write!(f, "{}:{}", hostname, port)
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions