durable-streams-server 0.2.0

Durable Streams protocol server in Rust, built with axum and tokio
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::protocol::error::{Error, Result};
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;

/// Offset value with validated format.
///
/// Canonical format: `{read_seq:016x}_{byte_offset:016x}`.
/// Sentinels: `-1` (stream start), `now` (tail/live).
#[derive(Debug, Clone)]
pub enum Offset {
    Start,
    Now,
    Concrete {
        read_seq: u64,
        byte_offset: u64,
        raw: [u8; 33],
    },
}

impl Offset {
    /// Sentinel value for stream start
    pub const START: &'static str = "-1";

    /// Sentinel value for stream tail/live mode
    pub const NOW: &'static str = "now";

    /// Create a new offset from read sequence and byte offset.
    #[must_use]
    pub fn new(read_seq: u64, byte_offset: u64) -> Self {
        Self::Concrete {
            read_seq,
            byte_offset,
            raw: encode_offset(read_seq, byte_offset),
        }
    }

    /// Create the stream start sentinel
    #[must_use]
    pub fn start() -> Self {
        Self::Start
    }

    /// Create the tail/now sentinel
    #[must_use]
    pub fn now() -> Self {
        Self::Now
    }

    /// Check if this is the start sentinel
    #[must_use]
    pub fn is_start(&self) -> bool {
        matches!(self, Self::Start)
    }

    /// Check if this is the now/tail sentinel
    #[must_use]
    pub fn is_now(&self) -> bool {
        matches!(self, Self::Now)
    }

    /// Check if this is a sentinel value (start or now)
    #[must_use]
    pub fn is_sentinel(&self) -> bool {
        matches!(self, Self::Start | Self::Now)
    }

    /// Get the canonical offset string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Start => Self::START,
            Self::Now => Self::NOW,
            Self::Concrete { raw, .. } => {
                // SAFETY: `raw` is always constructed from ASCII hex digits + `_`.
                unsafe { std::str::from_utf8_unchecked(raw) }
            }
        }
    }

    /// Parse the offset into (`read_seq`, `byte_offset`) components.
    ///
    /// Returns `None` for sentinel values.
    #[must_use]
    pub fn parse_components(&self) -> Option<(u64, u64)> {
        match self {
            Self::Concrete {
                read_seq,
                byte_offset,
                ..
            } => Some((*read_seq, *byte_offset)),
            Self::Start | Self::Now => None,
        }
    }
}

impl FromStr for Offset {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        if s == Self::START {
            return Ok(Self::Start);
        }
        if s == Self::NOW {
            return Ok(Self::Now);
        }

        let bytes = s.as_bytes();
        if bytes.len() != 33 || bytes[16] != b'_' {
            return Err(Error::InvalidOffset(format!(
                "Expected format 'read_seq_byte_offset', got '{s}'"
            )));
        }

        for (idx, b) in bytes.iter().copied().enumerate() {
            if idx == 16 {
                continue;
            }
            if b.is_ascii_uppercase() {
                return Err(Error::InvalidOffset(format!(
                    "Offset must use lowercase hex digits: '{s}'"
                )));
            }
            if !b.is_ascii_digit() && !(b'a'..=b'f').contains(&b) {
                let part_num = if idx < 16 { 1 } else { 2 };
                return Err(Error::InvalidOffset(format!(
                    "Invalid hex character in part {part_num} of '{s}'"
                )));
            }
        }

        let read_seq = decode_hex_16(&bytes[..16])
            .ok_or_else(|| Error::InvalidOffset(format!("Failed to parse hex values in '{s}'")))?;
        let byte_offset = decode_hex_16(&bytes[17..])
            .ok_or_else(|| Error::InvalidOffset(format!("Failed to parse hex values in '{s}'")))?;

        Ok(Self::Concrete {
            read_seq,
            byte_offset,
            raw: encode_offset(read_seq, byte_offset),
        })
    }
}

