use crate::errors::AppError;
use axum::{
extract::{FromRequestParts, Path},
http::request::Parts,
response::{IntoResponse, Response},
};
use uuid::Uuid;
pub struct UuidPath(pub Uuid);
impl<S> FromRequestParts<S> for UuidPath
where
S: Send + Sync,
{
type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let Path(id) = Path::<String>::from_request_parts(parts, state)
.await
.map_err(|e| e.into_response())?;
match Uuid::parse_str(&id) {
Ok(uuid) => Ok(UuidPath(uuid)),
Err(_) => Err(AppError::BadRequest(format!("Invalid UUID: {id}")).into_response()),
}
}
}