Add support for host names in config address fields, resolve them to IP

This commit is contained in:
timvisee
2021-11-17 16:14:05 +01:00
parent ec24f088b2
commit b1bd9e1837
3 changed files with 38 additions and 1 deletions

30
src/util/serde.rs Normal file
View File

@@ -0,0 +1,30 @@
use std::net::{SocketAddr, ToSocketAddrs};
use serde::de::{Error, Unexpected};
use serde::{Deserialize, Deserializer};
/// Deserialize a `Vec` into a `HashMap` by key.
pub fn to_socket_addrs<'de, D>(d: D) -> Result<SocketAddr, D::Error>
where
D: Deserializer<'de>,
{
// Deserialize string
let addr = String::deserialize(d)?;
// Try to socket address to resolve
match addr.to_socket_addrs() {
Ok(mut addr) => {
if let Some(addr) = addr.next() {
return Ok(addr);
}
}
Err(err) => {
dbg!(err);
}
}
// Parse raw IP address
addr.parse().map_err(|_| {
Error::invalid_value(Unexpected::Str(&addr), &"IP or resolvable host and port")
})
}