Skip to content

Commit

Permalink
clippy cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyandrews committed Dec 3, 2022
1 parent 5b771ec commit bfb3a60
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 65 deletions.
22 changes: 0 additions & 22 deletions build.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ impl GooseConfiguration {
message: "",
},
])
.unwrap_or_else(|| "".to_string());
.unwrap_or_default();

// Initialize the Goose logger.
self.initialize_goose_logger();
Expand Down
90 changes: 55 additions & 35 deletions src/goose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ macro_rules! scenario {
};
}

/// Goose transactions return a result, which is empty on success, or contains a
/// Goose transactions return a result, which is empty on success, or contains a boxed
/// [`TransactionError`](./enum.TransactionError.html) on error.
pub type TransactionResult = Result<(), TransactionError>;
pub type TransactionResult = Result<(), Box<TransactionError>>;

/// An enumeration of all errors a [`Transaction`](./struct.Transaction.html) can return.
#[derive(Debug)]
Expand Down Expand Up @@ -688,7 +688,7 @@ impl fmt::Display for GooseMethod {

/// Convert [`http::method::Method`](https://docs.rs/http/*/http/method/struct.Method.html)
/// to [`GooseMethod`](./enum.GooseMethod.html).
pub fn goose_method_from_method(method: Method) -> Result<GooseMethod, TransactionError> {
pub fn goose_method_from_method(method: Method) -> Result<GooseMethod, Box<TransactionError>> {
Ok(match method {
Method::DELETE => GooseMethod::Delete,
Method::GET => GooseMethod::Get,
Expand All @@ -697,7 +697,7 @@ pub fn goose_method_from_method(method: Method) -> Result<GooseMethod, Transacti
Method::POST => GooseMethod::Post,
Method::PUT => GooseMethod::Put,
_ => {
return Err(TransactionError::InvalidMethod { method });
return Err(Box::new(TransactionError::InvalidMethod { method }));
}
})
}
Expand Down Expand Up @@ -1087,7 +1087,7 @@ impl GooseUser {
/// current scenario)
/// 3. [`GooseDefault::Host`](../config/enum.GooseDefault.html#variant.Host) (default host
/// defined for the current load test)
pub fn build_url(&self, path: &str) -> Result<String, TransactionError> {
pub fn build_url(&self, path: &str) -> Result<String, Box<TransactionError>> {
// If URL includes a host, simply use it.
if let Ok(parsed_path) = Url::parse(path) {
if let Some(_host) = parsed_path.host() {
Expand All @@ -1096,7 +1096,10 @@ impl GooseUser {
}

// Otherwise use the `base_url`.
Ok(self.base_url.join(path)?.to_string())
match self.base_url.join(path) {
Ok(u) => Ok(u.to_string()),
Err(e) => Err(Box::new(e.into())),
}
}

/// A helper to make a `GET` request of a path and collect relevant metrics.
Expand Down Expand Up @@ -1124,7 +1127,7 @@ impl GooseUser {
/// Ok(())
/// }
/// ```
pub async fn get(&mut self, path: &str) -> Result<GooseResponse, TransactionError> {
pub async fn get(&mut self, path: &str) -> Result<GooseResponse, Box<TransactionError>> {
// GET path.
let goose_request = GooseRequest::builder()
.method(GooseMethod::Get)
Expand Down Expand Up @@ -1164,7 +1167,7 @@ impl GooseUser {
&mut self,
path: &str,
name: &str,
) -> Result<GooseResponse, TransactionError> {
) -> Result<GooseResponse, Box<TransactionError>> {
// GET path named.
let goose_request = GooseRequest::builder()
.method(GooseMethod::Get)
Expand Down Expand Up @@ -1205,7 +1208,7 @@ impl GooseUser {
&mut self,
path: &str,
body: T,
) -> Result<GooseResponse, TransactionError> {
) -> Result<GooseResponse, Box<TransactionError>> {
// Build a Reqwest RequestBuilder object.
let url = self.build_url(path)?;
let reqwest_request_builder = self.client.post(url);
Expand Down Expand Up @@ -1251,7 +1254,7 @@ impl GooseUser {
&mut self,
path: &str,
form: &T,
) -> Result<GooseResponse, TransactionError> {
) -> Result<GooseResponse, Box<TransactionError>> {
// Build a Reqwest RequestBuilder object.
let url = self.build_url(path)?;
let reqwest_request_builder = self.client.post(url);
Expand Down Expand Up @@ -1300,7 +1303,7 @@ impl GooseUser {
&mut self,
path: &str,
json: &T,
) -> Result<GooseResponse, TransactionError> {
) -> Result<GooseResponse, Box<TransactionError>> {
// Build a Reqwest RequestBuilder object.
let url = self.build_url(path)?;
let reqwest_request_builder = self.client.post(url);
Expand Down Expand Up @@ -1341,7 +1344,7 @@ impl GooseUser {
/// Ok(())
/// }
/// ```
pub async fn head(&mut self, path: &str) -> Result<GooseResponse, TransactionError> {
pub async fn head(&mut self, path: &str) -> Result<GooseResponse, Box<TransactionError>> {
// HEAD request.
let goose_request = GooseRequest::builder()
.method(GooseMethod::Head)
Expand Down Expand Up @@ -1377,7 +1380,7 @@ impl GooseUser {
/// Ok(())
/// }
/// ```
pub async fn delete(&mut self, path: &str) -> Result<GooseResponse, TransactionError> {
pub async fn delete(&mut self, path: &str) -> Result<GooseResponse, Box<TransactionError>> {
// DELETE request.
let goose_request = GooseRequest::builder()
.method(GooseMethod::Delete)
Expand Down Expand Up @@ -1433,7 +1436,7 @@ impl GooseUser {
&self,
method: &GooseMethod,
path: &str,
) -> Result<RequestBuilder, TransactionError> {
) -> Result<RequestBuilder, Box<TransactionError>> {
// Prepend the `base_url` to all relative paths.
let url = self.build_url(path)?;

Expand Down Expand Up @@ -1485,7 +1488,7 @@ impl GooseUser {
pub async fn request<'a>(
&mut self,
mut request: GooseRequest<'_>,
) -> Result<GooseResponse, TransactionError> {
) -> Result<GooseResponse, Box<TransactionError>> {
// If the RequestBuilder is already defined in the GooseRequest use it.
let request_builder = if request.request_builder.is_some() {
request.request_builder.take().unwrap()
Expand All @@ -1502,14 +1505,19 @@ impl GooseUser {
// ...wait until there's room to add a token to the throttle channel before proceeding.
debug!("GooseUser: waiting on throttle");
// Will result in TransactionError::RequestCanceled if this fails.
self.throttle.clone().unwrap().send_async(true).await?;
if let Err(e) = self.throttle.clone().unwrap().send_async(true).await {
return Err(Box::new(e.into()));
}
};

// Once past the throttle, the request is officially started.
let started = Instant::now();

// Create a Reqwest Request object from the RequestBuilder.
let built_request = request_builder.build()?;
let built_request = match request_builder.build() {
Ok(r) => r,
Err(e) => return Err(Box::new(e.into())),
};

// Get a string version of request path for logging.
let path = match Url::parse(built_request.url().as_ref()) {
Expand Down Expand Up @@ -1589,7 +1597,10 @@ impl GooseUser {
let base_url = self.base_url.to_string();
// Check if the URL redirected started with the load test base_url.
if !request_metric.final_url.starts_with(&base_url) {
let redirected_url = Url::parse(&request_metric.final_url)?;
let redirected_url = match Url::parse(&request_metric.final_url) {
Ok(u) => u,
Err(e) => return Err(Box::new(e.into())),
};
let redirected_base_url =
redirected_url[..url::Position::BeforePath].to_string();
info!(
Expand Down Expand Up @@ -1627,9 +1638,9 @@ impl GooseUser {

if request.error_on_fail && !request_metric.success {
error!("{:?} {}", &path, &request_metric.error);
return Err(TransactionError::RequestFailed {
return Err(Box::new(TransactionError::RequestFailed {
raw_request: request_metric,
});
}));
}

Ok(GooseResponse::new(request_metric, response))
Expand Down Expand Up @@ -1728,7 +1739,7 @@ impl GooseUser {
async fn coordinated_omission_mitigation(
&self,
request_metric: &GooseRequestMetric,
) -> Result<u64, TransactionError> {
) -> Result<u64, Box<TransactionError>> {
if let Some(co_mitigation) = self.config.co_mitigation.as_ref() {
// Return immediately if coordinated omission mitigation is disabled.
if co_mitigation == &GooseCoordinatedOmissionMitigation::Disabled {
Expand Down Expand Up @@ -1785,15 +1796,19 @@ impl GooseUser {
// If requests-file is enabled, send a copy of the raw request to the logger thread.
if !self.config.request_log.is_empty() {
if let Some(logger) = self.logger.as_ref() {
logger.send(Some(GooseLog::Request(request_metric.clone())))?;
if let Err(e) = logger.send(Some(GooseLog::Request(request_metric.clone()))) {
return Err(Box::new(e.into()));
}
}
}

// Parent is not defined when running
// [`test_start`](../struct.GooseAttack.html#method.test_start),
// [`test_stop`](../struct.GooseAttack.html#method.test_stop), and during testing.
if let Some(metrics_channel) = self.metrics_channel.clone() {
metrics_channel.send(GooseMetric::Request(request_metric))?;
if let Err(e) = metrics_channel.send(GooseMetric::Request(request_metric)) {
return Err(Box::new(e.into()));
}
}

Ok(())
Expand Down Expand Up @@ -1844,9 +1859,9 @@ impl GooseUser {
/// }
/// }
///
/// Err(TransactionError::RequestFailed {
/// Err(Box::new(TransactionError::RequestFailed {
/// raw_request: goose.request.clone(),
/// })
/// }))
/// }
/// ````
pub fn set_success(&self, request: &mut GooseRequestMetric) -> TransactionResult {
Expand Down Expand Up @@ -1934,9 +1949,9 @@ impl GooseUser {
// Print log to stdout.
info!("set_failure: {}", tag);

Err(TransactionError::RequestFailed {
Err(Box::new(TransactionError::RequestFailed {
raw_request: request.clone(),
})
}))
}

/// Write to [`debug_file`](../struct.GooseConfiguration.html#structfield.debug_file)
Expand Down Expand Up @@ -2025,13 +2040,15 @@ impl GooseUser {
// [`test_stop`](../struct.GooseAttack.html#method.test_stop), and during testing.
if let Some(logger) = self.logger.clone() {
if self.config.no_debug_body {
logger.send(Some(GooseLog::Debug(GooseDebug::new(
if let Err(e) = logger.send(Some(GooseLog::Debug(GooseDebug::new(
tag, request, headers, None,
))))?;
} else {
logger.send(Some(GooseLog::Debug(GooseDebug::new(
tag, request, headers, body,
))))?;
)))) {
return Err(Box::new(e.into()));
}
} else if let Err(e) = logger.send(Some(GooseLog::Debug(GooseDebug::new(
tag, request, headers, body,
)))) {
return Err(Box::new(e.into()));
}
}
}
Expand Down Expand Up @@ -2261,8 +2278,11 @@ impl GooseUser {
/// Ok(())
/// }
/// ```
pub fn set_base_url(&mut self, host: &str) -> Result<(), TransactionError> {
self.base_url = Url::parse(host)?;
pub fn set_base_url(&mut self, host: &str) -> Result<(), Box<TransactionError>> {
self.base_url = match Url::parse(host) {
Ok(u) => u,
Err(e) => return Err(Box::new(e.into())),
};
Ok(())
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl FromStr for GooseLogFormat {
fn from_str(s: &str) -> Result<Self, Self::Err> {
// Use a [`RegexSet`] to match string representations of `GooseCoordinatedOmissionMitigation`,
// returning the appropriate enum value. Also match a wide range of abbreviations and synonyms.
let log_format = RegexSet::new(&[
let log_format = RegexSet::new([
r"(?i)^csv$",
r"(?i)^(json|jsn)$",
r"(?i)^raw$",
Expand Down Expand Up @@ -483,7 +483,7 @@ impl GooseConfiguration {
message: "",
},
])
.unwrap_or_else(|| "".to_string());
.unwrap_or_default();

// Set `debug_format`.
self.debug_format = self.get_value(vec![
Expand Down Expand Up @@ -523,7 +523,7 @@ impl GooseConfiguration {
message: "",
},
])
.unwrap_or_else(|| "".to_string());
.unwrap_or_default();

// Set `error_format`.
self.error_format = self.get_value(vec![
Expand Down Expand Up @@ -563,7 +563,7 @@ impl GooseConfiguration {
message: "",
},
])
.unwrap_or_else(|| "".to_string());
.unwrap_or_default();

// Set `request_format`.
self.request_format = self.get_value(vec![
Expand Down Expand Up @@ -621,7 +621,7 @@ impl GooseConfiguration {
message: "",
},
])
.unwrap_or_else(|| "".to_string());
.unwrap_or_default();

// Set `transaction_format`.
self.transaction_format = self.get_value(vec![
Expand Down Expand Up @@ -661,7 +661,7 @@ impl GooseConfiguration {
message: "",
},
])
.unwrap_or_else(|| "".to_string());
.unwrap_or_default();

// Set `scenario_format`.
self.scenario_format = self.get_value(vec![
Expand Down
2 changes: 1 addition & 1 deletion src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl FromStr for GooseCoordinatedOmissionMitigation {
fn from_str(s: &str) -> Result<Self, Self::Err> {
// Use a [`RegexSet`] to match string representations of `GooseCoordinatedOmissionMitigation`,
// returning the appropriate enum value. Also match a wide range of abbreviations and synonyms.
let co_mitigation = RegexSet::new(&[
let co_mitigation = RegexSet::new([
r"(?i)^(average|ave|aver|avg|mean)$",
r"(?i)^(maximum|ma|max|maxi)$",
r"(?i)^(minimum|mi|min|mini)$",
Expand Down

0 comments on commit bfb3a60

Please sign in to comment.