use std::{
fmt::{Display, Formatter},
io,
net::{SocketAddr, ToSocketAddrs},
path::Path,
vec,
};
use figment::{
providers::{Format, Toml},
Figment,
};
use serde::{Deserialize, Serialize};
pub const DEFAULT_NODE_RPC_PORT: u16 = 57291;
pub const DEFAULT_BLOCK_PRODUCER_PORT: u16 = 48046;
pub const DEFAULT_STORE_PORT: u16 = 28943;
pub const DEFAULT_FAUCET_SERVER_PORT: u16 = 8080;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
pub struct Endpoint {
pub host: String,
pub port: u16,
}
impl Endpoint {
pub fn localhost(port: u16) -> Self {
Endpoint { host: "localhost".to_string(), port }
}
}
impl ToSocketAddrs for Endpoint {
type Iter = vec::IntoIter<SocketAddr>;
fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
(self.host.as_ref(), self.port).to_socket_addrs()
}
}
impl Display for Endpoint {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("http://{}:{}", self.host, self.port))
}
}
pub fn load_config(config_file: &Path) -> Figment {
Figment::from(Toml::file(config_file))
}