tzcraft 0.1.0

A schema-driven date & time library: one 128-bit nanosecond timeline, a const civil calendar, and codec-aware wire formats on nextjson / rustbinary.
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Codec integration: one implementation per type, two wire shapes.
//!
//! Every `tzcraft` type implements `nextjson`'s format-neutral contracts
//! (`NsonSchema` + `NsonSerialize` + `NsonDeserialize`) exactly once. The
//! codec selects the wire shape through
//! [`FormatEncoder::is_human_readable`] / [`FormatDecoder::is_human_readable`]:
//!
//! | type         | human-readable (nextjson JSON)      | binary (rustbinary)          |
//! |--------------|-------------------------------------|------------------------------|
//! | `Ticks`      | RFC 3339 string                     | `i128` nanoseconds           |
//! | `Duration`   | ISO 8601 duration string            | `i128` nanoseconds           |
//! | `Date`       | `YYYY-MM-DD` string                 | `i32` days                   |
//! | `TimeOfDay`  | `HH:MM:SS[.f]` string               | `u64` nanoseconds-of-day     |
//! | `CivilDateTime` | `YYYY-MM-DDTHH:MM:SS[.f]` string | packed `i128`                |
//! | `Offset`     | `+08:00` / `Z` string               | `i32` seconds                |
//! | `Zone`       | `UTC` / offset string               | tagged array                 |
//! | `Zoned`      | RFC 3339 string with offset         | `[ticks, offset]` array      |
//! | `Weekday`    | `"Monday"` / number                 | `u8` discriminant            |
//! | `Month`      | `"January"` / number                | `u8` month number            |
//!
//! JSON therefore stays human-readable and self-describing while the binary
//! profile stays compact — with no separate serde module and no feature that
//! toggles the shape.

use alloc::string::ToString;

// `Token` is a private helper; the format-neutral contracts are re-exported
// at the bottom of this module (they are part of the public surface).
use nextjson::Token;

use crate::calendar::{Month, Weekday, NS_PER_DAY};
use crate::date::Date;
use crate::datetime::CivilDateTime;
use crate::duration::Duration;
use crate::format::FractionDigits;
use crate::offset::Offset;
use crate::ticks::Ticks;
use crate::time::TimeOfDay;
use crate::zone::Zone;
use crate::zoned::Zoned;

/// All `tzcraft` types describe their canonical (human) form as a string.
macro_rules! impl_schema_str {
    ($($t:ty),* $(,)?) => {$(
        impl NsonSchema for $t {
            const SCHEMA: TypeSchema = TypeSchema::Str;
        }
    )*};
}
impl_schema_str!(
    Ticks,
    Duration,
    Date,
    TimeOfDay,
    CivilDateTime,
    Offset,
    Zone,
    Zoned,
    Weekday,
    Month,
);

/// Map a `tzcraft` error into the codec's error type.
fn codec_err<E: nextjson::FormatError>(e: crate::error::Error) -> E {
    E::custom(e.to_string())
}

// ---------------------------------------------------------------------------
// Ticks
// ---------------------------------------------------------------------------

impl NsonSerialize for Ticks {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(&self.to_rfc3339(FractionDigits::Auto))
        } else {
            enc.write_i128(self.as_unix_nanos())
        }
    }
}

impl<'de> NsonDeserialize<'de> for Ticks {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let s = dec.string()?;
            let v = Ticks::from_rfc3339(&s).map_err(codec_err)?;
            out.write(v);
        } else {
            out.write(Ticks::from_unix_nanos(dec.i128()?));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Duration
// ---------------------------------------------------------------------------

impl NsonSerialize for Duration {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(&self.to_iso8601())
        } else {
            enc.write_i128(self.as_nanos())
        }
    }
}

impl<'de> NsonDeserialize<'de> for Duration {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let s = dec.string()?;
            let v = Duration::from_iso8601(&s).map_err(codec_err)?;
            out.write(v);
        } else {
            out.write(Duration::from_nanos(dec.i128()?));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Date
// ---------------------------------------------------------------------------

impl NsonSerialize for Date {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(&self.to_iso())
        } else {
            enc.write_i32(self.days_since_epoch())
        }
    }
}

impl<'de> NsonDeserialize<'de> for Date {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let s = dec.string()?;
            let v = Date::from_iso(&s).map_err(codec_err)?;
            out.write(v);
        } else {
            out.write(Date::from_days_since_epoch(dec.i32()?));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// TimeOfDay
// ---------------------------------------------------------------------------

impl NsonSerialize for TimeOfDay {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(&self.to_iso())
        } else {
            enc.write_u64(self.nanos_since_midnight())
        }
    }
}

impl<'de> NsonDeserialize<'de> for TimeOfDay {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let s = dec.string()?;
            let v = TimeOfDay::from_iso(&s).map_err(codec_err)?;
            out.write(v);
        } else {
            let v = TimeOfDay::from_nanos_since_midnight(dec.u64()?).map_err(codec_err)?;
            out.write(v);
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// CivilDateTime
// ---------------------------------------------------------------------------

impl NsonSerialize for CivilDateTime {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(&self.to_iso())
        } else {
            let packed = self.date().days_since_epoch() as i128 * NS_PER_DAY
                + self.time().nanos_since_midnight() as i128;
            enc.write_i128(packed)
        }
    }
}

impl<'de> NsonDeserialize<'de> for CivilDateTime {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let s = dec.string()?;
            let v = CivilDateTime::from_iso(&s).map_err(codec_err)?;
            out.write(v);
        } else {
            let packed = dec.i128()?;
            let days = i64::try_from(packed.div_euclid(NS_PER_DAY))
                .map_err(|_| D::Error::custom("civil date-time out of range"))?;
            let date = Date::from_days_checked(days).map_err(codec_err)?;
            let time = TimeOfDay::from_nanos_since_midnight(packed.rem_euclid(NS_PER_DAY) as u64)
                .map_err(codec_err)?;
            out.write(CivilDateTime::new(date, time));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Offset
// ---------------------------------------------------------------------------

impl NsonSerialize for Offset {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(&self.to_iso())
        } else {
            enc.write_i32(self.as_seconds())
        }
    }
}

