use libp2p_identity::KeyType;
use std::{collections::HashMap, net::Ipv4Addr};
use thiserror::Error;
pub static DEFAULT_IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(0, 0, 0, 0);
pub static DEFAULT_KEEP_ALIVE_DURATION: Seconds = 60;
#[derive(Error, Debug)]
pub enum SwarmNlError {
#[error("could not read bootstrap config file")]
BoostrapFileReadError(String),
#[error("could not parse data read from bootstrap config file")]
BoostrapDataParseError(String),
#[error("could not configure transport. It is likely not supported on machine")]
TransportConfigError(TransportOpts),
#[error("could not configure DNS resolution into transport")]
DNSConfigError,
#[error("could not configure the selected protocols")]
ProtocolConfigError,
#[error("could not listen on specified address")]
MultiaddressListenError(String),
#[error("could not dial remote peer")]
RemotePeerDialError(String),
#[error("could not parse provided network id")]
NetworkIdParseError(String),
#[error("could not configure node for gossiping")]
GossipConfigError,
}
pub type SwarmNlResult<T> = Result<T, SwarmNlError>;
pub type Port = u16;
pub type Seconds = u64;
pub type PeerIdString = String;
pub type MultiaddrString = String;
pub type Nodes = HashMap<PeerIdString, MultiaddrString>;
pub const MIN_PORT: u16 = 1000;
pub const MAX_PORT: u16 = 65535;
pub static DEFAULT_NETWORK_ID: &str = "/swarmnl/1.0";
pub static MIN_NETWORK_ID_LENGTH: u8 = 4;
pub trait CustomFrom {
fn from(string: &str) -> Option<Self>
where
Self: Sized;
}
impl CustomFrom for KeyType {
fn from(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"ed25519" => Some(KeyType::Ed25519),
"rsa" => Some(KeyType::RSA),
"secp256k1" => Some(KeyType::Secp256k1),
"ecdsa" => Some(KeyType::Ecdsa),
_ => None,
}
}
}
#[derive(Hash, Eq, PartialEq, Debug, Clone)]
pub enum TransportOpts {
TcpQuic { tcp_config: TcpConfig },
}
#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy)]
pub enum TcpConfig {
Default,
Custom {
ttl: u32,
nodelay: bool,
backlog: u32,
},
}