torrentdht 0.1.1

DHT discovery with mTLS handshake handoff; optional BitTorrent when the host opts in
Documentation
use crate::bencode::BencodeValue;

pub use crate::wire::{
    decode_nodes_auto, decode_peer_values, decode_peers, decode_peers_auto,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DhtMethod {
    Ping,
    FindNode,
    GetPeers,
    AnnouncePeer,
}

impl DhtMethod {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Ping => "ping",
            Self::FindNode => "find_node",
            Self::GetPeers => "get_peers",
            Self::AnnouncePeer => "announce_peer",
        }
    }

    pub fn parse(raw: &str) -> Option<Self> {
        match raw {
            "ping" => Some(Self::Ping),
            "find_node" => Some(Self::FindNode),
            "get_peers" => Some(Self::GetPeers),
            "announce_peer" => Some(Self::AnnouncePeer),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
pub enum DhtMessage {
    Query(DhtQuery),
    Response(DhtResponse),
}

#[derive(Debug, Clone)]
pub struct DhtQuery {
    pub transaction_id: Vec<u8>,
    pub method: DhtMethod,
    pub arguments: std::collections::BTreeMap<Vec<u8>, BencodeValue>,
}

#[derive(Debug, Clone)]
pub struct DhtResponse {
    pub transaction_id: Vec<u8>,
    pub values: std::collections::BTreeMap<Vec<u8>, BencodeValue>,
}