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;
20#[cfg(feature = "serde-deserialize")]
21pub mod de;
22pub mod decoder;
23pub mod encoder;
24pub(crate) mod leb128;
25pub mod schema;
26pub mod types;
27
28#[cfg(feature = "serde-deserialize")]
29pub use de::DeserError;
30pub use dial9_trace_format_derive::TraceEvent;
31pub use types::DynamicListRef;
32pub use types::DynamicMapRef;
33pub use types::EventEncoder;
34pub use types::FieldValue;
35pub use types::InternedStackFrames;
36pub use types::InternedString;
37pub use types::StackFrames;
38pub use types::TraceField;
39
40use schema::{FieldDef, SchemaEntry};
41use types::FieldValueRef;
42
43/// Trait implemented by `#[derive(TraceEvent)]` for compile-time event types.
44pub trait TraceEvent {
45 /// Decoded form of this event, potentially borrowing from the input buffer.
46 type Ref<'a>;
47
48 /// The event type name (used in schema registration).
49 fn event_name() -> &'static str;
50 /// Field definitions for schema registration.
51 /// When `has_timestamp()` is true, the timestamp is NOT included here —
52 /// it is encoded in the event frame header.
53 fn field_defs() -> Vec<FieldDef>;
54 /// Whether this event type carries a packed timestamp in the event header.
55 fn has_timestamp() -> bool {
56 true
57 }
58 /// Return the event's timestamp in nanoseconds.
59 fn timestamp(&self) -> u64;
60 /// Encode this event's non-timestamp fields into the encoder.
61 fn encode_fields<W: std::io::Write>(
62 &self,
63 enc: &mut types::EventEncoder<'_, W>,
64 ) -> std::io::Result<()>;
65 /// Decode from field values using field definitions for name resolution.
66 /// `timestamp_ns` is the absolute timestamp from the event header (if present).
67 fn decode<'a>(
68 timestamp_ns: Option<u64>,
69 fields: &[FieldValueRef<'a>],
70 field_defs: &[FieldDef],
71 ) -> Option<Self::Ref<'a>>;
72
73 /// Build a SchemaEntry for this event type.
74 fn schema_entry() -> SchemaEntry {
75 SchemaEntry {
76 name: Self::event_name().to_string(),
77 has_timestamp: Self::has_timestamp(),
78 fields: Self::field_defs(),
79 annotations: Vec::new(),
80 }
81 }
82}