use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Bindings {
#[serde(default)]
tenants: HashMap<String, TenantBinding>,
#[serde(default)]
global: TenantBinding,
}
impl Bindings {
pub fn from_json<T: AsRef<[u8]>>(data: T) -> serde_json::Result<Self> {
serde_json::from_slice(data.as_ref())
}
pub fn tenant_binding(&self, tenant: &str) -> Option<&TenantBinding> {
self.tenants.get(tenant)
}
pub fn global_binding(&self) -> &TenantBinding {
&self.global
}
pub(crate) fn tenants_iter(&self) -> impl Iterator<Item = (&String, &TenantBinding)> {
self.tenants.iter()
}
pub fn with_tenant(mut self, tenant: impl Into<String>, binding: TenantBinding) -> Self {
self.tenants.insert(tenant.into(), binding);
self
}
pub fn with_global(mut self, binding: TenantBinding) -> Self {
self.global = binding;
self
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct TenantBinding {
#[serde(default)]
allow_env: Vec<String>,
}
impl TenantBinding {
pub fn new<I, S>(allow: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
let allow_env = allow.into_iter().map(Into::into).collect();
Self { allow_env }
}
pub fn allow_env(&self) -> &[String] {
&self.allow_env
}
}