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
// Copyright 2020-2023 Andrew D. Straw.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT
// or http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

/// serde helpers for `Option<FlydraFloatTimestampLocal>` to store as f64.
///
/// A `None` value will be represented as a floating point NaN.
use crate::*;

/// serialize to f64 when annotating a field with this for serde auto derive
pub fn serialize<S, CLK>(
    orig: &Option<FlydraFloatTimestampLocal<CLK>>,
    serializer: S,
) -> std::result::Result<S::Ok, S::Error>
where
    S: serde::Serializer,
    CLK: Source,
{
    let val = match orig {
        Some(ref tl) => tl.as_f64(),
        None => std::f64::NAN,
    };
    serializer.serialize_f64(val)
}

/// deserialize from f64 when annotating a field with this for serde auto derive
pub fn deserialize<'de, D, S>(
    de: D,
) -> std::result::Result<Option<FlydraFloatTimestampLocal<S>>, D::Error>
where
    D: serde::de::Deserializer<'de>,
    S: Source,
{
    match timestamp_f64::deserialize(de) {
        Ok(valid) => Ok(Some(valid)),
        Err(_) => {
            // TODO: should match on DeserializeError with empty field only,
            // otherwise, return error. The way this is written, anything
            // will return a nan.
            Ok(None)
        }
    }
}