impl<'de> NsonDeserialize<'de> for Offset {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let s = dec.string()?;
            let v = Offset::from_iso(&s).map_err(codec_err)?;
            out.write(v);
        } else {
            let v = Offset::from_seconds(dec.i32()?).map_err(codec_err)?;
            out.write(v);
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Zone
// ---------------------------------------------------------------------------

impl NsonSerialize for Zone {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(&self.to_iso())
        } else {
            enc.begin_array()?;
            enc.separator()?;
            match *self {
                Zone::Utc => enc.write_u8(0)?,
                Zone::Fixed(offset) => {
                    enc.write_u8(1)?;
                    enc.separator()?;
                    enc.write_i32(offset.as_seconds())?;
                }
            }
            enc.end_array()
        }
    }
}

impl<'de> NsonDeserialize<'de> for Zone {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let s = dec.string()?;
            let v = Zone::from_iso(&s).map_err(codec_err)?;
            out.write(v);
        } else {
            dec.begin_array()?;
            let tag = dec.u8()?;
            let mut payload: Option<i32> = None;
            let mut count = 1u32;
            while dec.array_has_more()? {
                payload = Some(dec.i32()?);
                count += 1;
                if !dec.array_entry_sep()? {
                    break;
                }
            }
            dec.end_array()?;
            let zone = match (tag, payload, count) {
                (0, None, 1) => Zone::Utc,
                (1, Some(secs), 2) => {
                    let offset = Offset::from_seconds(secs).map_err(codec_err)?;
                    Zone::fixed(offset)
                }
                _ => return Err(D::Error::custom("invalid zone encoding")),
            };
            out.write(zone);
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Zoned
// ---------------------------------------------------------------------------

impl NsonSerialize for Zoned {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(&self.to_rfc3339(FractionDigits::Auto))
        } else {
            enc.begin_array()?;
            enc.separator()?;
            enc.write_i128(self.ticks().as_unix_nanos())?;
            enc.separator()?;
            enc.write_i32(self.zone().offset().as_seconds())?;
            enc.end_array()
        }
    }
}

impl<'de> NsonDeserialize<'de> for Zoned {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let s = dec.string()?;
            let v = Zoned::from_rfc3339(&s).map_err(codec_err)?;
            out.write(v);
        } else {
            dec.begin_array()?;
            let ticks = Ticks::from_unix_nanos(dec.i128()?);
            let offset = if dec.array_has_more()? {
                let seconds = dec.i32()?;
                if dec.array_entry_sep()? {
                    return Err(D::Error::custom("trailing data in zoned encoding"));
                }
                seconds
            } else {
                return Err(D::Error::custom("missing offset in zoned encoding"));
            };
            dec.end_array()?;
            let offset = Offset::from_seconds(offset).map_err(codec_err)?;
            out.write(Zoned::new(ticks, Zone::fixed(offset)));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Weekday / Month
// ---------------------------------------------------------------------------

impl NsonSerialize for Weekday {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(self.name())
        } else {
            enc.write_u8(self.as_u8())
        }
    }
}

impl<'de> NsonDeserialize<'de> for Weekday {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let v = match dec.peek_token()? {
                Token::Number(_) => {
                    let n = dec.u8()?;
                    Weekday::from_discriminant(n)
                        .ok_or_else(|| D::Error::custom("invalid weekday number"))?
                }
                _ => {
                    let s = dec.string()?;
                    Weekday::from_name(&s)
                        .ok_or_else(|| D::Error::custom("unknown weekday name"))?
                }
            };
            out.write(v);
        } else {
            let v = Weekday::from_discriminant(dec.u8()?)
                .ok_or_else(|| D::Error::custom("invalid weekday discriminant"))?;
            out.write(v);
        }
        Ok(())
    }
}

impl NsonSerialize for Month {
    fn nextencode<E: FormatEncoder>(&self, enc: &mut E) -> Result<(), E::Error> {
        if enc.is_human_readable() {
            enc.write_str(self.name())
        } else {
            enc.write_u8(self.as_u32() as u8)
        }
    }
}

impl<'de> NsonDeserialize<'de> for Month {
    fn nextdecode_into<D: FormatDecoder<'de>>(
        dec: &mut D,
        out: &mut DecodeSlot<Self>,
    ) -> Result<(), D::Error> {
        if dec.is_human_readable() {
            let v = match dec.peek_token()? {
                Token::Number(_) => {
                    let n = dec.u8()?;
                    Month::from_u32(n as u32)
                        .ok_or_else(|| D::Error::custom("invalid month number"))?
                }
                _ => {
                    let s = dec.string()?;
                    Month::from_name(&s).ok_or_else(|| D::Error::custom("unknown month name"))?
                }
            };
            out.write(v);
        } else {
            let v = Month::from_u32(dec.u8()? as u32)
                .ok_or_else(|| D::Error::custom("invalid month"))?;
            out.write(v);
        }
        Ok(())
    }
}

/// Re-export of the `nextjson` format-neutral contracts used by `tzcraft`.
///
/// Available as `tzcraft::codec::NsonSerialize` and friends so downstream
/// code can write `tzcraft::codec::nextencode(...)` without naming `nextjson`
/// directly.
pub use nextjson::{
    DecodeSlot, FormatDecoder, FormatEncoder, FormatError, NsonDeserialize, NsonSchema,
    NsonSerialize, TypeSchema,
};