use serde::ser::SerializeMap;
use sonic_rs::Value;
use super::super::api::plugin::RequestContextPluginRead;
use super::RequestContextDomain;
use super::RequestContextError;
pub(crate) const CLIENT_NAME_KEY: &str = "hive::telemetry::client_name";
pub(crate) const CLIENT_VERSION_KEY: &str = "hive::telemetry::client_version";
#[derive(Debug, Clone, Default)]
pub struct TelemetryContext {
pub client_name: Option<String>,
pub client_version: Option<String>,
}
pub struct RequestContextTelemetryRead<'a> {
context: &'a TelemetryContext,
}
impl RequestContextTelemetryRead<'_> {
pub fn client_name(&self) -> Option<&String> {
self.context.client_name.as_ref()
}
pub fn client_version(&self) -> Option<&String> {
self.context.client_version.as_ref()
}
}
impl<Hook> RequestContextPluginRead<Hook> {
pub fn telemetry(&self) -> RequestContextTelemetryRead<'_> {
RequestContextTelemetryRead {
context: &self.snapshot.telemetry,
}
}
}
impl RequestContextDomain for TelemetryContext {
const DOMAIN_PREFIX: &'static str = "hive::telemetry::";
fn set_key_value(&mut self, key: &str, _value: Value) -> Result<(), RequestContextError> {
match key {
CLIENT_NAME_KEY => self.forbidden_mutation(key),
CLIENT_VERSION_KEY => self.forbidden_mutation(key),
_ => self.unknown_key(key),
}
}
super::impl_domain_serde!(
CLIENT_NAME_KEY => client_name,
CLIENT_VERSION_KEY => client_version,
);
}