Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl fmt::Display for NetAddress #2183

Closed
benthecarman opened this issue Apr 13, 2023 · 2 comments
Closed

impl fmt::Display for NetAddress #2183

benthecarman opened this issue Apr 13, 2023 · 2 comments

Comments

@benthecarman
Copy link
Contributor

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)
            }
        }
    }
}
@TheBlueMatt
Copy link
Collaborator

Lol. After #2134 we'll have "implemented your own base32 encoding function here" we can reuse :).

@benthecarman
Copy link
Contributor Author

Closed by #2670

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants