use axum::extract::FromRequestParts;
use axum::http::request::Parts;
use axum::http::StatusCode;
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TenantId(pub Uuid);
impl TenantId {
pub fn new(id: Uuid) -> Self {
Self(id)
}
pub fn into_uuid(self) -> Uuid {
self.0
}
}
impl From<Uuid> for TenantId {
fn from(id: Uuid) -> Self {
Self(id)
}
}
impl From<TenantId> for Uuid {
fn from(t: TenantId) -> Uuid {
t.0
}
}
impl std::fmt::Display for TenantId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<S> FromRequestParts<S> for TenantId
where
S: Send + Sync,
{
type Rejection = (StatusCode, &'static str);
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
parts
.extensions
.get::<TenantId>()
.copied()
.ok_or((
StatusCode::INTERNAL_SERVER_ERROR,
"tenaxum: TenantId extension missing — your auth layer must insert it",
))
}
}