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
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
use std::{str::FromStr, sync::OnceLock};

use chrono::{NaiveTime, Timelike};
#[cfg(feature = "std")]
use regex::Regex;
use thiserror_no_std::Error;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LightningTime {
    pub bolts: u8,
    pub bolt_colors: LightningBaseColors,
    pub zaps: u8,
    pub zap_colors: LightningBaseColors,
    pub sparks: u8,
    pub spark_colors: LightningBaseColors,
    pub charges: u8,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LightningBaseColors(pub u8, pub u8);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LightningTimeColors {
    pub bolt: palette::Srgb<u8>,
    pub zap: palette::Srgb<u8>,
    pub spark: palette::Srgb<u8>,
}

impl Default for LightningTime {
    fn default() -> Self {
        Self {
            bolts: 0,
            bolt_colors: LightningBaseColors(161, 0),
            zaps: 0,
            zap_colors: LightningBaseColors(50, 214),
            sparks: 0,
            spark_colors: LightningBaseColors(246, 133),
            charges: 0,
        }
    }
}

impl LightningTime {
    pub fn new(bolts: u8, zaps: u8, sparks: u8, charges: u8) -> Self {
        Self {
            bolts,
            zaps,
            sparks,
            charges,
            ..Default::default()
        }
    }

    pub fn colors(&self) -> LightningTimeColors {
        LightningTimeColors {
            bolt: palette::Srgb::new(
                self.bolts * 16 + self.zaps,
                self.bolt_colors.0,
                self.bolt_colors.1,
            ),
            zap: palette::Srgb::new(
                self.zap_colors.0,
                self.zaps * 16 + self.sparks,
                self.zap_colors.1,
            ),
            spark: palette::Srgb::new(
                self.spark_colors.0,
                self.spark_colors.1,
                self.sparks * 16 + self.charges,
            ),
        }
    }

    #[cfg(feature = "std")]
    pub fn to_stripped_string(&self) -> String {
        format!("{:x}~{:x}~{:x}", self.bolts, self.zaps, self.sparks)
    }
}

const MILLIS_PER_CHARGE: f64 = 86_400_000.0 / 65_536.0; // Div by 16^4

impl From<NaiveTime> for LightningTime {
    fn from(value: NaiveTime) -> Self {
        let millis = 1_000. * 60. * 60. * value.hour() as f64
            + 1_000. * 60. * value.minute() as f64
            + 1_000. * value.second() as f64
            + value.nanosecond() as f64 / 1.0e6;

        let total_charges = millis / MILLIS_PER_CHARGE;
        let total_sparks = total_charges / 16.;
        let total_zaps = total_sparks / 16.;
        let total_bolts = total_zaps / 16.;

        #[cfg(feature = "std")]
        {
            LightningTime {
                bolts: (total_bolts.floor() % 16.) as u8,
                sparks: (total_sparks.floor() % 16.) as u8,
                zaps: (total_zaps.floor() % 16.) as u8,
                charges: (total_charges.floor() % 16.) as u8,
                ..Default::default()
            }
        }

        #[cfg(not(feature = "std"))]
        {
            use libm::floor;
            LightningTime {
                bolts: (floor(total_bolts) % 16.) as u8,
                sparks: (floor(total_sparks) % 16.) as u8,
                zaps: (floor(total_zaps) % 16.) as u8,
                charges: (floor(total_charges) % 16.) as u8,
                ..Default::default()
            }
        }
    }
}

#[cfg(feature = "std")]
static RE: OnceLock<Regex> = OnceLock::new();

#[cfg(feature = "std")]
impl FromStr for LightningTime {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let re = RE.get_or_init(|| {
            Regex::new(r"(?P<bolt>[[:xdigit:]])~(?P<spark>[[:xdigit:]])~(?P<zap>[[:xdigit:]])(?:\|(?P<charge>[[:xdigit:]]))?").unwrap()
        });

        let caps = re.captures(s);
        match caps {
            Some(caps) => {
                if caps.len() < 4 {
                    return Err(Error::InvalidConversion);
                }
                Ok(LightningTime {
                    bolts: u8::from_str_radix(caps.name("bolt").unwrap().as_str(), 16).unwrap(),
                    zaps: u8::from_str_radix(caps.name("zap").unwrap().as_str(), 16).unwrap(),
                    sparks: u8::from_str_radix(caps.name("spark").unwrap().as_str(), 16).unwrap(),
                    charges: caps
                        .name("charge")
                        .map(|c| u8::from_str_radix(c.as_str(), 16).unwrap())
                        .unwrap_or(0),
                    ..Default::default()
                })
            }
            None => Err(Error::InvalidConversion),
        }
    }
}

impl core::fmt::Display for LightningTime {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!(
            "{:x}~{:x}~{:x}|{:x}",
            self.bolts, self.zaps, self.sparks, self.charges
        ))
    }
}

#[derive(Debug, Clone, Copy, Error)]
pub enum Error {
    #[error("Invalid conversion")]
    InvalidConversion,
}

impl TryInto<NaiveTime> for LightningTime {
    type Error = Error;

    fn try_into(self) -> Result<NaiveTime, Self::Error> {
        let elapsed: usize =
            ((self.bolts as usize * 16 + self.zaps as usize) * 16 + self.sparks as usize) * 16
                + self.charges as usize;

        let millis = elapsed as f64 * MILLIS_PER_CHARGE;

        let seconds = millis / 1000.;
        let leftover_millis = millis % 1000.;

        NaiveTime::from_num_seconds_from_midnight_opt(
            seconds as u32,
            (leftover_millis * 1.0e6) as u32,
        )
        .ok_or(Error::InvalidConversion)
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use chrono::{NaiveTime, Timelike};
    use palette::Srgb;

    use crate::{Error, LightningTime, LightningTimeColors};

    #[test]
    fn convert_to_lightning() {
        let real = NaiveTime::from_hms_opt(12, 0, 0).unwrap();
        let lightning = LightningTime::from(real);
        assert_eq!(
            lightning,
            LightningTime {
                bolts: 0x8,
                ..Default::default()
            }
        );

        assert_eq!(lightning.to_string(), "8~0~0|0");
        assert_eq!(lightning.to_stripped_string(), "8~0~0");
        assert_eq!(
            lightning.colors(),
            LightningTimeColors {
                bolt: Srgb::new(0x80, 0xa1, 0x00),
                zap: Srgb::new(0x32, 0x00, 0xd6),
                spark: Srgb::new(0xf6, 0x85, 0x00),
            }
        );
    }

    #[test]
    fn parse() {
        assert!(LightningTime::from_str("f~3~a|8").is_ok());
        assert!(LightningTime::from_str("f~3~a").is_ok());
        assert!(LightningTime::from_str("f~~|").is_err());
    }

    #[test]
    fn convert_to_real() {
        let lightning = LightningTime {
            bolts: 0x8,
            ..Default::default()
        };

        let naive: Result<NaiveTime, Error> = lightning.try_into();

        assert!(naive.is_ok());
        assert_eq!(naive.unwrap(), NaiveTime::from_hms_opt(12, 0, 0).unwrap());

        let lightning = LightningTime {
            bolts: 0x8,
            charges: 0xa,
            ..Default::default()
        };

        let naive: Result<NaiveTime, Error> = lightning.try_into();

        assert!(naive.is_ok());
        // Floating point is not fun
        assert_eq!(
            naive.unwrap().second(),
            NaiveTime::from_hms_opt(12, 0, 13).unwrap().second()
        );
    }
}