impl fmt::Display for Offset {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl From<Offset> for String {
    fn from(offset: Offset) -> Self {
        offset.as_str().to_string()
    }
}

impl PartialEq for Offset {
    fn eq(&self, other: &Self) -> bool {
        match (self.parse_components(), other.parse_components()) {
            (Some(a), Some(b)) => a == b,
            _ => self.as_str() == other.as_str(),
        }
    }
}

impl Eq for Offset {}

impl PartialOrd for Offset {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Offset {
    fn cmp(&self, other: &Self) -> Ordering {
        match (self.parse_components(), other.parse_components()) {
            (Some((a_rs, a_bo)), Some((b_rs, b_bo))) => (a_rs, a_bo).cmp(&(b_rs, b_bo)),
            _ => self.as_str().cmp(other.as_str()),
        }
    }
}

impl Hash for Offset {
    fn hash<H: Hasher>(&self, state: &mut H) {
        if let Some((read_seq, byte_offset)) = self.parse_components() {
            0u8.hash(state);
            read_seq.hash(state);
            byte_offset.hash(state);
        } else {
            1u8.hash(state);
            self.as_str().hash(state);
        }
    }
}

fn encode_offset(read_seq: u64, byte_offset: u64) -> [u8; 33] {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut raw = [0u8; 33];

    for (i, slot) in raw[..16].iter_mut().enumerate() {
        let shift = (15 - i) * 4;
        *slot = HEX[((read_seq >> shift) & 0xF) as usize];
    }
    raw[16] = b'_';
    for (i, slot) in raw[17..].iter_mut().enumerate() {
        let shift = (15 - i) * 4;
        *slot = HEX[((byte_offset >> shift) & 0xF) as usize];
    }

    raw
}

fn decode_hex_16(bytes: &[u8]) -> Option<u64> {
    if bytes.len() != 16 {
        return None;
    }

    let mut value = 0u64;
    for b in bytes {
        let digit = match b {
            b'0'..=b'9' => b - b'0',
            b'a'..=b'f' => b - b'a' + 10,
            _ => return None,
        };
        value = (value << 4) | u64::from(digit);
    }
    Some(value)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_offset_new() {
        let offset = Offset::new(0, 0);
        assert_eq!(offset.as_str(), "0000000000000000_0000000000000000");

        let offset = Offset::new(1, 42);
        assert_eq!(offset.as_str(), "0000000000000001_000000000000002a");

        let offset = Offset::new(u64::MAX, u64::MAX);
        assert_eq!(offset.as_str(), "ffffffffffffffff_ffffffffffffffff");
    }

    #[test]
    fn test_offset_sentinels() {
        let start = Offset::start();
        assert!(start.is_start());
        assert!(start.is_sentinel());
        assert!(!start.is_now());
        assert_eq!(start.as_str(), "-1");

        let now = Offset::now();
        assert!(now.is_now());
        assert!(now.is_sentinel());
        assert!(!now.is_start());
        assert_eq!(now.as_str(), "now");
    }

    #[test]
    fn test_offset_parse_valid() {
        let offset: Offset = "0000000000000000_0000000000000000".parse().unwrap();
        assert_eq!(offset.as_str(), "0000000000000000_0000000000000000");

        let offset: Offset = "0000000000000001_000000000000002a".parse().unwrap();
        assert_eq!(offset.as_str(), "0000000000000001_000000000000002a");

        let offset: Offset = "-1".parse().unwrap();
        assert!(offset.is_start());

        let offset: Offset = "now".parse().unwrap();
        assert!(offset.is_now());
    }

    #[test]
    fn test_offset_parse_invalid() {
        // Wrong separator
        assert!(
            "0000000000000000-0000000000000000"
                .parse::<Offset>()
                .is_err()
        );

        // Too short
        assert!("000_000".parse::<Offset>().is_err());

        // Too long
        assert!(
            "00000000000000000_0000000000000000"
                .parse::<Offset>()
                .is_err()
        );

        // Uppercase (not canonical)
        assert!(
            "000000000000000A_0000000000000000"
                .parse::<Offset>()
                .is_err()
        );

        // Invalid hex
        assert!(
            "000000000000000g_0000000000000000"
                .parse::<Offset>()
                .is_err()
        );

        // Missing underscore
        assert!(
            "00000000000000000000000000000000"
                .parse::<Offset>()
                .is_err()
        );

        // Extra parts
        assert!(
            "0000000000000000_0000000000000000_0000000000000000"
                .parse::<Offset>()
                .is_err()
        );
    }

    #[test]
    fn test_offset_ordering() {
        let offset1 = Offset::new(0, 0);
        let offset2 = Offset::new(0, 1);
        let offset3 = Offset::new(1, 0);
        let offset4 = Offset::new(1, 1);

        assert!(offset1 < offset2);
        assert!(offset2 < offset3);
        assert!(offset3 < offset4);
        assert!(offset1 < offset4);

        // Lexicographic ordering equals temporal ordering
        assert_eq!(offset1.as_str() < offset2.as_str(), offset1 < offset2);
    }

    #[test]
    fn test_offset_parse_components() {
        let offset = Offset::new(42, 100);
        let (read_seq, byte_offset) = offset.parse_components().unwrap();
        assert_eq!(read_seq, 42);
        assert_eq!(byte_offset, 100);

        // Sentinels return None
        assert!(Offset::start().parse_components().is_none());
        assert!(Offset::now().parse_components().is_none());
    }

    #[test]
    fn test_offset_sentinel_ordering() {
        let start = Offset::start();
        let now = Offset::now();
        let zero = Offset::new(0, 0);
        let mid = Offset::new(5, 10);

        // "-1" < "0000..." (ASCII '-' < '0')
        assert!(start < zero);
        assert!(start < mid);

        // "now" > "0000..." (ASCII 'n' > '0')
        assert!(now > zero);
        assert!(now > mid);

        // Sentinels are not equal to each other
        assert_ne!(start, now);

        // Same sentinel is equal to itself
        assert_eq!(Offset::start(), Offset::start());
        assert_eq!(Offset::now(), Offset::now());
    }

    #[test]
    fn test_offset_equality_and_hash() {
        use std::collections::HashSet;

        let a = Offset::new(1, 2);
        let b = Offset::new(1, 2);
        let c = Offset::new(1, 3);

        assert_eq!(a, b);
        assert_ne!(a, c);

        // Equal offsets must produce the same hash (HashSet insertion)
        let mut set = HashSet::new();
        set.insert(a.as_str().to_string());
        assert!(set.contains(b.as_str()));

        // Sentinels hash consistently
        let mut set2 = HashSet::new();
        set2.insert(Offset::start().as_str().to_string());
        set2.insert(Offset::now().as_str().to_string());
        assert_eq!(set2.len(), 2);
    }

    #[test]
    fn test_offset_display() {
        let offset = Offset::new(1, 2);
        assert_eq!(format!("{offset}"), "0000000000000001_0000000000000002");

        let start = Offset::start();
        assert_eq!(format!("{start}"), "-1");

        let now = Offset::now();
        assert_eq!(format!("{now}"), "now");
    }
}