diff options
author | Alex Auvolat <alex@adnab.me> | 2023-10-03 18:40:37 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2023-10-03 18:40:37 +0200 |
commit | 2e656b541b1dd1492798e1ed764fa40868da4d6a (patch) | |
tree | 66fbf4c11a248d3c92cf44c4d3f18663670acbbe /src/util/socket_address.rs | |
parent | 9ac1d5be0eba1b3b35f7fb2f99fe8df549044197 (diff) | |
parent | 1243db87f2090a3302c7c8beb386e68ddf9b66b5 (diff) | |
download | garage-0.9.0-rc1.tar.gz garage-0.9.0-rc1.zip |
Merge branch 'main' into nextv0.9.0-rc1
Diffstat (limited to 'src/util/socket_address.rs')
-rw-r--r-- | src/util/socket_address.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/util/socket_address.rs b/src/util/socket_address.rs new file mode 100644 index 00000000..f01225f6 --- /dev/null +++ b/src/util/socket_address.rs @@ -0,0 +1,44 @@ +use std::fmt::{Debug, Display, Formatter}; +use std::net::SocketAddr; +use std::path::PathBuf; +use std::str::FromStr; + +use serde::de::Error; +use serde::{Deserialize, Deserializer}; + +#[derive(Debug, Clone)] +pub enum UnixOrTCPSocketAddress { + TCPSocket(SocketAddr), + UnixSocket(PathBuf), +} + +impl Display for UnixOrTCPSocketAddress { + fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result { + match self { + UnixOrTCPSocketAddress::TCPSocket(address) => write!(formatter, "http://{}", address), + UnixOrTCPSocketAddress::UnixSocket(path) => { + write!(formatter, "http+unix://{}", path.to_string_lossy()) + } + } + } +} + +impl<'de> Deserialize<'de> for UnixOrTCPSocketAddress { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + let string = String::deserialize(deserializer)?; + let string = string.as_str(); + + if string.starts_with("/") { + Ok(UnixOrTCPSocketAddress::UnixSocket( + PathBuf::from_str(string).map_err(Error::custom)?, + )) + } else { + Ok(UnixOrTCPSocketAddress::TCPSocket( + SocketAddr::from_str(string).map_err(Error::custom)?, + )) + } + } +} |