Skip to content
This repository was archived by the owner on Feb 20, 2024. It is now read-only.

Commit 925ee7b

Browse files
authored
Merge pull request #23 from Virtual-Finland-Development/refactor/fix-clippy-warnings
refactor: cleanup the api_app lib from clippy warnings
2 parents ec8dfd0 + 2b1938c commit 925ee7b

11 files changed

Lines changed: 44 additions & 46 deletions

File tree

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by Cargo
22
# will have compiled files and executables
3-
/target/
3+
target
44

55
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
66
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
@@ -15,4 +15,4 @@ infra/build/*
1515
.cargo_home
1616

1717
# idea
18-
.idea
18+
.idea

src/lib/api_app/src/api/requests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ async fn engage_request<I: Debug + Serialize>(
200200

201201
let response_status = response.status();
202202
let response_headers = response.headers().clone();
203-
let response_body = response.text().await.unwrap_or("No response body received".to_string());
203+
let response_body = response.text().await.unwrap_or_else(|_| "No response body received".to_string());
204204

205205
log::debug!(
206206
"Request: {}, status code: {:#?}, elapsed time: {}ms",

src/lib/api_app/src/api/responses.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub fn resolve_external_service_bad_response(
195195
// Ensure JSON response
196196
let response_json = serde_json
197197
::from_str::<serde_json::Value>(&response_body)
198-
.unwrap_or(json!({"content": response_body}));
198+
.unwrap_or_else(|_| json!({"content": response_body}));
199199

200200
// Use the status code from the response if it's a valid HTTP status code
201201
if response_json.is_object() {
@@ -209,9 +209,9 @@ pub fn resolve_external_service_bad_response(
209209
}
210210

211211
Ok(APIRoutingResponse {
212-
status_code: status_code,
212+
status_code,
213213
body: json!({
214-
"message": format!("External service responded with a status: {}", status_code).to_string(),
214+
"message": format!("External service responded with a status: {}", status_code),
215215
"data": response_json,
216216
}).to_string(),
217217
headers: get_default_headers(),

src/lib/api_app/src/api/routes/application.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub async fn get_external_service_bad_response(
6868
) -> Result<APIRoutingResponse, APIRoutingError> {
6969
let status_code = response.status();
7070
let response_body = response.text().await?;
71-
return resolve_external_service_bad_response(status_code, response_body);
71+
resolve_external_service_bad_response(status_code, response_body)
7272
}
7373

7474
pub async fn wake_up_external_services(

src/lib/api_app/src/api/routes/testbed/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,5 @@ fn access_control_check(proxy_destination_url: &str) -> bool {
8282
}
8383
}
8484

85-
return !acl_is_satisfied;
85+
!acl_is_satisfied
8686
}

src/lib/api_app/src/api/routes/testbed/productizers/job/job_input_extenders.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn extend_job_occupation_uris(occupation_uri: String, codesets: &Vec<Occupation>
3333
let mut extended_occupation_uris: Vec<String> = Vec::new();
3434

3535
for codeset in codesets {
36-
if !codeset.uri.is_some() || !codeset.broader.is_some() {
36+
if codeset.uri.is_none() || codeset.broader.is_none() {
3737
continue;
3838
}
3939

@@ -55,7 +55,7 @@ fn extend_job_occupation_uris(occupation_uri: String, codesets: &Vec<Occupation>
5555

5656
fn get_esco_occupations_codeset() -> Vec<Occupation> {
5757
let contents = include_str!("../../../../resources/testbed/jobs/esco-1.1.0-occupations.json");
58-
return deserialize_json_from_string::<Vec<Occupation>>(contents.as_ref()).expect(
58+
deserialize_json_from_string::<Vec<Occupation>>(contents).expect(
5959
"Failed to parse the ESCO occupations codeset"
60-
);
60+
)
6161
}

src/lib/api_app/src/api/routes/testbed/productizers/job/job_models.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct JobPostingResponse<T> {
6464
#[serde(rename = "totalCount")]
6565
pub total_count: i32,
6666
}
67-
#[derive(Deserialize, Serialize, Debug, PartialEq)]
67+
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
6868
pub struct JobPosting {
6969
pub employer: String,
7070
pub location: Location,
@@ -78,13 +78,13 @@ pub struct JobPosting {
7878
pub application_url: Option<String>,
7979
}
8080

81-
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
81+
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
8282
pub struct Location {
8383
pub municipality: String,
8484
pub postcode: String,
8585
}
8686

87-
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
87+
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
8888
pub struct BasicInfo {
8989
pub title: String,
9090
pub description: String,
@@ -96,7 +96,7 @@ pub struct BasicInfo {
9696
// Transformed outputs for the frontend app
9797
// @TODO: there must be a better way to do this in rust
9898
//
99-
#[derive(Deserialize, Serialize, Debug, PartialEq)]
99+
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
100100
pub struct JobPostingForFrontend {
101101
pub id: String,
102102
#[serde(rename = "jobsSource")]

src/lib/api_app/src/api/routes/testbed/productizers/job/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ pub fn construct_productizer_requests(
124124
requirements: request_input.requirements,
125125
paging: RequestPaging {
126126
limit: request_limit,
127-
offset: offset,
127+
offset,
128128
},
129129
};
130130

131-
return Ok(ProductizerRequest {
132-
endpoint_urls: endpoint_urls,
131+
Ok(ProductizerRequest {
132+
endpoint_urls,
133133
request_input: jobs_request,
134134
headers: request_headers,
135-
original_input: original_input,
136-
});
135+
original_input,
136+
})
137137
}
138138

139139
pub fn parse_job_request_input(request_input: &JobsRequestFromFrontend) -> JobsRequestFromFrontend {
@@ -153,7 +153,7 @@ pub fn parse_job_request_input(request_input: &JobsRequestFromFrontend) -> JobsR
153153
* Merge the job posting results, by mutation
154154
*/
155155
pub fn merge_job_posting_results(results: &mut Vec<JobPostingForFrontend>) {
156-
results.sort_by(|a, b| job_postings_sort_comparator(a, b));
156+
results.sort_by(job_postings_sort_comparator);
157157
results.dedup_by(|a, b| is_job_postings_the_same(a, b));
158158
}
159159

