summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAva Hahn <a.hahn@f5.com>2024-05-06 12:13:52 -0700
committeravahahn <110854134+avahahn@users.noreply.github.com>2024-05-08 13:30:08 -0700
commit6ad1fa342813f2c9f00813108c3f2eec8824fd6f (patch)
tree4231ce46effffc24d800785eb808a193b38f3676
parentcb03d31e02741b87fc3a3aa379bf8f1849442ae2 (diff)
downloadunit-6ad1fa342813f2c9f00813108c3f2eec8824fd6f.tar.gz
unit-6ad1fa342813f2c9f00813108c3f2eec8824fd6f.tar.bz2
tools/unitctl: clean up control socket impls
Signed-off-by: Ava Hahn <a.hahn@f5.com>
-rw-r--r--tools/unitctl/unit-client-rs/src/control_socket_address.rs174
1 files changed, 87 insertions, 87 deletions
diff --git a/tools/unitctl/unit-client-rs/src/control_socket_address.rs b/tools/unitctl/unit-client-rs/src/control_socket_address.rs
index b9ae5afc..402d2293 100644
--- a/tools/unitctl/unit-client-rs/src/control_socket_address.rs
+++ b/tools/unitctl/unit-client-rs/src/control_socket_address.rs
@@ -34,6 +34,92 @@ impl ControlSocketScheme {
}
}
+
+impl Display for ControlSocket {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ match self {
+ UnixLocalAbstractSocket(name) => f.write_fmt(format_args!("unix:@{}", name)),
+ UnixLocalSocket(path) => f.write_fmt(format_args!("unix:{}", path.to_string_lossy())),
+ TcpSocket(uri) => uri.fmt(f),
+ }
+ }
+}
+
+impl From<ControlSocket> for String {
+ fn from(val: ControlSocket) -> Self {
+ val.to_string()
+ }
+}
+
+impl From<ControlSocket> for PathBuf {
+ fn from(val: ControlSocket) -> Self {
+ match val {
+ UnixLocalAbstractSocket(socket_name) => PathBuf::from(format!("@{}", socket_name)),
+ UnixLocalSocket(socket_path) => socket_path,
+ TcpSocket(_) => PathBuf::default(),
+ }
+ }
+}
+
+impl From<ControlSocket> for Uri {
+ fn from(val: ControlSocket) -> Self {
+ val.create_uri_with_path("")
+ }
+}
+
+impl TryFrom<String> for ControlSocket {
+ type Error = UnitClientError;
+
+ fn try_from(socket_address: String) -> Result<Self, Self::Error> {
+ ControlSocket::parse_address(socket_address.as_str())
+ }
+}
+
+impl TryFrom<&str> for ControlSocket {
+ type Error = UnitClientError;
+
+ fn try_from(socket_address: &str) -> Result<Self, Self::Error> {
+ ControlSocket::parse_address(socket_address)
+ }
+}
+
+impl TryFrom<Uri> for ControlSocket {
+ type Error = UnitClientError;
+
+ fn try_from(socket_uri: Uri) -> Result<Self, Self::Error> {
+ match socket_uri.scheme_str() {
+ // URIs with the unix scheme will have a hostname that is a hex encoded string
+ // representing the path to the socket
+ Some("unix") => {
+ let host = match socket_uri.host() {
+ Some(host) => host,
+ None => {
+ return Err(UnitClientError::TcpSocketAddressParseError {
+ message: "No host found in socket address".to_string(),
+ control_socket_address: socket_uri.to_string(),
+ })
+ }
+ };
+ let bytes = hex::decode(host).map_err(|error| UnitClientError::TcpSocketAddressParseError {
+ message: error.to_string(),
+ control_socket_address: socket_uri.to_string(),
+ })?;
+ let path = String::from_utf8_lossy(&bytes);
+ ControlSocket::parse_address(path)
+ }
+ Some("http") | Some("https") => Ok(TcpSocket(socket_uri)),
+ Some(unknown) => Err(UnitClientError::TcpSocketAddressParseError {
+ message: format!("Unsupported scheme found in socket address: {}", unknown).to_string(),
+ control_socket_address: socket_uri.to_string(),
+ }),
+ None => Err(UnitClientError::TcpSocketAddressParseError {
+ message: "No scheme found in socket address".to_string(),
+ control_socket_address: socket_uri.to_string(),
+ }),
+ }
+ }
+}
+
impl ControlSocket {
pub fn socket_scheme(&self) -> ControlSocketScheme {
match self {
@@ -69,41 +155,7 @@ impl ControlSocket {
}
}
}
-}
-
-impl Display for ControlSocket {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- match self {
- UnixLocalAbstractSocket(name) => f.write_fmt(format_args!("unix:@{}", name)),
- UnixLocalSocket(path) => f.write_fmt(format_args!("unix:{}", path.to_string_lossy())),
- TcpSocket(uri) => uri.fmt(f),
- }
- }
-}
-impl From<ControlSocket> for String {
- fn from(val: ControlSocket) -> Self {
- val.to_string()
- }
-}
-
-impl From<ControlSocket> for PathBuf {
- fn from(val: ControlSocket) -> Self {
- match val {
- UnixLocalAbstractSocket(socket_name) => PathBuf::from(format!("@{}", socket_name)),
- UnixLocalSocket(socket_path) => socket_path,
- TcpSocket(_) => PathBuf::default(),
- }
- }
-}
-
-impl From<ControlSocket> for Uri {
- fn from(val: ControlSocket) -> Self {
- val.create_uri_with_path("")
- }
-}
-
-impl ControlSocket {
pub fn validate_http_address(uri: Uri) -> Result<(), UnitClientError> {
let http_address = uri.to_string();
if uri.authority().is_none() {
@@ -202,7 +254,7 @@ impl ControlSocket {
}
/// Flexibly parse a textual representation of a socket address
- fn parse_address<S: Into<String>>(socket_address: S) -> Result<Self, UnitClientError> {
+ pub fn parse_address<S: Into<String>>(socket_address: S) -> Result<Self, UnitClientError> {
let full_socket_address: String = socket_address.into();
let socket_prefix = "unix:";
let socket_uri_prefix = "unix://";
@@ -260,58 +312,6 @@ impl ControlSocket {
}
}
-impl TryFrom<String> for ControlSocket {
- type Error = UnitClientError;
-
- fn try_from(socket_address: String) -> Result<Self, Self::Error> {
- ControlSocket::parse_address(socket_address.as_str())
- }
-}
-
-impl TryFrom<&str> for ControlSocket {
- type Error = UnitClientError;
-
- fn try_from(socket_address: &str) -> Result<Self, Self::Error> {
- ControlSocket::parse_address(socket_address)
- }
-}
-
-impl TryFrom<Uri> for ControlSocket {
- type Error = UnitClientError;
-
- fn try_from(socket_uri: Uri) -> Result<Self, Self::Error> {
- match socket_uri.scheme_str() {
- // URIs with the unix scheme will have a hostname that is a hex encoded string
- // representing the path to the socket
- Some("unix") => {
- let host = match socket_uri.host() {
- Some(host) => host,
- None => {
- return Err(UnitClientError::TcpSocketAddressParseError {
- message: "No host found in socket address".to_string(),
- control_socket_address: socket_uri.to_string(),
- })
- }
- };
- let bytes = hex::decode(host).map_err(|error| UnitClientError::TcpSocketAddressParseError {
- message: error.to_string(),
- control_socket_address: socket_uri.to_string(),
- })?;
- let path = String::from_utf8_lossy(&bytes);
- ControlSocket::parse_address(path)
- }
- Some("http") | Some("https") => Ok(TcpSocket(socket_uri)),
- Some(unknown) => Err(UnitClientError::TcpSocketAddressParseError {
- message: format!("Unsupported scheme found in socket address: {}", unknown).to_string(),
- control_socket_address: socket_uri.to_string(),
- }),
- None => Err(UnitClientError::TcpSocketAddressParseError {
- message: "No scheme found in socket address".to_string(),
- control_socket_address: socket_uri.to_string(),
- }),
- }
- }
-}
#[cfg(test)]
mod tests {