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
use generic_array::{ArrayLength, GenericArray};

mod sealed {
    pub trait Sealed {}
}

#[doc(hidden)]
pub trait TimestampStrStorage: sealed::Sealed {
    type Length: ArrayLength<u8>;

    fn init() -> GenericArray<u8, Self::Length>;

    const IS_FULL: bool;
    const HAS_OFFSET: bool;
    const PRECISION: usize;
}

/// Shorthand format without punctuation, (`YYYYMMDDTHHmmss.SSSZ`)
pub struct Short;
/// Full ISO8601 format without offset, (`YYYY-MM-DDTHH:mm:ss.SSSZ`) with character literal `Z` meaning UTC
pub struct Full;
/// Full ISO8601 format with hour/minute timezone offset, (`YYYY-MM-DDTHH:mm:ss.SSS+HZ:MZ`) with offset at end
pub struct FullOffset;
/// Full ISO8601 format without offset, but to nanosecond precision, (`YYYY-MM-DDTHH:mm:ss.SSSSSSSSSZ`)
pub struct FullNanoseconds;
/// Full ISO8601 format without offset, but to microsecond precision, (`YYYY-MM-DDTHH:mm:ss.SSSSSSZ`)
pub struct FullMicroseconds;

impl sealed::Sealed for Short {}
impl sealed::Sealed for Full {}
impl sealed::Sealed for FullOffset {}
impl sealed::Sealed for FullNanoseconds {}
impl sealed::Sealed for FullMicroseconds {}

impl TimestampStrStorage for Short {
    type Length = generic_array::typenum::consts::U20;

    #[inline(always)]
    fn init() -> GenericArray<u8, Self::Length> {
        //nericArray::from(*b"YYYYMMDDTHHmmss.SSSZ")
        GenericArray::from(*b"00000000T000000.000Z")
    }

    const IS_FULL: bool = false;
    const HAS_OFFSET: bool = false;
    const PRECISION: usize = 3;
}

impl TimestampStrStorage for Full {
    type Length = generic_array::typenum::consts::U24;

    #[inline(always)]
    fn init() -> GenericArray<u8, Self::Length> {
        //nericArray::from(*b"YYYY-MM-DDTHH:mm:ss.SSSZ")
        GenericArray::from(*b"0000-00-00T00:00:00.000Z")
    }

    const IS_FULL: bool = true;
    const HAS_OFFSET: bool = false;
    const PRECISION: usize = 3;
}

impl TimestampStrStorage for FullOffset {
    type Length = generic_array::typenum::consts::U29;

    #[inline(always)]
    fn init() -> GenericArray<u8, Self::Length> {
        //nericArray::from(*b"YYYY-MM-DDTHH:mm:ss.SSS+HH:MM")
        GenericArray::from(*b"0000-00-00T00:00:00.000+00:00")
    }

    const IS_FULL: bool = true;
    const HAS_OFFSET: bool = true;
    const PRECISION: usize = 3;
}

impl TimestampStrStorage for FullNanoseconds {
    type Length = generic_array::typenum::consts::U30;

    #[inline(always)]
    fn init() -> GenericArray<u8, Self::Length> {
        //nericArray::from(*b"YYYY-MM-DDTHH:mm:ss.SSSSSSSSSZ")
        GenericArray::from(*b"0000-00-00T00:00:00.000000000Z")
    }

    const IS_FULL: bool = true;
    const HAS_OFFSET: bool = false;
    const PRECISION: usize = 9;
}

impl TimestampStrStorage for FullMicroseconds {
    type Length = generic_array::typenum::consts::U27;

    #[inline(always)]
    fn init() -> GenericArray<u8, Self::Length> {
        //nericArray::from(*b"YYYY-MM-DDTHH:mm:ss.SSSSSSZ")
        GenericArray::from(*b"0000-00-00T00:00:00.000000Z")
    }

    const IS_FULL: bool = true;
    const HAS_OFFSET: bool = false;
    const PRECISION: usize = 6;
}

/// Fixed-size inline string storage that exactly fits the formatted timestamp
pub struct TimestampStr<S: TimestampStrStorage>(pub(crate) GenericArray<u8, S::Length>);

impl<S: TimestampStrStorage> AsRef<str> for TimestampStr<S> {
    #[inline]
    fn as_ref(&self) -> &str {
        unsafe { core::str::from_utf8_unchecked(&self.0) }
    }
}

use core::borrow::Borrow;

impl<S: TimestampStrStorage> Borrow<str> for TimestampStr<S> {
    #[inline]
    fn borrow(&self) -> &str {
        self.as_ref()
    }
}

use core::ops::Deref;

impl<S: TimestampStrStorage> Deref for TimestampStr<S> {
    type Target = str;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

impl<S: TimestampStrStorage> PartialEq for TimestampStr<S> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_ref() == other.as_ref()
    }
}

impl<S: TimestampStrStorage> PartialEq<str> for TimestampStr<S> {
    #[inline]
    fn eq(&self, other: &str) -> bool {
        self.as_ref() == other
    }
}

impl<S: TimestampStrStorage> PartialEq<TimestampStr<S>> for str {
    #[inline]
    fn eq(&self, other: &TimestampStr<S>) -> bool {
        self == other.as_ref()
    }
}

use core::fmt;

impl<S: TimestampStrStorage> fmt::Debug for TimestampStr<S> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.as_ref(), f)
    }
}

impl<S: TimestampStrStorage> fmt::Display for TimestampStr<S> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.as_ref(), f)
    }
}

#[cfg(feature = "serde")]
mod serde_impl {
    use serde::ser::{Serialize, Serializer};

    use super::{TimestampStr, TimestampStrStorage};

    impl<STORAGE: TimestampStrStorage> Serialize for TimestampStr<STORAGE> {
        #[inline]
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_str(&*self)
        }
    }
}