messagepack-core 0.2.4

messagepack for `no_std`
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
411
412
413
414
415
416
417
418
419
420
//! MessagePack timestamp extension values.

mod decode;
mod encode;
use crate::extension::{ExtensionRef, FixedExtension};

pub(crate) const TIMESTAMP_EXTENSION_TYPE: i8 = -1;

/// The error type returned when a checked extension conversion fails
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TryFromTimestampError {
    /// The format is not a valid timestamp type
    InvalidType,
    /// The data length is not valid for timestamp format
    InvalidDataLength,
    /// The payload contains invalid field values (e.g. nanoseconds overflow)
    InvalidData,
}

impl core::fmt::Display for TryFromTimestampError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            TryFromTimestampError::InvalidType => write!(f, "invalid timestamp extension type"),
            TryFromTimestampError::InvalidDataLength => write!(f, "invalid timestamp data length"),
            TryFromTimestampError::InvalidData => write!(f, "invalid timestamp data fields"),
        }
    }
}

impl core::error::Error for TryFromTimestampError {}

/// Represents timestamp 32 extension type.
/// This stores 32bit unsigned seconds
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Timestamp32 {
    secs: u32,
}

impl Timestamp32 {
    /// Create a 32‑bit seconds timestamp.
    pub fn new(seconds: u32) -> Self {
        Self { secs: seconds }
    }

    /// Get seconds since the UNIX epoch.
    pub fn seconds(&self) -> u32 {
        self.secs
    }

    pub(crate) fn to_buf(self) -> [u8; 4] {
        self.secs.to_be_bytes()
    }

    pub(crate) fn from_buf(buf: [u8; 4]) -> Self {
        Self {
            secs: u32::from_be_bytes(buf),
        }
    }
}

impl TryFrom<ExtensionRef<'_>> for Timestamp32 {
    type Error = TryFromTimestampError;

    fn try_from(value: ExtensionRef<'_>) -> Result<Self, Self::Error> {
        if value.r#type != TIMESTAMP_EXTENSION_TYPE {
            return Err(TryFromTimestampError::InvalidType);
        }

        let data = value.data;
        let mut buf = [0u8; 4];
        if data.len() != buf.len() {
            return Err(TryFromTimestampError::InvalidDataLength);
        }

        buf.copy_from_slice(data);
        Ok(Self::from_buf(buf))
    }
}

impl TryFrom<FixedExtension<4>> for Timestamp32 {
    type Error = TryFromTimestampError;

    fn try_from(value: FixedExtension<4>) -> Result<Self, Self::Error> {
        value.as_ref().try_into()
    }
}

impl From<Timestamp32> for FixedExtension<4> {
    fn from(value: Timestamp32) -> Self {
        let buf = value.to_buf();
        FixedExtension::new_fixed(TIMESTAMP_EXTENSION_TYPE, buf)
    }
}

impl From<Timestamp32> for core::time::Duration {
    fn from(value: Timestamp32) -> Self {
        core::time::Duration::from_secs(value.seconds().into())
    }
}

impl TryFrom<core::time::Duration> for Timestamp32 {
    type Error = core::num::TryFromIntError;
    fn try_from(value: core::time::Duration) -> Result<Self, Self::Error> {
        let sec = value.as_secs();
        u32::try_from(sec).map(Self::new)
    }
}

/// MessagePack spec says timestamp64/96 nanoseconds must not be larger than 999_999_999.
///
/// > In timestamp 64 and timestamp 96 formats, nanoseconds must not be larger than 999999999.
pub(crate) const TIMESTAMP_NANO_MAX: u32 = 999_999_999;

/// Represents timestamp 64 extension type.
/// This stores 34bit unsigned seconds and 30bit nanoseconds
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Timestamp64 {
    data: [u8; 8],
}

/// `seconds` or `nanos` cannot be represented
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ConstructTimestampError {
    /// Requested seconds that exceeded the timestamp bit range.
    ExceedSeconds,
    /// Requested nanoseconds that exceeded 999999999.
    ExceedNanos,
}

impl core::fmt::Display for ConstructTimestampError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ConstructTimestampError::ExceedSeconds => {
                write!(f, "seconds exceed representable range")
            }
            ConstructTimestampError::ExceedNanos => {
                write!(f, "nanoseconds exceed {}", TIMESTAMP_NANO_MAX)
            }
        }
    }
}

impl core::error::Error for ConstructTimestampError {}

