use std::{
fmt::{Display, Formatter},
io,
net::{SocketAddr, ToSocketAddrs},
path::Path,
vec,
};
use figment::{
providers::{Format, Toml},
Figment,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
pub struct Endpoint {
pub host: String,
pub port: u16,
}
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))
}