zino_http/request/
context.rsuse std::time::Instant;
use zino_core::Uuid;
#[cfg(feature = "i18n")]
use unic_langid::LanguageIdentifier;
#[derive(Debug, Clone)]
pub struct Context {
start_time: Instant,
instance: String,
request_id: Uuid,
trace_id: Uuid,
session_id: Option<String>,
#[cfg(feature = "i18n")]
locale: Option<LanguageIdentifier>,
}
impl Context {
pub fn new(request_id: Uuid) -> Self {
Self {
start_time: Instant::now(),
instance: String::new(),
request_id,
trace_id: Uuid::nil(),
session_id: None,
#[cfg(feature = "i18n")]
locale: None,
}
}
#[inline]
pub fn set_instance(&mut self, instance: impl ToString) {
self.instance = instance.to_string();
}
#[inline]
pub fn set_trace_id(&mut self, trace_id: Uuid) {
self.trace_id = trace_id;
}
#[inline]
pub fn set_session_id(&mut self, session_id: Option<String>) {
self.session_id = session_id;
}
#[cfg(feature = "i18n")]
#[inline]
pub fn set_locale(&mut self, locale: &str) {
match locale.parse() {
Ok(locale) => self.locale = Some(locale),
Err(err) => tracing::error!("{err}: `{locale}`"),
}
}
#[inline]
pub fn start_time(&self) -> Instant {
self.start_time
}
#[inline]
pub fn instance(&self) -> &str {
&self.instance
}
#[inline]
pub fn request_id(&self) -> Uuid {
self.request_id
}
#[inline]
pub fn trace_id(&self) -> Uuid {
self.trace_id
}
#[inline]
pub fn session_id(&self) -> Option<&str> {
self.session_id.as_deref()
}
#[cfg(feature = "i18n")]
pub fn locale(&self) -> Option<&LanguageIdentifier> {
self.locale.as_ref()
}
}