Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiPolo committed May 24, 2020
1 parent b13cffc commit e78b163
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 37 deletions.
49 changes: 19 additions & 30 deletions src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ impl Mutation {
payload: MutationValue::Value(value),
}
}
pub fn new_param(
mutagen: instructions::MutagenInstruction,
value: RequestParam,
) -> Self {
pub fn new_param(mutagen: instructions::MutagenInstruction, value: RequestParam) -> Self {
Mutation {
mutagen,
payload: MutationValue::Param(value),
Expand Down Expand Up @@ -93,7 +90,7 @@ impl fmt::Display for Mutation {
write!(f, " \"{}\"", param.name)?;
}
// format!("{} {}", self.mutagen.request_part, param.name)
},
}
MutationValue::Value(value) => {
write!(f, " \"{}\"", value)?;
}
Expand All @@ -116,8 +113,7 @@ impl Mutator {
// TODO: If no mutation is found for one of the required elements, print it out
pub fn mutate<'a>(&self, endpoint: &'a Endpoint) -> Vec<Scenario<'a>> {
let mutations = self.mutations_from_mutagen(&endpoint, instructions::mutagens());
let query_mutations =
self.mutations_from_mutagen_query(&endpoint);
let query_mutations = self.mutations_from_mutagen_query(&endpoint);
// let body_mutations = self.mutations_from_mutagen_body(&endpoint, instructions::schema_mutagens());
self.scenarios_from_mutations(&endpoint, &mutations, &query_mutations)
}
Expand All @@ -136,12 +132,9 @@ impl Mutator {
// Each vector has the mutations for one parameter and we do not care about that order
// But we care that 200 is at the top so we order by expedted
for mutations_vec in query_mutations {
let sorted = mutations_vec.iter().sorted_by(|a, b| {
Ord::cmp(
&a.mutagen.expected,
&b.mutagen.expected,
)
});
let sorted = mutations_vec
.iter()
.sorted_by(|a, b| Ord::cmp(&a.mutagen.expected, &b.mutagen.expected));
query_params.push(sorted.collect());
}

Expand Down Expand Up @@ -286,12 +279,8 @@ impl Mutator {
let mut query_params = Vec::new();
for mutation in mutations {
match mutation.mutagen.request_part {
RequestPart::ContentType => {
request = request.content_type(mutation.value())
}
RequestPart::Method => {
request = request.set_method(mutation.value())
}
RequestPart::ContentType => request = request.content_type(mutation.value()),
RequestPart::Method => request = request.set_method(mutation.value()),
RequestPart::Path => request = request.path(mutation.value()),
RequestPart::AnyParam => query_params.push(mutation.param_value()),
_ => {} //unimplemented!("We do not know how to mutate this endpoint level item. {:?}", instruction.request_part),
Expand Down Expand Up @@ -323,21 +312,21 @@ impl Mutator {

// }

fn mutations_from_mutagen_query(
&self,
endpoint: &Endpoint
) -> Vec<Vec<Mutation>> {
fn mutations_from_mutagen_query(&self, endpoint: &Endpoint) -> Vec<Vec<Mutation>> {
let mut params = Vec::new();
params.extend(endpoint.method.optional_parameters());
params.extend(endpoint.method.required_parameters());

params.iter().filter_map(|param| {
if param.location_string() == "path" {
None
} else {
Some(params::mutate(&param, &self.known_params).variations)
}}
).collect()
params
.iter()
.filter_map(|param| {
if param.location_string() == "path" {
None
} else {
Some(params::mutate(&param, &self.known_params).variations)
}
})
.collect()
}

fn mutations_from_mutagen(
Expand Down
7 changes: 5 additions & 2 deletions src/mutation/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum Mutagen {
// Path mutagen
PathProper,
PathRandom, // No format, so should not be checked
// TODO: PathImproper, merge it with params
// TODO: PathImproper, merge it with params

// Query param mutagen
ParamProper,
Expand Down Expand Up @@ -118,7 +118,10 @@ impl fmt::Display for MutagenInstruction {
}

pub fn schema_mutagen(mutagen: &Mutagen) -> Vec<MutagenInstruction> {
schema_mutagens().into_iter().filter(|instruction| instruction.mutagen == *mutagen).collect()
schema_mutagens()
.into_iter()
.filter(|instruction| instruction.mutagen == *mutagen)
.collect()
}

// TODO: allow multiple possible returns types because different possible valid implementations
Expand Down
8 changes: 5 additions & 3 deletions src/mutation/param_mutation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::mutation::instructions::schema_mutagen;
use crate::mutation::{Mutagen, Mutation};
use crate::request_param::RequestParam;
use openapi_utils::ParameterExt;
use crate::mutation::instructions::schema_mutagen;
pub struct ParamMutation {
pub variations: Vec<Mutation>,
pub param: openapiv3::Parameter,
Expand All @@ -17,13 +17,15 @@ impl ParamMutation {
pub fn push(&mut self, value: &str, mutagen: Mutagen) {
let instruction = schema_mutagen(&mutagen)[0].clone();
let param = RequestParam::new(&self.param.parameter_data().name, value.clone());
self.variations.push(Mutation::new_param(instruction, param));
self.variations
.push(Mutation::new_param(instruction, param));
}
pub fn push_multiple(&mut self, value: Option<String>, mutagen: Mutagen) {
let instructions = schema_mutagen(&mutagen);
for instruction in instructions {
let param = RequestParam::new2(&self.param.parameter_data().name, value.clone());
self.variations.push(Mutation::new_param(instruction, param));
self.variations
.push(Mutation::new_param(instruction, param));
}
}
pub fn extend(&mut self, other: Self) {
Expand Down
1 change: 0 additions & 1 deletion src/mutation/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use openapi_utils::{ParameterDataExt, ParameterExt};
use openapiv3::Type;
//use crate::request_param::RequestParam;


/// TODO: How to make sure we generate for all the Mutagens?
pub fn mutate(param: &openapiv3::Parameter, known_params: &KnownParamCollection) -> ParamMutation {
let data = param.parameter_data();
Expand Down
2 changes: 1 addition & 1 deletion src/request_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl RequestParam {
pub fn new2(name: &str, value: Option<String>) -> Self {
RequestParam {
name: name.to_string(),
value
value,
}
}
}
Expand Down

0 comments on commit e78b163

Please sign in to comment.