use std::collections::HashMap;
use async_trait::async_trait;
use crate::error::TenancyError;
use crate::tenant::{normalize_host, TenantId};
pub mod chain;
pub mod domain;
pub mod path;
pub mod request_data;
pub mod subdomain;
pub use chain::ResolverChain;
pub use domain::DomainResolver;
pub use path::PathResolver;
pub use request_data::{ClosureResolver, CookieResolver, HeaderResolver};
pub use subdomain::SubdomainResolver;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TenantKey {
Id(TenantId),
Slug(String),
Domain(String),
}
#[derive(Debug, Clone, Default)]
pub struct RequestInfo {
pub method: String,
pub path: String,
pub host: String,
pub headers: HashMap<String, String>,
pub cookies: HashMap<String, String>,
pub query: HashMap<String, String>,
}
impl RequestInfo {
pub fn new(method: impl Into<String>, path: impl Into<String>, host: &str) -> Self {
Self {
method: method.into(),
path: path.into(),
host: normalize_host(host),
headers: HashMap::new(),
cookies: HashMap::new(),
query: HashMap::new(),
}
}
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.get(&name.to_ascii_lowercase())
.map(String::as_str)
}
pub fn cookie(&self, name: &str) -> Option<&str> {
self.cookies.get(name).map(String::as_str)
}
pub fn query(&self, name: &str) -> Option<&str> {
self.query.get(name).map(String::as_str)
}
pub fn with_header(mut self, name: impl AsRef<str>, value: impl Into<String>) -> Self {
self.headers
.insert(name.as_ref().to_ascii_lowercase(), value.into());
self
}
}
#[async_trait]
pub trait TenantResolver: Send + Sync {
async fn resolve(&self, request: &RequestInfo) -> Result<Option<TenantKey>, TenancyError>;
}