1use libp2p_identity::KeyType;
7use std::{collections::HashMap, net::Ipv4Addr};
8use thiserror::Error;
9
10pub static DEFAULT_IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(0, 0, 0, 0);
12
13pub static DEFAULT_KEEP_ALIVE_DURATION: Seconds = 60;
15
16#[derive(Error, Debug)]
18pub enum SwarmNlError {
19 #[error("could not read bootstrap config file")]
20 BoostrapFileReadError(String),
21 #[error("could not parse data read from bootstrap config file")]
22 BoostrapDataParseError(String),
23 #[error("could not configure transport. It is likely not supported on machine")]
24 TransportConfigError(TransportOpts),
25 #[error("could not configure DNS resolution into transport")]
26 DNSConfigError,
27 #[error("could not configure the selected protocols")]
28 ProtocolConfigError,
29 #[error("could not listen on specified address")]
30 MultiaddressListenError(String),
31 #[error("could not dial remote peer")]
32 RemotePeerDialError(String),
33 #[error("could not parse provided network id")]
34 NetworkIdParseError(String),
35 #[error("could not configure node for gossiping")]
36 GossipConfigError,
37}
38
39pub type SwarmNlResult<T> = Result<T, SwarmNlError>;
41pub type Port = u16;
43pub type Seconds = u64;
45pub type PeerIdString = String;
47pub type MultiaddrString = String;
49pub type Nodes = HashMap<PeerIdString, MultiaddrString>;
51
52pub const MIN_PORT: u16 = 1000;
54pub const MAX_PORT: u16 = 65535;
56
57pub static DEFAULT_NETWORK_ID: &str = "/swarmnl/1.0";
59pub static MIN_NETWORK_ID_LENGTH: u8 = 4;
62
63pub trait CustomFrom {
68 fn from(string: &str) -> Option<Self>
69 where
70 Self: Sized;
71}
72
73impl CustomFrom for KeyType {
74 fn from(s: &str) -> Option<Self> {
75 match s.to_lowercase().as_str() {
76 "ed25519" => Some(KeyType::Ed25519),
77 "rsa" => Some(KeyType::RSA),
78 "secp256k1" => Some(KeyType::Secp256k1),
79 "ecdsa" => Some(KeyType::Ecdsa),
80 _ => None,
81 }
82 }
83}
84
85#[derive(Hash, Eq, PartialEq, Debug, Clone)]
87pub enum TransportOpts {
88 TcpQuic { tcp_config: TcpConfig },
91}
92
93#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy)]
95pub enum TcpConfig {
96 Default,
98 Custom {
99 ttl: u32,
101 nodelay: bool,
103 backlog: u32,
105 },
108}