Skip to main content

gsm_core/
context.rs

1use crate::prelude::*;
2use once_cell::sync::Lazy;
3use std::sync::RwLock;
4
5/// Returns the current environment identifier, defaulting to `dev`.
6pub fn current_env() -> EnvId {
7    CURRENT_ENV
8        .read()
9        .expect("current env lock poisoned")
10        .clone()
11}
12
13/// Updates the process-wide environment identifier used for tenant contexts.
14pub fn set_current_env(env: EnvId) {
15    *CURRENT_ENV.write().expect("current env lock poisoned") = env;
16}
17
18/// Constructs a tenant context from the provided identifiers.
19pub fn make_tenant_ctx(tenant: String, team: Option<String>, user: Option<String>) -> TenantCtx {
20    let env = current_env();
21    let tenant_id = TenantId(tenant);
22    let mut ctx = TenantCtx::new(env, tenant_id);
23    ctx = ctx.with_team(team.map(TeamId));
24    ctx = ctx.with_user(user.map(UserId));
25    ctx
26}
27
28static CURRENT_ENV: Lazy<RwLock<EnvId>> = Lazy::new(|| RwLock::new(EnvId("dev".to_string())));