mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-15 17:32:56 +00:00
Updates `compute_tools` and `compute_api` crates to edition 2024. We like to stay on the latest edition if possible. There is no functional changes, however some code changes had to be done to accommodate the edition's breaking changes. The PR has three commits: * the first commit updates the named crates to edition 2024 and appeases `cargo clippy` by changing code. * the second commit performs a `cargo fmt` that does some minor changes (not many) * the third commit performs a cargo fmt with nightly options to reorder imports as a one-time thing. it's completely optional, but I offer it here for the compute team to review it. I'd like to hear opinions about the third commit, if it's wanted and felt worth the diff or not. I think most attention should be put onto the first commit. Part of #10918
46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
use std::ops::{Deref, DerefMut};
|
|
|
|
use axum::extract::rejection::JsonRejection;
|
|
use axum::extract::{FromRequest, Request};
|
|
use compute_api::responses::GenericAPIError;
|
|
use http::StatusCode;
|
|
|
|
/// Custom `Json` extractor, so that we can format errors into
|
|
/// `JsonResponse<GenericAPIError>`.
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
pub(crate) struct Json<T>(pub T);
|
|
|
|
impl<S, T> FromRequest<S> for Json<T>
|
|
where
|
|
axum::Json<T>: FromRequest<S, Rejection = JsonRejection>,
|
|
S: Send + Sync,
|
|
{
|
|
type Rejection = (StatusCode, axum::Json<GenericAPIError>);
|
|
|
|
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
|
match axum::Json::<T>::from_request(req, state).await {
|
|
Ok(value) => Ok(Self(value.0)),
|
|
Err(rejection) => Err((
|
|
rejection.status(),
|
|
axum::Json(GenericAPIError {
|
|
error: rejection.body_text().to_lowercase(),
|
|
}),
|
|
)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> Deref for Json<T> {
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl<T> DerefMut for Json<T> {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.0
|
|
}
|
|
}
|