use crate::{Color, Payload, TypeValueEncodable};
#[derive(Debug, Clone)]
pub struct GenericEventAttributes<C, M> {
pub category: Option<C>,
pub color: Option<Color>,
pub message: Option<M>,
pub payload: Option<Payload>,
}
#[derive(Debug, Clone)]
pub struct GenericEventAttributesBuilder<C, M> {
category: Option<C>,
color: Option<Color>,
message: Option<M>,
payload: Option<Payload>,
}
impl<C, M> Default for GenericEventAttributesBuilder<C, M> {
fn default() -> Self {
Self {
category: None,
color: None,
message: None,
payload: None,
}
}
}
impl<C, M> GenericEventAttributesBuilder<C, M> {
#[must_use]
pub fn category(mut self, category: impl Into<C>) -> Self {
self.category = Some(category.into());
self
}
#[must_use]
pub fn color(mut self, color: impl Into<Color>) -> Self {
self.color = Some(color.into());
self
}
#[must_use]
pub fn message(mut self, message: impl Into<M>) -> Self {
self.message = Some(message.into());
self
}
#[must_use]
pub(crate) fn clear_message(mut self) -> Self {
self.message = None;
self
}
#[must_use]
pub fn payload(mut self, payload: impl Into<Payload>) -> Self {
self.payload = Some(payload.into());
self
}
pub fn build(self) -> GenericEventAttributes<C, M> {
GenericEventAttributes {
category: self.category,
color: self.color,
message: self.message,
payload: self.payload,
}
}
}
pub trait CategoryEncodable {
fn encode_id(&self) -> u32;
}
impl<C, M> GenericEventAttributes<C, M>
where
C: CategoryEncodable,
M: TypeValueEncodable<Type = nvtx_sys::MessageType, Value = nvtx_sys::MessageValue>,
{
pub fn encode(&self) -> nvtx_sys::EventAttributes {
let (color_type, color_value) = self
.color
.as_ref()
.map_or(Color::default_encoding(), Color::encode);
let (payload_type, payload_value) = self
.payload
.as_ref()
.map_or(Payload::default_encoding(), Payload::encode);
let cat = self
.category
.as_ref()
.map_or(0, CategoryEncodable::encode_id);
let (message_type, message_value) = self
.message
.as_ref()
.map_or(M::default_encoding(), M::encode);
nvtx_sys::EventAttributes {
version: nvtx_sys::NVTX_VERSION as u16,
size: nvtx_sys::NVTX_EVENT_ATTRIBUTES_SIZE as u16,
category: cat,
colorType: i32::from(color_type),
color: color_value,
payloadType: i32::from(payload_type),
reserved0: 0,
payload: payload_value,
messageType: i32::from(message_type),
message: message_value,
}
}
}