diff --git a/src/blocking.rs b/src/blocking.rs index 2ca519a..df8ef16 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -1,26 +1,24 @@ -use crate::{ - commons::{BindingDestinationType, UserLimitTarget, VirtualHostLimitTarget}, - requests::{ - self, EnforcedLimitParams, ExchangeParams, Permissions, PolicyParams, QueueParams, - RuntimeParameterDefinition, UserParams, VirtualHostParams, XArguments, - }, - responses::{self, BindingInfo}, -}; -use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; +use std::fmt; + use reqwest::{ blocking::Client as HttpClient, header::{HeaderMap, HeaderValue, InvalidHeaderValue}, + StatusCode, }; use serde::Serialize; use serde_json::{json, Map, Value}; -use std::{ - collections::HashMap, - fmt::{self, Display}, -}; - -use crate::responses::DefinitionSet; use thiserror::Error; +use crate::{ + commons::{BindingDestinationType, UserLimitTarget, VirtualHostLimitTarget}, + path, + requests::{ + self, EnforcedLimitParams, ExchangeParams, Permissions, PolicyParams, QueueParams, + RuntimeParameterDefinition, UserParams, VirtualHostParams, XArguments, + }, + responses::{self, BindingInfo, DefinitionSet}, +}; + type HttpClientResponse = reqwest::blocking::Response; #[derive(Error, Debug)] @@ -28,9 +26,9 @@ pub enum Error { #[error("encountered an error when performing an HTTP request")] RequestError(#[from] reqwest::Error), #[error("API responded with a client error: status code of {0}")] - ClientErrorResponse(u16, HttpClientResponse), + ClientErrorResponse(StatusCode, HttpClientResponse), #[error("API responded with a server error: status code of {0}")] - ServerErrorResponse(u16, HttpClientResponse), + ServerErrorResponse(StatusCode, HttpClientResponse), #[error("Health check failed: resource alarms are in effect")] HealthCheckFailed(responses::HealthCheckFailureDetails), #[error("Could not find the requested resource")] @@ -227,164 +225,124 @@ where /// Lists cluster nodes. pub fn list_nodes(&self) -> Result> { - let response = self.http_get("nodes")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("nodes", None, None)?; + let response = response.json()?; + Ok(response) } /// Lists virtual hosts in the cluster. pub fn list_vhosts(&self) -> Result> { - let response = self.http_get("vhosts")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("vhosts", None, None)?; + let response = response.json()?; + Ok(response) } /// Lists users in the internal database. pub fn list_users(&self) -> Result> { - let response = self.http_get("users")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("users", None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all client connections across the cluster. pub fn list_connections(&self) -> Result> { - let response = self.http_get("connections")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("connections", None, None)?; + let response = response.json()?; + Ok(response) } pub fn get_connection_info(&self, name: &str) -> Result { - let response = self.http_get(&format!("connections/{}", self.percent_encode(name)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get(path!("connections", name), None, None)?; + let response = response.json()?; + Ok(response) } pub fn close_connection(&self, name: &str, reason: Option<&str>) -> Result<()> { - let response: HttpClientResponse = match reason { - None => self.http_delete(&format!("connections/{}", self.percent_encode(name)))?, + match reason { + None => self.http_delete( + path!("connections", name), + Some(StatusCode::NOT_FOUND), + None, + )?, Some(value) => { let mut headers = HeaderMap::new(); let hdr = HeaderValue::from_str(value)?; headers.insert("X-Reason", hdr); - self.http_delete_with_headers( - &format!("connections/{}", self.percent_encode(name)), - headers, - )? + self.http_delete_with_headers(path!("connections", name), headers, None, None)? } }; - let _ = self.ok_or_status_code_error_except_404(response)?; Ok(()) } /// Lists all connections in the given virtual host. pub fn list_connections_in(&self, virtual_host: &str) -> Result> { - let response = self.http_get(&format!( - "vhosts/{}/connections", - self.percent_encode(virtual_host) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("vhosts", virtual_host, "connections"), None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all connections of a specific user. pub fn list_user_connections(&self, username: &str) -> Result> { - let response = self.http_get(&format!( - "connections/username/{}", - self.percent_encode(username) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("connections", "username", username), None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all channels across the cluster. pub fn list_channels(&self) -> Result> { - let response = self.http_get("channels")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("channels", None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all channels in the given virtual host. pub fn list_channels_in(&self, virtual_host: &str) -> Result> { - let response = self.http_get(&format!( - "vhosts/{}/channels", - self.percent_encode(virtual_host) - ))?; + let response = self.http_get(path!("vhosts", virtual_host, "channels"), None, None)?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = response.json()?; + Ok(response) } /// Lists all queues and streams across the cluster. pub fn list_queues(&self) -> Result> { - let response = self.http_get("queues")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("queues", None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all queues and streams in the given virtual host. pub fn list_queues_in(&self, virtual_host: &str) -> Result> { - let response = self.http_get(&format!("queues/{}", self.percent_encode(virtual_host)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("queues", virtual_host), None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all exchanges across the cluster. pub fn list_exchanges(&self) -> Result> { - let response = self.http_get("exchanges")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("exchanges", None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all exchanges in the given virtual host. pub fn list_exchanges_in(&self, virtual_host: &str) -> Result> { - let response = - self.http_get(&format!("exchanges/{}", self.percent_encode(virtual_host)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("exchanges", virtual_host), None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all bindings (both queue-to-exchange and exchange-to-exchange ones) across the cluster. pub fn list_bindings(&self) -> Result> { - let response = self.http_get("bindings")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("bindings", None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all bindings (both queue-to-exchange and exchange-to-exchange ones) in the given virtual host. pub fn list_bindings_in(&self, virtual_host: &str) -> Result> { - let response = self.http_get(&format!("bindings/{}", self.percent_encode(virtual_host)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("bindings", virtual_host), None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all bindings of a specific queue. @@ -393,15 +351,10 @@ where virtual_host: &str, queue: &str, ) -> Result> { - let response = self.http_get(&format!( - "queues/{}/{}/bindings", - self.percent_encode(virtual_host), - self.percent_encode(queue) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = + self.http_get(path!("queues", virtual_host, queue, "bindings"), None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all bindings of a specific exchange where it is the source. @@ -432,58 +385,44 @@ where /// Lists all consumers across the cluster. pub fn list_consumers(&self) -> Result> { - let response = self.http_get("consumers")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("consumers", None, None)?; + let response = response.json()?; + Ok(response) } /// Lists all consumers in the given virtual host. pub fn list_consumers_in(&self, virtual_host: &str) -> Result> { - let response = self.http_get(&format!("consumers/{}", virtual_host))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("consumers", virtual_host), None, None)?; + let response = response.json()?; + Ok(response) } /// Returns information about a cluster node. pub fn get_node_info(&self, name: &str) -> Result { - let response = self.http_get(&format!("nodes/{}", name))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get(path!("nodes", name), None, None)?; + let response = response.json()?; + Ok(response) } /// Returns information about a virtual host. pub fn get_vhost(&self, name: &str) -> Result { - let response = self.http_get(&format!("vhosts/{}", self.percent_encode(name)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get(path!("vhosts", name), None, None)?; + let response = response.json()?; + Ok(response) } /// Returns information about a user in the internal database. pub fn get_user(&self, name: &str) -> Result { - let response = self.http_get(&format!("users/{}", self.percent_encode(name)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2.json::().map_err(Error::from) + let response = self.http_get(path!("users", name), None, None)?; + let response = response.json()?; + Ok(response) } /// Returns information about a queue or stream. pub fn get_queue_info(&self, virtual_host: &str, name: &str) -> Result { - let response = self.http_get(&format!( - "queues/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(name) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get(path!("queues", virtual_host, name), None, None)?; + let response = response.json()?; + Ok(response) } /// Returns information about an exchange. @@ -492,15 +431,9 @@ where virtual_host: &str, name: &str, ) -> Result { - let response = self.http_get(&format!( - "exchanges/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(name) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get(path!("exchanges", virtual_host, name), None, None)?; + let response = response.json()?; + Ok(response) } /// Creates a virtual host. @@ -514,11 +447,7 @@ where /// /// See [`VirtualHostParams`] pub fn update_vhost(&self, params: &VirtualHostParams) -> Result<()> { - let response = self.http_put( - &format!("vhosts/{}", self.percent_encode(params.name)), - params, - )?; - self.ok_or_status_code_error(response)?; + let _response = self.http_put(path!("vhosts", params.name), params, None, None)?; Ok(()) } @@ -526,67 +455,40 @@ where /// /// See [`UserParams`] and [`crate::password_hashing`]. pub fn create_user(&self, params: &UserParams) -> Result<()> { - let response = self.http_put( - &format!("users/{}", self.percent_encode(params.name)), - params, - )?; - self.ok_or_status_code_error(response)?; + let _response = self.http_put(path!("users", params.name), params, None, None)?; Ok(()) } pub fn declare_permissions(&self, params: &Permissions) -> Result<()> { - let response = self.http_put( + let _response = self.http_put( // /api/permissions/vhost/user - &format!( - "permissions/{}/{}", - self.percent_encode(params.vhost), - self.percent_encode(params.user) - ), + path!("permissions", params.vhost, params.user), params, + None, + None, )?; - self.ok_or_status_code_error(response)?; Ok(()) } pub fn grant_permissions(&self, vhost: &str, user: &str) -> Result<()> { - let response = self.http_delete(&format!( - "permissions/{}/{}", - self.percent_encode(vhost), - self.percent_encode(user) - ))?; - self.ok_or_status_code_error(response)?; + let _response = self.http_delete(path!("permissions", vhost, user), None, None)?; Ok(()) } - pub fn declare_queue(&self, virtual_host: &str, params: &QueueParams) -> Result<()> { - let response = self.http_put( - &format!( - "queues/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(params.name) - ), - params, - )?; - self.ok_or_status_code_error(response)?; + pub fn declare_queue(&self, vhost: &str, params: &QueueParams) -> Result<()> { + let _response = self.http_put(path!("queues", vhost, params.name), params, None, None)?; Ok(()) } - pub fn declare_exchange(&self, virtual_host: &str, params: &ExchangeParams) -> Result<()> { - let response = self.http_put( - &format!( - "exchanges/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(params.name) - ), - params, - )?; - self.ok_or_status_code_error(response)?; + pub fn declare_exchange(&self, vhost: &str, params: &ExchangeParams) -> Result<()> { + let _response = + self.http_put(path!("exchanges", vhost, params.name), params, None, None)?; Ok(()) } pub fn bind_queue( &self, - virtual_host: &str, + vhost: &str, queue: &str, exchange: &str, routing_key: Option<&str>, @@ -600,20 +502,18 @@ where body.insert("arguments".to_owned(), json!(args)); } - let path = format!( - "bindings/{}/e/{}/q/{}", - self.percent_encode(virtual_host), - self.percent_encode(exchange), - self.percent_encode(queue) - ); - let response = self.http_post(&path, &body)?; - self.ok_or_status_code_error(response)?; + let _response = self.http_post( + path!("bindings", vhost, "e", exchange, "q", queue), + &body, + None, + None, + )?; Ok(()) } pub fn bind_exchange( &self, - virtual_host: &str, + vhost: &str, destination: &str, source: &str, routing_key: Option<&str>, @@ -627,57 +527,43 @@ where body.insert("arguments".to_owned(), json!(args)); } - let path = format!( - "bindings/{}/e/{}/e/{}", - self.percent_encode(virtual_host), - self.percent_encode(source), - self.percent_encode(destination) - ); - let response = self.http_post(&path, &body)?; - self.ok_or_status_code_error(response)?; + let _response = self.http_post( + path!("bindings", vhost, "e", source, "e", destination), + &body, + None, + None, + )?; Ok(()) } - pub fn delete_vhost(&self, virtual_host: &str) -> Result<()> { - let response = - self.http_delete(&format!("vhosts/{}", self.percent_encode(virtual_host)))?; - self.ok_or_status_code_error_except_404(response)?; + pub fn delete_vhost(&self, vhost: &str) -> Result<()> { + let _response = + self.http_delete(path!("vhosts", vhost), Some(StatusCode::NOT_FOUND), None)?; Ok(()) } pub fn delete_user(&self, username: &str) -> Result<()> { - let response = self.http_delete(&format!("users/{}", self.percent_encode(username)))?; - self.ok_or_status_code_error_except_404(response)?; + let _response = + self.http_delete(path!("users", username), Some(StatusCode::NOT_FOUND), None)?; Ok(()) } - pub fn clear_permissions(&self, virtual_host: &str, username: &str) -> Result<()> { - let response = self.http_delete(&format!( - "permissions/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(username) - ))?; - self.ok_or_status_code_error_except_404(response)?; + pub fn clear_permissions(&self, vhost: &str, username: &str) -> Result<()> { + let _response = self.http_delete( + path!("permissions", vhost, username), + Some(StatusCode::NOT_FOUND), + None, + )?; Ok(()) } - pub fn delete_queue(&self, virtual_host: &str, name: &str) -> Result<()> { - let response = self.http_delete(&format!( - "queues/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(name) - ))?; - self.ok_or_status_code_error(response)?; + pub fn delete_queue(&self, vhost: &str, name: &str) -> Result<()> { + let _response = self.http_delete(path!("queues", vhost, name), None, None)?; Ok(()) } - pub fn delete_exchange(&self, virtual_host: &str, name: &str) -> Result<()> { - let response = self.http_delete(&format!( - "exchanges/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(name) - ))?; - self.ok_or_status_code_error(response)?; + pub fn delete_exchange(&self, vhost: &str, name: &str) -> Result<()> { + let _response = self.http_delete(path!("exchanges", vhost, name), None, None)?; Ok(()) } @@ -709,64 +595,58 @@ where 0 => Err(Error::NotFound()), 1 => { let first_key = bs.first().unwrap().properties_key.clone(); + let path_appreviation = destination_type.path_appreviation(); let path = match first_key { Some(pk) => { - format!( + path!( // /api/bindings/vhost/e/exchange/[eq]/destination/props - "bindings/{}/e/{}/{}/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(source), - destination_type.path_appreviation(), - self.percent_encode(destination), - self.percent_encode(pk.as_str()) + "bindings", + virtual_host, + "e", + source, + path_appreviation, + destination, + pk.as_str() ) } None => { - format!( + path!( // /api/bindings/vhost/e/exchange/[eq]/destination/ - "bindings/{}/e/{}/{}/{}", - self.percent_encode(virtual_host), - self.percent_encode(source), - destination_type.path_appreviation(), - self.percent_encode(destination), + "bindings", + virtual_host, + "e", + source, + path_appreviation, + destination ) } }; - let response = self.http_delete(&path)?; - self.ok_or_status_code_error(response) + let response = self.http_delete(&path, None, None)?; + Ok(response) } _ => Err(Error::ManyMatchingBindings()), } } pub fn purge_queue(&self, virtual_host: &str, name: &str) -> Result<()> { - let response = self.http_delete(&format!( - "queues/{}/{}/contents", - self.percent_encode(virtual_host), - self.percent_encode(name) - ))?; - self.ok_or_status_code_error(response)?; + let _response = + self.http_delete(path!("queues", virtual_host, name, "contents"), None, None)?; Ok(()) } pub fn list_runtime_parameters(&self) -> Result> { - let response = self.http_get("parameters")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("parameters", None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_runtime_parameters_of_component( &self, component: &str, ) -> Result> { - let path = format!("parameters/{}", self.percent_encode(component)); - let response = self.http_get(&path)?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("parameters", component), None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_runtime_parameters_of_component_in( @@ -774,16 +654,9 @@ where component: &str, vhost: &str, ) -> Result> { - let path = format!( - "parameters/{}/{}", - self.percent_encode(component), - self.percent_encode(vhost) - ); - let response = self.http_get(&path)?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("parameters", component, vhost), None, None)?; + let response = response.json()?; + Ok(response) } pub fn get_runtime_parameter( @@ -792,40 +665,24 @@ where vhost: &str, name: &str, ) -> Result { - let path = format!( - "parameters/{}/{}/{}", - self.percent_encode(component), - self.percent_encode(vhost), - self.percent_encode(name) - ); - let response = self.http_get(&path)?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get(path!("parameters", component, vhost, name), None, None)?; + let response = response.json()?; + Ok(response) } pub fn upsert_runtime_parameter(&self, param: &RuntimeParameterDefinition) -> Result<()> { - let path = format!( - "parameters/{}/{}/{}", - self.percent_encode(¶m.component), - self.percent_encode(¶m.vhost), - self.percent_encode(¶m.name) - ); - let response = self.http_put(&path, ¶m)?; - self.ok_or_status_code_error(response)?; + let _response = self.http_put( + path!("parameters", param.component, param.vhost, param.name), + ¶m, + None, + None, + )?; Ok(()) } pub fn clear_runtime_parameter(&self, component: &str, vhost: &str, name: &str) -> Result<()> { - let path = format!( - "parameters/{}/{}/{}", - self.percent_encode(component), - self.percent_encode(vhost), - self.percent_encode(name) - ); - let response = self.http_delete(&path)?; - self.ok_or_status_code_error(response)?; + let _response = + self.http_delete(path!("parameters", component, vhost, name), None, None)?; Ok(()) } @@ -850,39 +707,31 @@ where username: &str, limit: EnforcedLimitParams, ) -> Result<()> { - let path = format!("user-limits/{}/{}", username, String::from(limit.kind)); - - let mut body = Map::::new(); - body.insert("value".to_owned(), json!(limit.value)); - - let response = self.http_put(&path, &body)?; - self.ok_or_status_code_error(response)?; + let body = json!({"value": limit.value}); + let _response = self.http_put( + path!("user-limits", username, limit.kind), + &body, + None, + None, + )?; Ok(()) } pub fn clear_user_limit(&self, username: &str, kind: UserLimitTarget) -> Result<()> { - let path = format!("user-limits/{}/{}", username, String::from(kind)); - - let response = self.http_delete(&path)?; - self.ok_or_status_code_error(response)?; + let _response = self.http_delete(path!("user-limits", username, kind), None, None)?; Ok(()) } pub fn list_all_user_limits(&self) -> Result> { - let response = self.http_get("user-limits")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("user-limits", None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_user_limits(&self, username: &str) -> Result> { - let path = format!("user-limits/{}", username); - let response = self.http_get(&path)?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("user-limits", username), None, None)?; + let response = response.json()?; + Ok(response) } pub fn set_vhost_limit( @@ -890,203 +739,141 @@ where vhost: &str, limit: EnforcedLimitParams, ) -> Result<()> { - let path = format!( - "vhost-limits/{}/{}", - self.percent_encode(vhost), - String::from(limit.kind) - ); - - let mut body = Map::::new(); - body.insert("value".to_owned(), json!(limit.value)); - - let response = self.http_put(&path, &body)?; - self.ok_or_status_code_error(response)?; + let body = json!({"value": limit.value}); + let _response = + self.http_put(path!("vhost-limits", vhost, limit.kind), &body, None, None)?; Ok(()) } pub fn clear_vhost_limit(&self, vhost: &str, kind: VirtualHostLimitTarget) -> Result<()> { - let path = format!( - "vhost-limits/{}/{}", - self.percent_encode(vhost), - String::from(kind) - ); - - let response = self.http_delete(&path)?; - self.ok_or_status_code_error_except_404(response)?; + let _response = self.http_delete( + path!("vhost-limits", vhost, kind), + Some(StatusCode::NOT_FOUND), + None, + )?; Ok(()) } pub fn list_all_vhost_limits(&self) -> Result> { - let response = self.http_get("vhost-limits")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("vhost-limits", None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_vhost_limits(&self, vhost: &str) -> Result> { - let path = format!("vhost-limits/{}", self.percent_encode(vhost)); - let response = self.http_get(&path)?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("vhost-limits", vhost), None, None)?; + let response = response.json()?; + Ok(response) } pub fn get_cluster_name(&self) -> Result { - let response = self.http_get("cluster-name")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get("cluster-name", None, None)?; + let response = response.json()?; + Ok(response) } pub fn set_cluster_name(&self, new_name: &str) -> Result<()> { - let mut map = HashMap::new(); - map.insert("name", new_name); - - let response = self.http_put("cluster-name", &map)?; - self.ok_or_status_code_error(response)?; + let body = json!({"name": new_name}); + let _response = self.http_put("cluster-name", &body, None, None)?; Ok(()) } pub fn get_policy(&self, vhost: &str, name: &str) -> Result { - let response = self.http_get(&format!( - "policies/{}/{}", - self.percent_encode(vhost), - self.percent_encode(name) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2.json::().map_err(Error::from) + let response = self.http_get(path!("policies", vhost, name), None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_policies(&self) -> Result> { - let response = self.http_get("policies")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("policies", None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_policies_in(&self, vhost: &str) -> Result> { - let response = self.http_get(&format!("policies/{}", self.percent_encode(vhost)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("policies", vhost), None, None)?; + let response = response.json()?; + Ok(response) } pub fn declare_policy(&self, params: &PolicyParams) -> Result<()> { - let response = self.http_put( - &format!( - "policies/{}/{}", - self.percent_encode(params.vhost), - self.percent_encode(params.name) - ), + let _response = self.http_put( + path!("policies", params.vhost, params.name), params, + None, + None, )?; - self.ok_or_status_code_error(response)?; Ok(()) } pub fn delete_policy(&self, vhost: &str, name: &str) -> Result<()> { - let response = self.http_delete(&format!( - "policies/{}/{}", - self.percent_encode(vhost), - self.percent_encode(name) - ))?; - self.ok_or_status_code_error_except_404(response)?; + let _response = self.http_delete( + path!("policies", vhost, name), + Some(StatusCode::NOT_FOUND), + None, + )?; Ok(()) } pub fn get_operator_policy(&self, vhost: &str, name: &str) -> Result { - let response = self.http_get(&format!( - "operator-policies/{}/{}", - self.percent_encode(vhost), - self.percent_encode(name) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2.json::().map_err(Error::from) + let response = self.http_get(path!("operator-policies", vhost, name), None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_operator_policies(&self) -> Result> { - let response = self.http_get("operator-policies")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("operator-policies", None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_operator_policies_in(&self, vhost: &str) -> Result> { - let response = - self.http_get(&format!("operator-policies/{}", self.percent_encode(vhost)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("operator-policies", vhost), None, None)?; + let response = response.json()?; + Ok(response) } pub fn declare_operator_policy(&self, params: &PolicyParams) -> Result<()> { - let response = self.http_put( - &format!( - "operator-policies/{}/{}", - self.percent_encode(params.vhost), - self.percent_encode(params.name) - ), + let _response = self.http_put( + path!("operator-policies", params.vhost, params.name), params, + None, + None, )?; - self.ok_or_status_code_error_except_404(response)?; Ok(()) } pub fn delete_operator_policy(&self, vhost: &str, name: &str) -> Result<()> { - let response = self.http_delete(&format!( - "operator-policies/{}/{}", - self.percent_encode(vhost), - self.percent_encode(name) - ))?; - self.ok_or_status_code_error_except_404(response)?; + let _response = self.http_delete( + path!("operator-policies", vhost, name), + Some(StatusCode::NOT_FOUND), + None, + )?; Ok(()) } pub fn list_permissions(&self) -> Result> { - let response = self.http_get("permissions")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get("permissions", None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_permissions_in(&self, vhost: &str) -> Result> { - let response = self.http_get(&format!( - "vhosts/{}/permissions", - self.percent_encode(vhost) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("vhosts", vhost, "permissions"), None, None)?; + let response = response.json()?; + Ok(response) } pub fn list_permissions_of(&self, user: &str) -> Result> { - let response = - self.http_get(&format!("users/{}/permissions", self.percent_encode(user)))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_get(path!("users", user, "permissions"), None, None)?; + let response = response.json()?; + Ok(response) } pub fn get_permissions(&self, vhost: &str, user: &str) -> Result { - let response = self.http_get(&format!( - "permissions/{}/{}", - self.percent_encode(vhost), - self.percent_encode(user) - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get(path!("permissions", vhost, user), None, None)?; + let response = response.json()?; + Ok(response) } // @@ -1094,9 +881,7 @@ where // pub fn rebalance_queue_leaders(&self) -> Result<()> { - let m: HashMap = HashMap::new(); - self.http_post("rebalance/queues", &m)?; - + self.http_post("rebalance/queues", &json!({}), None, None)?; Ok(()) } @@ -1108,22 +893,19 @@ where } pub fn export_definitions_as_string(&self) -> Result { - let response = self.http_get("definitions")?; - let response2 = self.ok_or_status_code_error(response)?; - response2.text().map_err(Error::from) + let response = self.http_get("definitions", None, None)?; + let response = response.text()?; + Ok(response) } pub fn export_definitions_as_data(&self) -> Result { - let response = self.http_get("definitions")?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_get("definitions", None, None)?; + let response = response.json()?; + Ok(response) } pub fn import_definitions(&self, definitions: Value) -> Result<()> { - let response = self.http_post("definitions", &definitions)?; - self.ok_or_status_code_error(response)?; + self.http_post("definitions", &definitions, None, None)?; Ok(()) } @@ -1140,16 +922,17 @@ where } pub fn health_check_if_node_is_quorum_critical(&self) -> Result<()> { - let response = self.http_get("health/checks/node-is-quorum-critical")?; - let response2 = self.ok_or_status_code_error_except_503(response)?; + let response = self.http_get( + "health/checks/node-is-quorum-critical", + None, + Some(StatusCode::SERVICE_UNAVAILABLE), + )?; - if response2.status().is_success() { + if response.status().is_success() { return Ok(()); } - let failure_details = response2 - .json::() - .map_err(Error::from)?; + let failure_details = response.json()?; Err(Error::HealthCheckFailed( responses::HealthCheckFailureDetails::NodeIsQuorumCritical(failure_details), )) @@ -1166,12 +949,6 @@ where payload: &str, properties: requests::MessageProperties, ) -> Result { - let url = format!( - "exchanges/{}/{}/publish", - self.percent_encode(vhost), - self.percent_encode(exchange) - ); - let body = serde_json::json!({ "routing_key": routing_key, "payload": payload, @@ -1179,12 +956,14 @@ where "properties": properties, }); - let response = self.http_post(&url, &body)?; - - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::() - .map_err(Error::from) + let response = self.http_post( + path!("exchanges", vhost, exchange, "publish"), + &body, + None, + None, + )?; + let response = response.json()?; + Ok(response) } pub fn get_messages( @@ -1194,24 +973,15 @@ where count: u32, ack_mode: &str, ) -> Result> { - let url = format!( - "queues/{}/{}/get", - self.percent_encode(vhost), - self.percent_encode(queue) - ); - let body = serde_json::json!({ "count": count, "ackmode": ack_mode, "encoding": "auto" }); - let response = self.http_post(&url, &body)?; - - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) + let response = self.http_post(path!("queues", vhost, queue, "get"), &body, None, None)?; + let response = response.json()?; + Ok(response) } // @@ -1219,16 +989,12 @@ where // fn health_check_alarms(&self, path: &str) -> Result<()> { - let response = self.http_get(path)?; - let response2 = self.ok_or_status_code_error_except_503(response)?; - - if response2.status().is_success() { + let response = self.http_get(path, None, Some(StatusCode::SERVICE_UNAVAILABLE))?; + if response.status().is_success() { return Ok(()); } - let failure_details = response2 - .json::() - .map_err(Error::from)?; + let failure_details = response.json()?; Err(Error::HealthCheckFailed( responses::HealthCheckFailureDetails::AlarmCheck(failure_details), )) @@ -1236,38 +1002,50 @@ where fn list_exchange_bindings_with_source_or_destination( &self, - virtual_host: &str, + vhost: &str, exchange: &str, vertex: BindindVertex, ) -> Result> { - let response = self.http_get(&format!( - "exchanges/{}/{}/bindings/{}", - self.percent_encode(virtual_host), - self.percent_encode(exchange), - vertex - ))?; - let response2 = self.ok_or_status_code_error(response)?; - response2 - .json::>() - .map_err(Error::from) - } - - fn percent_encode(&self, value: &str) -> String { - utf8_percent_encode(value, NON_ALPHANUMERIC).to_string() + let response = self.http_get( + path!("exchanges", vhost, exchange, "bindings", vertex), + None, + None, + )?; + let response = response.json()?; + Ok(response) } - fn http_get(&self, path: &str) -> crate::blocking::Result { + fn http_get( + &self, + path: S, + client_expect_code_error: Option, + server_expect_code_error: Option, + ) -> Result + where + S: AsRef, + { let response = self .client .get(self.rooted_path(path)) .basic_auth(&self.username, Some(&self.password)) - .send(); - - self.ok_or_http_client_error(response) + .send()?; + let response = self.ok_or_status_code_error( + response, + client_expect_code_error, + server_expect_code_error, + )?; + Ok(response) } - fn http_put(&self, path: &str, payload: &T) -> crate::blocking::Result + fn http_put( + &self, + path: S, + payload: &T, + client_expect_code_error: Option, + server_expect_code_error: Option, + ) -> Result where + S: AsRef, T: Serialize, { let response = self @@ -1275,13 +1053,24 @@ where .put(self.rooted_path(path)) .json(&payload) .basic_auth(&self.username, Some(&self.password)) - .send(); - - self.ok_or_http_client_error(response) + .send()?; + let response = self.ok_or_status_code_error( + response, + client_expect_code_error, + server_expect_code_error, + )?; + Ok(response) } - fn http_post(&self, path: &str, payload: &T) -> crate::blocking::Result + fn http_post( + &self, + path: S, + payload: &T, + client_expect_code_error: Option, + server_expect_code_error: Option, + ) -> Result where + S: AsRef, T: Serialize, { let response = self @@ -1289,95 +1078,90 @@ where .post(self.rooted_path(path)) .json(&payload) .basic_auth(&self.username, Some(&self.password)) - .send(); - - self.ok_or_http_client_error(response) + .send()?; + let response = self.ok_or_status_code_error( + response, + client_expect_code_error, + server_expect_code_error, + )?; + Ok(response) } - fn http_delete(&self, path: &str) -> crate::blocking::Result { + fn http_delete( + &self, + path: S, + client_expect_code_error: Option, + server_expect_code_error: Option, + ) -> Result + where + S: AsRef, + { let response = self .client .delete(self.rooted_path(path)) .basic_auth(&self.username, Some(&self.password)) - .send(); - self.ok_or_http_client_error(response) + .send()?; + let response = self.ok_or_status_code_error( + response, + client_expect_code_error, + server_expect_code_error, + )?; + Ok(response) } - fn http_delete_with_headers( + fn http_delete_with_headers( &self, - path: &str, + path: S, headers: HeaderMap, - ) -> crate::blocking::Result { + client_expect_code_error: Option, + server_expect_code_error: Option, + ) -> Result + where + S: AsRef, + { let response = self .client .delete(self.rooted_path(path)) .basic_auth(&self.username, Some(&self.password)) .headers(headers) - .send(); - self.ok_or_http_client_error(response) - } - - fn ok_or_http_client_error( - &self, - result: reqwest::Result, - ) -> Result { - match result { - Ok(val) => Ok(val), - Err(e) => Err(Error::from(e)), - } - } - - fn ok_or_status_code_error(&self, response: HttpClientResponse) -> Result { - let status = response.status(); - if status.is_client_error() { - return Err(Error::ClientErrorResponse(status.as_u16(), response)); - } - - if status.is_server_error() { - return Err(Error::ServerErrorResponse(status.as_u16(), response)); - } - - Ok(response) - } - - fn ok_or_status_code_error_except_404( - &self, - response: HttpClientResponse, - ) -> Result { - let status = response.status(); - - // Do not consider 404s an error to allow for idempotent deletes - if status.is_client_error() && status.as_u16() != 404 { - return Err(Error::ClientErrorResponse(status.as_u16(), response)); - } - - if status.is_server_error() { - return Err(Error::ServerErrorResponse(status.as_u16(), response)); - } - + .send()?; + let response = self.ok_or_status_code_error( + response, + client_expect_code_error, + server_expect_code_error, + )?; Ok(response) } - fn ok_or_status_code_error_except_503( + fn ok_or_status_code_error( &self, response: HttpClientResponse, + client_expect_code_error: Option, + server_expect_code_error: Option, ) -> Result { let status = response.status(); if status.is_client_error() { - return Err(Error::ClientErrorResponse(status.as_u16(), response)); + match client_expect_code_error { + Some(expect) if status == expect => {} + _ => return Err(Error::ClientErrorResponse(status, response)), + } } - // 503 Service Unavailable is used to indicate a health check failure. - // In this case, we want to parse the response and provide a more specific error. - if status.is_server_error() && status.as_u16() != 503 { - return Err(Error::ServerErrorResponse(status.as_u16(), response)); + if status.is_server_error() { + match server_expect_code_error { + Some(expect) if status == expect => {} + _ => return Err(Error::ServerErrorResponse(status, response)), + } } Ok(response) } - fn rooted_path(&self, path: &str) -> String { - format!("{}/api/{}", self.endpoint, path) + fn rooted_path(&self, path: S) -> String + where + S: AsRef, + { + format!("{}/api/{}", self.endpoint, path.as_ref()) } } @@ -1387,16 +1171,17 @@ impl Default for Client<&'static str, &'static str, &'static str> { } } +#[derive(Debug, Clone, Copy)] enum BindindVertex { Source, Destination, } -impl Display for BindindVertex { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl AsRef for BindindVertex { + fn as_ref(&self) -> &str { match self { - Self::Source => write!(f, "source"), - Self::Destination => write!(f, "destination"), + Self::Source => "source", + Self::Destination => "destination", } } } diff --git a/src/commons.rs b/src/commons.rs index b82c9ad..b0e154a 100644 --- a/src/commons.rs +++ b/src/commons.rs @@ -1,6 +1,6 @@ -use serde::{Deserialize, Serialize}; use std::fmt; -use std::fmt::Formatter; + +use serde::{Deserialize, Serialize}; /// Exchange types. Most variants are for exchange types included with modern RabbitMQ distributions. /// For custom types provided by 3rd party plugins, use the `Plugin(String)` variant. @@ -129,7 +129,7 @@ pub enum BindingDestinationType { } impl fmt::Display for BindingDestinationType { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { BindingDestinationType::Queue => write!(f, "queue")?, BindingDestinationType::Exchange => write!(f, "exchange")?, @@ -189,7 +189,7 @@ pub enum PolicyTarget { } impl fmt::Display for PolicyTarget { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", Into::::into(self.clone()))?; Ok(()) @@ -244,12 +244,11 @@ pub enum VirtualHostLimitTarget { MaxQueues, } -#[allow(clippy::to_string_trait_impl)] -impl ToString for VirtualHostLimitTarget { - fn to_string(&self) -> String { +impl AsRef for VirtualHostLimitTarget { + fn as_ref(&self) -> &str { match self { - VirtualHostLimitTarget::MaxConnections => "max-connections".to_owned(), - VirtualHostLimitTarget::MaxQueues => "max-queues".to_owned(), + VirtualHostLimitTarget::MaxConnections => "max-connections", + VirtualHostLimitTarget::MaxQueues => "max-queues", } } } @@ -276,10 +275,7 @@ impl From for VirtualHostLimitTarget { impl From for String { fn from(value: VirtualHostLimitTarget) -> Self { - match value { - VirtualHostLimitTarget::MaxConnections => "max-connections".to_owned(), - VirtualHostLimitTarget::MaxQueues => "max-queues".to_owned(), - } + value.as_ref().to_string() } } @@ -290,12 +286,11 @@ pub enum UserLimitTarget { MaxChannels, } -#[allow(clippy::to_string_trait_impl)] -impl ToString for UserLimitTarget { - fn to_string(&self) -> String { +impl AsRef for UserLimitTarget { + fn as_ref(&self) -> &str { match self { - UserLimitTarget::MaxConnections => "max-connections".to_owned(), - UserLimitTarget::MaxChannels => "max-channels".to_owned(), + UserLimitTarget::MaxConnections => "max-connections", + UserLimitTarget::MaxChannels => "max-channels", } } } @@ -322,9 +317,6 @@ impl From for UserLimitTarget { impl From for String { fn from(value: UserLimitTarget) -> Self { - match value { - UserLimitTarget::MaxConnections => "max-connections".to_owned(), - UserLimitTarget::MaxChannels => "max-channels".to_owned(), - } + value.as_ref().to_string() } } diff --git a/src/lib.rs b/src/lib.rs index 88ad2c0..40f46f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,3 +22,5 @@ pub mod password_hashing; pub mod requests; /// API response types pub mod responses; + +mod utils; diff --git a/src/responses.rs b/src/responses.rs index daa8b66..76e804f 100644 --- a/src/responses.rs +++ b/src/responses.rs @@ -1,5 +1,4 @@ -use std::borrow::Borrow; -use std::fmt; +use std::{fmt, ops}; use crate::commons::{BindingDestinationType, PolicyTarget}; use serde::{ @@ -55,6 +54,7 @@ impl fmt::Display for TagList { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct XArguments(pub Map); + impl fmt::Display for XArguments { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let coll = &self.0; @@ -69,6 +69,15 @@ impl fmt::Display for XArguments { #[derive(Debug, Serialize, Deserialize, Clone, Default)] #[serde(transparent)] pub struct RuntimeParameterValue(pub Map); + +impl ops::Deref for RuntimeParameterValue { + type Target = Map; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + impl fmt::Display for RuntimeParameterValue { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let coll = &self.0; @@ -80,26 +89,6 @@ impl fmt::Display for RuntimeParameterValue { } } -impl RuntimeParameterValue { - #[inline] - pub fn get(&self, key: &Q) -> Option<&serde_json::Value> - where - String: Borrow, - Q: ?Sized + Ord + Eq + core::hash::Hash, - { - self.0.get(key) - } - - #[inline] - pub fn contains_key(&self, key: &Q) -> bool - where - String: Borrow, - Q: ?Sized + Ord + Eq + core::hash::Hash, - { - self.0.contains_key(key) - } -} - #[derive(Debug, Deserialize, Clone)] pub struct NodeList(Vec); @@ -146,23 +135,11 @@ pub struct VirtualHost { #[derive(Debug, Deserialize, Clone)] pub struct EnforcedLimits(pub Map); -impl EnforcedLimits { - #[inline] - pub fn get(&self, key: &Q) -> Option<&serde_json::Value> - where - String: Borrow, - Q: ?Sized + Ord + Eq + core::hash::Hash, - { - self.0.get(key) - } +impl ops::Deref for EnforcedLimits { + type Target = Map; - #[inline] - pub fn contains_key(&self, key: &Q) -> bool - where - String: Borrow, - Q: ?Sized + Ord + Eq + core::hash::Hash, - { - self.0.contains_key(key) + fn deref(&self) -> &Self::Target { + &self.0 } } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..527c3bf --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,25 @@ +#[macro_export] +macro_rules! path_one_part { + ($val:expr, $part:literal) => { + $val.push('/'); + $val.push_str($part); + }; + ($val:expr, $part:expr) => { + let part = $part.as_ref(); + let encoded = + percent_encoding::utf8_percent_encode(part, percent_encoding::NON_ALPHANUMERIC); + $val.push('/'); + $val.extend(encoded); + }; +} + +#[macro_export] +macro_rules! path { + ($part1:expr, $($part:expr),+) => {{ + let mut url = String::from($part1); + $( + $crate::path_one_part!(&mut url, $part); + )+ + url + }} +} diff --git a/tests/user_limit_tests.rs b/tests/user_limit_tests.rs index 251a369..60e0b6a 100644 --- a/tests/user_limit_tests.rs +++ b/tests/user_limit_tests.rs @@ -34,14 +34,14 @@ fn test_list_all_user_limits() { let vec = result3.unwrap(); assert!(vec.iter().any(|it| it.username == params.name)); - let key1 = UserLimitTarget::MaxConnections.to_string(); + let key1 = UserLimitTarget::MaxConnections; assert!(!vec .iter() - .any(|it| it.username == params.name && it.limits.get(&key1).is_some())); - let key2 = UserLimitTarget::MaxChannels.to_string(); + .any(|it| it.username == params.name && it.limits.get(key1.as_ref()).is_some())); + let key2 = UserLimitTarget::MaxChannels; assert!(vec .iter() - .any(|it| it.username == params.name && it.limits.get(&key2).is_some())); + .any(|it| it.username == params.name && it.limits.get(key2.as_ref()).is_some())); rc.delete_user(params.name).unwrap(); } @@ -72,14 +72,14 @@ fn test_list_user_limits() { assert!(result3.is_ok()); let vec = result3.unwrap(); - let key1 = UserLimitTarget::MaxChannels.to_string(); + let key1 = UserLimitTarget::MaxChannels; assert!(vec .iter() - .any(|it| it.username == params.name && it.limits.get(&key1).is_some())); - let key2 = UserLimitTarget::MaxConnections.to_string(); + .any(|it| it.username == params.name && it.limits.get(key1.as_ref()).is_some())); + let key2 = UserLimitTarget::MaxConnections; assert!(!vec .iter() - .any(|it| it.username == params.name && it.limits.get(&key2).is_some())); + .any(|it| it.username == params.name && it.limits.get(key2.as_ref()).is_some())); rc.delete_user(params.name).unwrap(); } diff --git a/tests/virtual_host_limit_tests.rs b/tests/virtual_host_limit_tests.rs index 07b3502..a1c7225 100644 --- a/tests/virtual_host_limit_tests.rs +++ b/tests/virtual_host_limit_tests.rs @@ -25,14 +25,14 @@ fn test_list_all_vhost_limits() { let vec = result3.unwrap(); assert!(vec.iter().any(|it| it.vhost == vh_params.name)); - let key1 = VirtualHostLimitTarget::MaxConnections.to_string(); + let key1 = VirtualHostLimitTarget::MaxConnections; assert!(!vec .iter() - .any(|it| it.vhost == vh_params.name && it.limits.get(&key1).is_some())); - let key2 = VirtualHostLimitTarget::MaxQueues.to_string(); + .any(|it| it.vhost == vh_params.name && it.limits.get(key1.as_ref()).is_some())); + let key2 = VirtualHostLimitTarget::MaxQueues; assert!(vec .iter() - .any(|it| it.vhost == vh_params.name && it.limits.get(&key2).is_some())); + .any(|it| it.vhost == vh_params.name && it.limits.get(key2.as_ref()).is_some())); rc.delete_vhost(vh_params.name).unwrap(); } @@ -54,14 +54,14 @@ fn test_list_vhost_limits() { assert!(result3.is_ok()); let vec = result3.unwrap(); - let key1 = VirtualHostLimitTarget::MaxConnections.to_string(); + let key1 = VirtualHostLimitTarget::MaxConnections; assert!(vec .iter() - .any(|it| it.vhost == vh_params.name && it.limits.get(&key1).is_some())); - let key2 = VirtualHostLimitTarget::MaxQueues.to_string(); + .any(|it| it.vhost == vh_params.name && it.limits.get(key1.as_ref()).is_some())); + let key2 = VirtualHostLimitTarget::MaxQueues; assert!(!vec .iter() - .any(|it| it.vhost == vh_params.name && it.limits.get(&key2).is_some())); + .any(|it| it.vhost == vh_params.name && it.limits.get(key2.as_ref()).is_some())); rc.delete_vhost(vh_params.name).unwrap(); }