btpeer 0.9.0

Simple CLI tool and library to get peers from TCP/HTTP and UDP BitTorrent trackers
Documentation
use std::str::FromStr;
use url::{ParseError, Url};

pub struct Announce(Url);

impl Announce {
    pub fn new(announce_url: &str, id20: &[u8; 20], port: u16) -> Result<Self, ParseError> {
        let mut url = Url::from_str(announce_url)?;

        let mut rng = rand::rng();
        let mut peer_id = [0u8; 20];
        rand::RngExt::fill(&mut rng, &mut peer_id);

        url.set_query(Some(&format!(
            "info_hash={}&peer_id={}&port={port}&uploaded=0&downloaded=0&left=1000&compact=1",
            super::url_encode_bytes(id20),
            super::url_encode_bytes(&peer_id)
        )));

        Ok(Self(url))
    }
}

impl std::fmt::Display for Announce {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}