@@ -174,10 +174,10 @@ fn is_job_postings_the_same(a: &JobPostingForFrontend, b: &JobPostingForFrontend
174174
*/
175175
pub fn transform_job_posting_results(
176176
jobs_source: String,
177-
results: &mut Vec<JobPosting>
177+
results: &mut [JobPosting]
178178
) -> Vec<JobPostingForFrontend> {
179179
results
180-
.into_iter()
180+
.iter_mut()
181181
.map(|job_posting| JobPostingForFrontend {
182182
id: generate_job_posting_id(job_posting),
183183
jobs_source: jobs_source.to_string(),
@@ -198,9 +198,9 @@ pub fn transform_job_posting_results(
198198
// Generate ID for the job posting
199199

200200
fn generate_job_posting_id(job_posting: &JobPosting) -> String {
201-
let job_now = job_posting.clone();
201+
let job_now = job_posting;
202202
let app_url = job_now.application_url.clone();
203-
let url_part = app_url.unwrap_or("".to_string());
203+
let url_part = app_url.unwrap_or_default();
204204

205205
let mut hasher = DefaultHasher::new();
206206
let mut id_parts = String::new();

src/lib/api_app/src/api/routes/testbed/productizers/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn parse_testbed_request_headers(request: ParsedRequest) -> Result<HeaderMap, AP
1818
"authorization",
1919
request.headers
2020
.get("authorization")
21-
.ok_or(APIRoutingError::UnprocessableEntity("No authorization header".to_string()))?
21+
.ok_or_else(|| APIRoutingError::UnprocessableEntity("No authorization header".to_string()))?
2222
.clone()
2323
);
2424

@@ -27,7 +27,7 @@ fn parse_testbed_request_headers(request: ParsedRequest) -> Result<HeaderMap, AP
2727
"x-consent-token",
2828
request.headers
2929
.get("x-consent-token")
30-
.ok_or(
30+
.ok_or_else(||
3131
APIRoutingError::UnprocessableEntity("No x-consent-token header".to_string())
3232
)?
3333
.clone()

src/lib/api_app/src/api/routes/testbed/productizers/user/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub async fn fetch_user_status_info(
3131
let endpoint_url = env
3232
::var("USER_STATUS_INFO_PRODUCTIZER_ENDPOINT")
3333
.expect("USER_STATUS_INFO_PRODUCTIZER_ENDPOINT must be set");
34-
let request_input: JSONValue = serde_json::from_str(request.body.as_str()).unwrap_or(json!({})); // Pass through body
34+
let request_input: JSONValue = serde_json::from_str(request.body.as_str()).unwrap_or_else(|_| json!({})); // Pass through body
3535
let request_headers = parse_testbed_request_headers(request)?;
3636
let response = post_json_request::<JSONValue, JSONValue>(
3737
endpoint_url.to_string(),
@@ -47,7 +47,7 @@ pub async fn update_user_status_info(
4747
let endpoint_url = env
4848
::var("USER_STATUS_INFO_WRITE_PRODUCTIZER_ENDPOINT")
4949
.expect("USER_STATUS_INFO_WRITE_PRODUCTIZER_ENDPOINT must be set");
50-
let request_input: JSONValue = serde_json::from_str(request.body.as_str()).unwrap_or(json!({})); // Pass through body
50+
let request_input: JSONValue = serde_json::from_str(request.body.as_str()).unwrap_or_else(|_| json!({})); // Pass through body
5151
let request_headers = parse_testbed_request_headers(request)?;
5252
let response = post_json_request::<JSONValue, JSONValue>(
5353
endpoint_url.to_string(),

0 commit comments

Comments
 (0)