aboutsummaryrefslogtreecommitdiff
path: root/src/util/socket_address.rs
diff options
context:
space:
mode:
authorAlex <alex@adnab.me>2023-10-03 16:23:02 +0000
committerAlex <alex@adnab.me>2023-10-03 16:23:02 +0000
commit1243db87f2090a3302c7c8beb386e68ddf9b66b5 (patch)
tree7280b64fd8ef63be32f27f44f26f0ee7c8ca44be /src/util/socket_address.rs
parent16aa418e473a5e9ef229060d20f6eb280df272a2 (diff)
parent6f8a87814be502ecaee49cd37616ec7fe4c5b588 (diff)
downloadgarage-1243db87f2090a3302c7c8beb386e68ddf9b66b5.tar.gz
garage-1243db87f2090a3302c7c8beb386e68ddf9b66b5.zip
Merge pull request 'Add support for binding to unix domain sockets' (#640) from networkException/garage:unix-sockets into main
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/640
Diffstat (limited to 'src/util/socket_address.rs')
-rw-r--r--src/util/socket_address.rs44
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)?,
+ ))
+ }
+ }
+}