Skip to content

Commit

Permalink
chore: fix clippy and rustfmt lints
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Oct 29, 2023
1 parent 09fb7b8 commit 3423405
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 68 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"petstore",
"prettytable",
"println",
"refpath",
"reqwest",
"rustfmt",
"semver",
Expand Down
4 changes: 2 additions & 2 deletions examples/complex-validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ fn main() {

let valid = json!({
"thing": "car",
"size": 3.14,
"size": 3.1,
"page": 1
});

let invalid1 = json!({
"thing": "car",
"size": 3.14,
"size": 3.1,
});

let invalid2 = json!({
Expand Down
6 changes: 3 additions & 3 deletions examples/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oas3::conformance::{
async fn main() -> eyre::Result<()> {
color_eyre::install()?;

dotenv::dotenv().ok();
dotenvy::dotenv().ok();
pretty_env_logger::init();

let spec = oas3::from_path("./data/oas-samples/pet-store.yml").expect("api spec parse error");
Expand Down Expand Up @@ -36,10 +36,10 @@ async fn main() -> eyre::Result<()> {
),
]);

println!("");
println!();
runner.run_queued_tests().await;
runner.print_results();
println!("");
println!();

Ok(())
}
5 changes: 1 addition & 4 deletions src/conformance/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ impl TestResponseSpec {
if &self.status == val {
Ok(())
} else {
Err(ValidationError::StatusMismatch(
self.status.clone(),
val.clone(),
))
Err(ValidationError::StatusMismatch(self.status, *val))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/conformance/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use colored::{ColoredString, Colorize};
use futures_util::{stream, FutureExt as _, StreamExt as _};
use log::{debug, trace};
use prettytable::{cell, row, Table};
use prettytable::{row, Table};
use serde_json::Value as JsonValue;
use url::Url;

Expand Down
50 changes: 25 additions & 25 deletions src/conformance/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl ConformanceTestSpec {
pub fn resolve(&self, spec: &Spec) -> Result<ResolvedConformanceTestSpec, Error> {
trace!("resolving: {:?}", &self.operation);

let mut req = self.resolve_request(&spec)?;
let mut req = self.resolve_request(spec)?;

if let Some(TestAuthentication::Custom(transformer)) = self.request.auth {
req = transformer(req);
Expand All @@ -69,11 +69,11 @@ impl ConformanceTestSpec {
Ok(ResolvedConformanceTestSpec {
unresolved: self.clone(),
request: req,
response: self.resolve_response_spec(&spec)?,
response: self.resolve_response_spec(spec)?,
})
}

pub fn resolve_test_operation<'a>(&self, spec: &'a Spec) -> Result<TestOperation, Error> {
pub fn resolve_test_operation(&self, spec: &Spec) -> Result<TestOperation, Error> {
trace!("resolve_test_operation: {:?}", &self.operation);

let test_op = match &self.operation {
Expand All @@ -99,16 +99,16 @@ impl ConformanceTestSpec {
}

pub fn resolve_params(&self, spec: &Spec) -> Result<Vec<TestParam>, Error> {
let test_op = self.resolve_test_operation(&spec)?;
let op = test_op.resolve_operation(&spec)?;
let test_op = self.resolve_test_operation(spec)?;
let op = test_op.resolve_operation(spec)?;

let mut test_params = vec![];

// iterate params
for param in &self.request.params {
// resolve in spec
let parameter = op
.parameter(&param.name, &spec)?
.parameter(&param.name, spec)?
.ok_or(ValidationError::ParameterNotFound(param.name.clone()))?;

// validate position
Expand Down Expand Up @@ -142,14 +142,14 @@ impl ConformanceTestSpec {
pub fn resolve_request(&self, spec: &Spec) -> Result<TestRequest, Error> {
trace!("resolving request: {:?}", &self.operation);

let test_op = self.resolve_test_operation(&spec)?;
let op = test_op.resolve_operation(&spec)?;
let test_op = self.resolve_test_operation(spec)?;
let op = test_op.resolve_operation(spec)?;

let mut req = match self.request.source {
RequestSource::Empty => TestRequest {
operation: test_op.clone(),
headers: HeaderMap::new(),
params: self.resolve_params(&spec)?,
params: self.resolve_params(spec)?,
body: Bytes::new(),
},

Expand All @@ -161,7 +161,7 @@ impl ConformanceTestSpec {
TestRequest {
operation: test_op.clone(),
headers: HeaderMap::new(),
params: self.resolve_params(&spec)?,
params: self.resolve_params(spec)?,
body: data.clone(),
}
}
Expand All @@ -170,12 +170,12 @@ impl ConformanceTestSpec {
ref media_type,
ref name,
} => {
let req_body = op.request_body(&spec)?;
let req_body = op.request_body(spec)?;
let media_spec = req_body.content.get(media_type).ok_or(SpecError::Ref(
RefError::Unresolvable(format!("mediaType/{}", &name)),
))?;
let schema = media_spec.schema(&spec)?;
let examples = media_spec.examples(&spec);
let schema = media_spec.schema(spec)?;
let examples = media_spec.examples(spec);
let example = examples
.get(name)
.ok_or(SpecError::Ref(RefError::Unresolvable(format!(
Expand All @@ -201,7 +201,7 @@ impl ConformanceTestSpec {
TestRequest {
operation: test_op.clone(),
headers: hdrs,
params: self.resolve_params(&spec)?,
params: self.resolve_params(spec)?,
body: example.as_bytes().into(),
}
}
Expand All @@ -222,34 +222,34 @@ impl ConformanceTestSpec {
}

pub fn resolve_response_spec(&self, spec: &Spec) -> Result<TestResponseSpec, Error> {
let test_op = self.resolve_test_operation(&spec)?;
let op = test_op.resolve_operation(&spec)?;
let test_op = self.resolve_test_operation(spec)?;
let op = test_op.resolve_operation(spec)?;

let res_spec =
match &self.response_spec.source {
ResponseSpecSource::Status(status) => TestResponseSpec {
operation: test_op.clone(),
status: status.clone(),
status: *status,
body_validator: None,
},

ResponseSpecSource::Schema { status, media_type } => {
// traverse spec
let responses = op.responses(&spec);
let responses = op.responses(spec);
let status_spec = responses.get(status.as_str()).ok_or(SpecError::Ref(
RefError::Unresolvable(format!("status/{}", &status.as_u16())),
))?;
let media_spec = status_spec.content.get(media_type).ok_or(SpecError::Ref(
RefError::Unresolvable(format!("mediaType/{}", &media_type)),
))?;
let schema = media_spec.schema(&spec)?;
let schema = media_spec.schema(spec)?;

// create validator
let validator = ValidationTree::from_schema(&schema, spec)?;

TestResponseSpec {
operation: test_op.clone(),
status: status.clone(),
status: *status,
body_validator: Some(validator),
}
}
Expand All @@ -260,15 +260,15 @@ impl ConformanceTestSpec {
name,
} => {
// traverse spec
let reses = op.responses(&spec);
let reses = op.responses(spec);
let status_spec = reses.get(status.as_str()).ok_or(SpecError::Ref(
RefError::Unresolvable(format!("status/{}", &status.as_u16())),
))?;
let media_spec = status_spec.content.get(media_type).ok_or(SpecError::Ref(
RefError::Unresolvable(format!("mediaType/{}", &media_type)),
))?;
let schema = media_spec.schema(&spec)?;
let examples = media_spec.examples(&spec);
let schema = media_spec.schema(spec)?;
let examples = media_spec.examples(spec);
let example =
examples
.get(name)
Expand All @@ -287,15 +287,15 @@ impl ConformanceTestSpec {
debug!("against schema: {:?}", &schema);
debug!("with validator: {:?}", &validator);

validator.validate(&ex).map_err(Error::Validation)?;
validator.validate(ex).map_err(Error::Validation)?;
}

let mut hdrs = HeaderMap::new();
hdrs.insert("Content-Type", media_type.clone().parse().unwrap());

TestResponseSpec {
operation: test_op.clone(),
status: status.clone(),
status: *status,
body_validator: Some(validator),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod tests {
println!(" Saving string to {:?}...", path);
std::fs::create_dir_all(&path).unwrap();
let full_filename = path.as_ref().to_path_buf().join(filename);
let mut f = File::create(&full_filename).unwrap();
let mut f = File::create(full_filename).unwrap();
f.write_all(data.as_bytes()).unwrap();
}

Expand Down Expand Up @@ -112,7 +112,7 @@ mod tests {
// File -> `String` -> `serde_yaml::Value` -> `serde_json::Value` -> `String`

// Read the original file to string
let spec_yaml_str = read_to_string(&input_file)
let spec_yaml_str = read_to_string(input_file)
.unwrap_or_else(|e| panic!("failed to read contents of {:?}: {}", input_file, e));
// Convert YAML string to JSON string
let spec_json_str = convert_yaml_str_to_json(&spec_yaml_str);
Expand All @@ -121,7 +121,7 @@ mod tests {
// File -> `Spec` -> `serde_json::Value` -> `String`

// Parse the input file
let parsed_spec = from_path(&input_file).unwrap();
let parsed_spec = from_path(input_file).unwrap();
// Convert to serde_json::Value
let parsed_spec_json = serde_json::to_value(parsed_spec).unwrap();
// Convert to a JSON string
Expand Down
2 changes: 1 addition & 1 deletion src/spec/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl FromRef for Example {
.as_ref()
.and_then(|cs| cs.examples.get(&refpath.name))
.ok_or_else(|| RefError::Unresolvable(path.to_owned()))
.and_then(|oor| oor.resolve(&spec)),
.and_then(|oor| oor.resolve(spec)),

typ => Err(RefError::MismatchedType(typ, RefType::Example)),
}
Expand Down
6 changes: 3 additions & 3 deletions src/spec/media_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ impl MediaType {
self.schema
.as_ref()
.unwrap()
.resolve(&spec)
.resolve(spec)
.map_err(Error::Ref)
}

pub fn examples(&self, spec: &Spec) -> BTreeMap<String, Example> {
self.examples
.as_ref()
.map(|examples| examples.resolve_all(&spec))
.unwrap_or_else(BTreeMap::new)
.map(|examples| examples.resolve_all(spec))
.unwrap_or_default()
}
}
2 changes: 1 addition & 1 deletion src/spec/media_type_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl MediaTypeExamples {
Self::Examples { examples } => examples
.iter()
.filter_map(|(name, oor)| {
oor.resolve(&spec)
oor.resolve(spec)
.map(|obj| (name.clone(), obj))
.map_err(|err| error!("{}", err))
.ok()
Expand Down
8 changes: 4 additions & 4 deletions src/spec/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ impl Operation {
self.request_body
.as_ref()
.unwrap()
.resolve(&spec)
.resolve(spec)
.map_err(Error::Ref)
}

pub fn responses(&self, spec: &Spec) -> BTreeMap<String, Response> {
self.responses
.iter()
.filter_map(|(name, oor)| {
oor.resolve(&spec)
oor.resolve(spec)
.map(|obj| (name.clone(), obj))
// TODO: find better error solution
.map_err(|err| error!("{}", err))
Expand All @@ -135,15 +135,15 @@ impl Operation {
.parameters
.iter()
// TODO: find better error solution, maybe vec<result<_>>
.filter_map(|oor| oor.resolve(&spec).map_err(|err| error!("{}", err)).ok())
.filter_map(|oor| oor.resolve(spec).map_err(|err| error!("{}", err)).ok())
.collect();

Ok(params)
}

pub fn parameter(&self, search: &str, spec: &Spec) -> Result<Option<Parameter>, Error> {
let param = self
.parameters(&spec)?
.parameters(spec)?
.iter()
.find(|param| param.name == search)
.cloned();
Expand Down
2 changes: 1 addition & 1 deletion src/spec/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl FromRef for Parameter {
.as_ref()
.and_then(|cs| cs.parameters.get(&refpath.name))
.ok_or_else(|| RefError::Unresolvable(path.to_owned()))
.and_then(|oor| oor.resolve(&spec)),
.and_then(|oor| oor.resolve(spec)),

typ => Err(RefError::MismatchedType(typ, RefType::Parameter)),
}
Expand Down
2 changes: 1 addition & 1 deletion src/spec/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
pub fn resolve(&self, spec: &Spec) -> Result<T, RefError> {
match self {
Self::Object(component) => Ok(component.clone()),
Self::Ref { ref_path } => T::from_ref(&spec, &ref_path),
Self::Ref { ref_path } => T::from_ref(spec, ref_path),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/spec/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl FromRef for RequestBody {
.as_ref()
.and_then(|cs| cs.request_bodies.get(&refpath.name))
.ok_or_else(|| RefError::Unresolvable(path.to_owned()))
.and_then(|oor| oor.resolve(&spec)),
.and_then(|oor| oor.resolve(spec)),

typ => Err(RefError::MismatchedType(typ, RefType::RequestBody)),
}
Expand Down
2 changes: 1 addition & 1 deletion src/spec/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl FromRef for Response {
.as_ref()
.and_then(|cs| cs.responses.get(&refpath.name))
.ok_or_else(|| RefError::Unresolvable(path.to_owned()))
.and_then(|oor| oor.resolve(&spec)),
.and_then(|oor| oor.resolve(spec)),

typ => Err(RefError::MismatchedType(typ, RefType::Response)),
}
Expand Down
2 changes: 1 addition & 1 deletion src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl FromRef for Schema {
.as_ref()
.and_then(|cs| cs.schemas.get(&refpath.name))
.ok_or_else(|| RefError::Unresolvable(path.to_owned()))
.and_then(|oor| oor.resolve(&spec)),
.and_then(|oor| oor.resolve(spec)),

typ => Err(RefError::MismatchedType(typ, RefType::Schema)),
}
Expand Down
Loading

0 comments on commit 3423405

Please sign in to comment.