impl Timestamp64 {
    /// Create a 64‑bit timestamp storing 34‑bit seconds and 30‑bit nanoseconds.
    pub fn new(seconds: u64, nanos: u32) -> Result<Self, ConstructTimestampError> {
        const SECONDS_MAX_LIMIT: u64 = 1 << 34;

        if seconds >= SECONDS_MAX_LIMIT {
            return Err(ConstructTimestampError::ExceedSeconds);
        }

        if nanos > TIMESTAMP_NANO_MAX {
            return Err(ConstructTimestampError::ExceedNanos);
        }

        let mut buf = [0u8; 8];
        buf[..].copy_from_slice(&seconds.to_be_bytes());

        let nano = (nanos << 2).to_be_bytes();
        buf[..3].copy_from_slice(&nano[..3]);
        // Keep lower 2 bits of seconds; overlay top 6 bits from nanos
        buf[3] = (buf[3] & 0b0000_0011) | (nano[3] & 0b1111_1100);

        Ok(Self::from_buf(buf))
    }

    /// Get the nanoseconds component.
    pub fn nanos(&self) -> u32 {
        let mut buf = [0u8; 4];
        buf.copy_from_slice(&self.data[..4]);
        let nanosec = u32::from_be_bytes(buf);
        nanosec >> 2
    }

    /// Get the seconds component.
    pub fn seconds(&self) -> u64 {
        // 34bit mask
        const MASK: u64 = (1 << 34) - 1;
        let mut buf = [0u8; 8];
        buf.copy_from_slice(&self.data[..]);
        let seconds = u64::from_be_bytes(buf);

        seconds & MASK
    }

    pub(crate) fn to_buf(self) -> [u8; 8] {
        self.data
    }

    pub(crate) fn from_buf(buf: [u8; 8]) -> Self {
        Self { data: buf }
    }
}

impl TryFrom<ExtensionRef<'_>> for Timestamp64 {
    type Error = TryFromTimestampError;

    fn try_from(value: ExtensionRef<'_>) -> Result<Self, Self::Error> {
        if value.r#type != TIMESTAMP_EXTENSION_TYPE {
            return Err(TryFromTimestampError::InvalidType);
        }

        let data = value.data;
        let mut buf = [0u8; 8];
        if data.len() != buf.len() {
            return Err(TryFromTimestampError::InvalidDataLength);
        }

        buf.copy_from_slice(data);
        let decoded = Self::from_buf(buf);
        // Validate fields via constructor to enforce invariants
        Self::new(decoded.seconds(), decoded.nanos())
            .map_err(|_| TryFromTimestampError::InvalidData)
    }
}

impl TryFrom<FixedExtension<8>> for Timestamp64 {
    type Error = TryFromTimestampError;

    fn try_from(value: FixedExtension<8>) -> Result<Self, Self::Error> {
        value.as_ref().try_into()
    }
}

impl From<Timestamp64> for FixedExtension<8> {
    fn from(value: Timestamp64) -> Self {
        let buf = value.to_buf();
        FixedExtension::new_fixed(TIMESTAMP_EXTENSION_TYPE, buf)
    }
}

impl From<Timestamp64> for core::time::Duration {
    fn from(value: Timestamp64) -> Self {
        let sec = value.seconds();
        let nano = value.nanos();
        core::time::Duration::from_secs(sec) + core::time::Duration::from_nanos(nano.into())
    }
}

impl TryFrom<core::time::Duration> for Timestamp64 {
    type Error = ConstructTimestampError;

    fn try_from(value: core::time::Duration) -> Result<Self, Self::Error> {
        let secs = value.as_secs();
        let nanos = value.subsec_nanos();
        Timestamp64::new(secs, nanos)
    }
}

/// Represents timestamp 96 extension type.
/// This stores 64bit signed seconds and 32bit nanoseconds
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Timestamp96 {
    nanos: u32,
    secs: i64,
}

impl Timestamp96 {
    /// Create a 96‑bit timestamp storing signed seconds and nanoseconds.
    pub fn new(seconds: i64, nanoseconds: u32) -> Result<Self, ConstructTimestampError> {
        if nanoseconds > TIMESTAMP_NANO_MAX {
            return Err(ConstructTimestampError::ExceedNanos);
        }
        Ok(Self {
            nanos: nanoseconds,
            secs: seconds,
        })
    }

    /// Get the nanoseconds component.
    pub fn nanos(&self) -> u32 {
        self.nanos
    }

    /// Get the seconds component.
    pub fn seconds(&self) -> i64 {
        self.secs
    }

    pub(crate) fn to_buf(self) -> [u8; 12] {
        let mut buf = [0u8; 12];
        buf[..4].copy_from_slice(&self.nanos.to_be_bytes());
        buf[4..].copy_from_slice(&self.secs.to_be_bytes());

        buf
    }

