use uuid::Uuid;
use crate::AxiamError;
use crate::client::{AxiamClient, OrgIdentifier, TenantIdentifier};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct Scope {
pub org_id: Option<Uuid>,
pub tenant_id: Option<Uuid>,
}
impl Scope {
pub(crate) fn org(&self, client: &AxiamClient, operation: &str) -> Result<Uuid, AxiamError> {
if let Some(id) = self.org_id {
return Ok(id);
}
match &client.inner.org {
Some(OrgIdentifier::Id(id)) => Ok(*id),
Some(OrgIdentifier::Slug(slug)) => Err(AxiamError::network(format!(
"{operation}: this route needs an organization UUID, but the client was built \
with org_slug {slug:?}. Rebuild it with .org_id(..), or name one on the handle \
with .in_org(..)."
))),
None => Err(AxiamError::network(format!(
"{operation}: this route needs an organization UUID and the client has none. \
Build the client with .org_id(..), or name one on the handle with .in_org(..)."
))),
}
}
pub(crate) fn tenant(&self, client: &AxiamClient, operation: &str) -> Result<Uuid, AxiamError> {
if let Some(id) = self.tenant_id {
return Ok(id);
}
match &client.inner.tenant {
TenantIdentifier::Id(id) => Ok(*id),
TenantIdentifier::Slug(slug) => Err(AxiamError::network(format!(
"{operation}: this route needs a tenant UUID, but the client was built with \
tenant_slug {slug:?}. Rebuild it with .tenant_id(..), or name one on the \
handle with .for_tenant(..)."
))),
}
}
}