Skip to main content

tell_encoding/
lib.rs

1mod helpers;
2mod batch;
3mod event;
4mod log;
5
6#[cfg(test)]
7mod batch_test;
8#[cfg(test)]
9mod event_test;
10#[cfg(test)]
11mod log_test;
12
13pub use batch::{encode_batch, encode_batch_into};
14pub use event::{encode_event, encode_event_data, encode_event_data_into};
15pub use log::{encode_log_entry, encode_log_data, encode_log_data_into};
16
17/// API key length in bytes (16 bytes = 32 hex chars).
18pub const API_KEY_LENGTH: usize = 16;
19
20/// UUID length in bytes.
21pub const UUID_LENGTH: usize = 16;
22
23/// IPv6 address length in bytes.
24pub const IPV6_LENGTH: usize = 16;
25
26/// Default protocol version (v1.0 = 100).
27pub const DEFAULT_VERSION: u8 = 100;
28
29/// Schema type for routing batches.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31#[repr(u8)]
32pub enum SchemaType {
33    Unknown = 0,
34    Event = 1,
35    Log = 2,
36}
37
38impl SchemaType {
39    #[inline]
40    pub const fn as_u8(self) -> u8 {
41        self as u8
42    }
43}
44
45/// Event type for analytics events.
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
47#[repr(u8)]
48pub enum EventType {
49    #[default]
50    Unknown = 0,
51    Track = 1,
52    Identify = 2,
53    Group = 3,
54    Alias = 4,
55    Enrich = 5,
56    Context = 6,
57}
58
59impl EventType {
60    #[inline]
61    pub const fn as_u8(self) -> u8 {
62        self as u8
63    }
64}
65
66/// Log event type.
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
68#[repr(u8)]
69pub enum LogEventType {
70    Unknown = 0,
71    #[default]
72    Log = 1,
73    Enrich = 2,
74}
75
76impl LogEventType {
77    #[inline]
78    pub const fn as_u8(self) -> u8 {
79        self as u8
80    }
81}
82
83/// Log severity levels following RFC 5424 + trace.
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
85#[repr(u8)]
86pub enum LogLevel {
87    Emergency = 0,
88    Alert = 1,
89    Critical = 2,
90    Error = 3,
91    Warning = 4,
92    Notice = 5,
93    #[default]
94    Info = 6,
95    Debug = 7,
96    Trace = 8,
97}
98
99impl LogLevel {
100    #[inline]
101    pub const fn as_u8(self) -> u8 {
102        self as u8
103    }
104}
105
106/// Parameters for encoding a single event.
107pub struct EventParams<'a> {
108    pub event_type: EventType,
109    pub timestamp: u64,
110    pub service: Option<&'a str>,
111    pub device_id: Option<&'a [u8; UUID_LENGTH]>,
112    pub session_id: Option<&'a [u8; UUID_LENGTH]>,
113    pub event_name: Option<&'a str>,
114    pub payload: Option<&'a [u8]>,
115}
116
117/// Parameters for encoding a single log entry.
118pub struct LogEntryParams<'a> {
119    pub event_type: LogEventType,
120    pub session_id: Option<&'a [u8; UUID_LENGTH]>,
121    pub level: LogLevel,
122    pub timestamp: u64,
123    pub source: Option<&'a str>,
124    pub service: Option<&'a str>,
125    pub payload: Option<&'a [u8]>,
126}
127
128/// Parameters for encoding a batch.
129pub struct BatchParams<'a> {
130    pub api_key: &'a [u8; API_KEY_LENGTH],
131    pub schema_type: SchemaType,
132    pub version: u8,
133    pub batch_id: u64,
134    pub data: &'a [u8],
135}