    pub(crate) fn from_buf(buf: [u8; 12]) -> Self {
        let mut nano = [0u8; 4];
        nano.copy_from_slice(&buf[..4]);

        let mut second = [0u8; 8];
        second.copy_from_slice(&buf[4..]);

        Self {
            nanos: u32::from_be_bytes(nano),
            secs: i64::from_be_bytes(second),
        }
    }
}

impl TryFrom<ExtensionRef<'_>> for Timestamp96 {
    type Error = TryFromTimestampError;

    fn try_from(value: ExtensionRef<'_>) -> Result<Self, Self::Error> {
        if value.r#type != TIMESTAMP_EXTENSION_TYPE {
            return Err(TryFromTimestampError::InvalidType);
        }

        let data = value.data;
        let mut buf = [0u8; 12];
        if data.len() != buf.len() {
            return Err(TryFromTimestampError::InvalidDataLength);
        }

        buf.copy_from_slice(data);
        let decoded = Self::from_buf(buf);
        // Validate fields via constructor to enforce invariants
        Self::new(decoded.seconds(), decoded.nanos())
            .map_err(|_| TryFromTimestampError::InvalidData)
    }
}

impl TryFrom<FixedExtension<12>> for Timestamp96 {
    type Error = TryFromTimestampError;

    fn try_from(value: FixedExtension<12>) -> Result<Self, Self::Error> {
        value.as_ref().try_into()
    }
}

impl From<Timestamp96> for FixedExtension<12> {
    fn from(value: Timestamp96) -> Self {
        let buf = value.to_buf();
        FixedExtension::new_fixed(TIMESTAMP_EXTENSION_TYPE, buf)
    }
}

impl TryFrom<Timestamp96> for core::time::Duration {
    type Error = core::num::TryFromIntError;

    fn try_from(value: Timestamp96) -> Result<Self, Self::Error> {
        let secs = u64::try_from(value.seconds())?;
        let nanos = value.nanos();

        Ok(core::time::Duration::from_secs(secs) + core::time::Duration::from_nanos(nanos.into()))
    }
}

impl TryFrom<core::time::Duration> for Timestamp96 {
    type Error = ConstructTimestampError;

    fn try_from(value: core::time::Duration) -> Result<Self, Self::Error> {
        let secs =
            i64::try_from(value.as_secs()).map_err(|_| ConstructTimestampError::ExceedSeconds)?;
        let nanos = value.subsec_nanos();
        Self::new(secs, nanos)
    }
}

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

    #[rstest]
    fn duration_to_timestamp32_roundtrip_within_range() {
        let d = core::time::Duration::from_secs(123);
        let ts32 = Timestamp32::try_from(d).unwrap();
        assert_eq!(ts32.seconds(), 123);
        let back: core::time::Duration = ts32.into();
        assert_eq!(back.as_secs(), 123);
        assert_eq!(back.subsec_nanos(), 0);
    }

    #[rstest]
    fn duration_to_timestamp64_roundtrip() {
        let d = core::time::Duration::from_secs(1_234_567) + core::time::Duration::from_nanos(890);
        let ts64 = Timestamp64::try_from(d).unwrap();
        assert_eq!(ts64.seconds(), 1_234_567);
        assert_eq!(ts64.nanos(), 890);
        let back: core::time::Duration = ts64.into();
        assert_eq!(back, d);
    }

    #[rstest]
    fn timestamp96_to_duration_fails_on_negative() {
        let ts96 = Timestamp96::new(-1, 0).unwrap();
        let res: Result<core::time::Duration, core::num::TryFromIntError> =
            core::time::Duration::try_from(ts96);
        assert!(res.is_err());
    }

    #[rstest]
    fn duration_to_timestamp96_roundtrip() {
        let d = core::time::Duration::from_secs(12_345) + core::time::Duration::from_nanos(678_901);
        let ts = Timestamp96::try_from(d).unwrap();
        assert_eq!(ts.seconds(), 12_345);
        assert_eq!(ts.nanos(), 678_901);
        let back = core::time::Duration::try_from(ts).unwrap();
        assert_eq!(back, d);
    }

    #[rstest]
    fn timestamp64_new_rejects_invalid_nanos() {
        let err = Timestamp64::new(0, 1_000_000_000).unwrap_err();
        assert_eq!(err, ConstructTimestampError::ExceedNanos);
    }

    #[rstest]
    fn timestamp96_new_rejects_invalid_nanos() {
        let err = Timestamp96::new(0, 1_000_000_000).unwrap_err();
        assert_eq!(err, ConstructTimestampError::ExceedNanos);
    }
}

// Submodules provide Encode/Decode impls and their tests.
// tests are colocated in submodules