use serde::{Deserialize, Serialize};
use crate::context::ExecutionContext;
use crate::error::{Error, Result};
use crate::tenant_id::TenantId;
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct ThreadKey {
tenant_id: TenantId,
thread_id: String,
}
impl ThreadKey {
#[must_use]
pub fn new(tenant_id: TenantId, thread_id: impl Into<String>) -> Self {
let thread_id = thread_id.into();
assert!(
!thread_id.is_empty(),
"ThreadKey::new: thread_id must be non-empty"
);
Self {
tenant_id,
thread_id,
}
}
pub fn from_ctx(ctx: &ExecutionContext) -> Result<Self> {
let thread_id = ctx.thread_id().ok_or_else(|| {
Error::config(
"ThreadKey::from_ctx requires ExecutionContext::thread_id; \
set it via ExecutionContext::with_thread_id(...)",
)
})?;
if thread_id.is_empty() {
return Err(Error::config(
"ThreadKey::from_ctx: thread_id must be non-empty",
));
}
Ok(Self {
tenant_id: ctx.tenant_id().clone(),
thread_id: thread_id.to_owned(),
})
}
#[must_use]
pub const fn tenant_id(&self) -> &TenantId {
&self.tenant_id
}
#[must_use]
pub fn thread_id(&self) -> &str {
&self.thread_id
}
}