dial9_trace_format/lib.rs
1//! # dial9-trace-format
2//!
3//! A compact binary trace format for recording timestamped events with
4//! schema-driven encoding. Events are described by schemas (registered at
5//! write time) and encoded with delta-compressed timestamps, LEB128 varints,
6//! and an interned string pool.
7//!
8//! ## Crate layout
9//!
10//! - [`encoder`] — high-level [`Encoder`](encoder::Encoder) for writing traces
11//! - [`decoder`] — streaming [`Decoder`](decoder::Decoder) for reading traces
12//! - [`codec`] — wire-format types ([`WireTypeId`](codec::WireTypeId),
13//! [`PoolEntry`](codec::PoolEntry)) that appear in decoded frames
14//! - [`schema`] — [`SchemaEntry`] and
15//! [`FieldDef`] describing event layouts
16//! - [`types`] — field value types, the [`TraceField`]
17//! trait, and the [`EventEncoder`] used by derived code
18
19pub mod codec;
20pub mod decoder;
21pub mod encoder;
22pub(crate) mod leb128;
23pub mod schema;
24pub mod types;
25
26pub use dial9_trace_format_derive::TraceEvent;
27pub use types::EventEncoder;
28pub use types::FieldValue;
29pub use types::InternedStackFrames;
30pub use types::InternedString;
31pub use types::StackFrames;
32pub use types::TraceField;
33
34use schema::{FieldDef, SchemaEntry};
35use types::FieldValueRef;
36
37/// Trait implemented by `#[derive(TraceEvent)]` for compile-time event types.
38pub trait TraceEvent {
39 /// Decoded form of this event, potentially borrowing from the input buffer.
40 type Ref<'a>;
41
42 /// The event type name (used in schema registration).
43 fn event_name() -> &'static str;
44 /// Field definitions for schema registration.
45 /// When `has_timestamp()` is true, the timestamp is NOT included here —
46 /// it is encoded in the event frame header.
47 fn field_defs() -> Vec<FieldDef>;
48 /// Whether this event type carries a packed timestamp in the event header.
49 fn has_timestamp() -> bool {
50 true
51 }
52 /// Return the event's timestamp in nanoseconds.
53 fn timestamp(&self) -> u64;
54 /// Encode this event's non-timestamp fields into the encoder.
55 fn encode_fields<W: std::io::Write>(
56 &self,
57 enc: &mut types::EventEncoder<'_, W>,
58 ) -> std::io::Result<()>;
59 /// Decode from field values using field definitions for name resolution.
60 /// `timestamp_ns` is the absolute timestamp from the event header (if present).
61 fn decode<'a>(
62 timestamp_ns: Option<u64>,
63 fields: &[FieldValueRef<'a>],
64 field_defs: &[FieldDef],
65 ) -> Option<Self::Ref<'a>>;
66
67 /// Build a SchemaEntry for this event type.
68 fn schema_entry() -> SchemaEntry {
69 SchemaEntry {
70 name: Self::event_name().to_string(),
71 has_timestamp: Self::has_timestamp(),
72 fields: Self::field_defs(),
73 }
74 }
75}