Skip to content

Commit 0727ea3

Browse files
Fix clippy large error result
1 parent 8c6f30c commit 0727ea3

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

rust/iscy-backend/src/lib.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -952,24 +952,36 @@ fn api_error_response(
952952
.into_response()
953953
}
954954

955-
fn validated_cve_id(raw_cve_id: &str) -> Result<String, Response> {
955+
struct ApiError {
956+
status: StatusCode,
957+
error_code: &'static str,
958+
message: String,
959+
}
960+
961+
impl ApiError {
962+
fn into_response(self) -> Response {
963+
api_error_response(self.status, self.error_code, self.message)
964+
}
965+
}
966+
967+
fn validated_cve_id(raw_cve_id: &str) -> Result<String, ApiError> {
956968
let normalized = normalize_cve_id(raw_cve_id);
957969
if normalized.is_empty() {
958-
return Err(api_error_response(
959-
StatusCode::BAD_REQUEST,
960-
"empty_cve_id",
961-
"CVE-ID darf nicht leer sein.",
962-
));
970+
return Err(ApiError {
971+
status: StatusCode::BAD_REQUEST,
972+
error_code: "empty_cve_id",
973+
message: "CVE-ID darf nicht leer sein.".to_string(),
974+
});
963975
}
964976
if !is_valid_cve_id(&normalized) {
965-
return Err(api_error_response(
966-
StatusCode::UNPROCESSABLE_ENTITY,
967-
"invalid_cve_id",
968-
format!(
977+
return Err(ApiError {
978+
status: StatusCode::UNPROCESSABLE_ENTITY,
979+
error_code: "invalid_cve_id",
980+
message: format!(
969981
"CVE-ID '{}' entspricht nicht dem erwarteten Format CVE-YYYY-NNNN.",
970982
normalized
971983
),
972-
));
984+
});
973985
}
974986
Ok(normalized)
975987
}
@@ -1107,7 +1119,7 @@ fn first_nvd_cve(payload: &Value) -> Option<Value> {
11071119
fn nvd_normalize_response(payload: NvdImportRequest) -> Response {
11081120
let normalized = match validated_cve_id(&payload.cve_id) {
11091121
Ok(normalized) => normalized,
1110-
Err(response) => return response,
1122+
Err(err) => return err.into_response(),
11111123
};
11121124
(
11131125
StatusCode::OK,
@@ -8306,7 +8318,7 @@ async fn nvd_import(
83068318
) -> Response {
83078319
let normalized = match validated_cve_id(&payload.cve_id) {
83088320
Ok(normalized) => normalized,
8309-
Err(response) => return response,
8321+
Err(err) => return err.into_response(),
83108322
};
83118323
let Some(store) = state.cve_store.as_ref() else {
83128324
return api_error_response(
@@ -8357,7 +8369,7 @@ async fn nvd_upsert(
83578369
let record = NvdCveRecord::from_nvd_value(&payload.cve, &raw_payload, fallback_cve_id);
83588370
let normalized = match validated_cve_id(&record.cve_id) {
83598371
Ok(normalized) => normalized,
8360-
Err(response) => return response,
8372+
Err(err) => return err.into_response(),
83618373
};
83628374

83638375
let Some(store) = state.cve_store.as_ref() else {

0 commit comments

Comments
 (0)