use std::borrow::Cow;
use byteorder::{ByteOrder, LittleEndian};
#[derive(Clone, Debug, PartialEq)]
pub struct RawHeader {
pub name: Cow<'static, [u8]>,
pub data: Cow<'static, [u8]>,
}
impl RawHeader {
pub fn new<N, V>(name: N, data: V) -> Self
where
N: Into<Cow<'static, [u8]>>,
V: Into<Cow<'static, [u8]>>,
{
Self {
name: name.into(),
data: data.into(),
}
}
}
pub trait Header {
fn name() -> &'static [u8];
fn data(&self) -> Cow<'static, [u8]>;
fn into_raw(self) -> RawHeader
where
Self: Sized,
{
RawHeader::new(Self::name(), self.data())
}
}
fn pack_u64(v: u64) -> Vec<u8> {
let mut buf = vec![0; 8];
LittleEndian::write_u64(&mut buf[..], v);
buf
}
#[derive(Clone, Debug, PartialEq)]
pub struct TraceId(pub u64);
impl Header for TraceId {
fn name() -> &'static [u8] {
b"trace_id"
}
fn data(&self) -> Cow<'static, [u8]> {
match *self {
TraceId(v) => pack_u64(v).into(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct SpanId(pub u64);
impl Header for SpanId {
fn name() -> &'static [u8] {
b"span_id"
}
fn data(&self) -> Cow<'static, [u8]> {
match *self {
SpanId(v) => pack_u64(v).into(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ParentId(pub u64);
impl Header for ParentId {
fn name() -> &'static [u8] {
b"parent_id"
}
fn data(&self) -> Cow<'static, [u8]> {
match *self {
ParentId(v) => pack_u64(v).into(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct TraceBit(pub bool);
impl Header for TraceBit {
fn name() -> &'static [u8] {
b"trace_bit"
}
fn data(&self) -> Cow<'static, [u8]> {
if let TraceBit(true) = *self {
b"1"[..].into()
} else {
b"0"[..].into()
}
}
}