Skip to main content

braid_types/
timestamp_opt_f64.rs

1// Copyright (C) The Strand-Braid Authors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4/// serde helpers for `Option<FlydraFloatTimestampLocal>` to store as f64.
5///
6/// A `None` value will be represented as a floating point NaN.
7use crate::*;
8
9/// serialize to f64 when annotating a field with this for serde auto derive
10pub fn serialize<S, CLK>(
11    orig: &Option<FlydraFloatTimestampLocal<CLK>>,
12    serializer: S,
13) -> std::result::Result<S::Ok, S::Error>
14where
15    S: serde::Serializer,
16    CLK: Source,
17{
18    let val = match orig {
19        Some(tl) => tl.as_f64(),
20        None => f64::NAN,
21    };
22    serializer.serialize_f64(val)
23}
24
25/// deserialize from f64 when annotating a field with this for serde auto derive
26pub fn deserialize<'de, D, S>(
27    de: D,
28) -> std::result::Result<Option<FlydraFloatTimestampLocal<S>>, D::Error>
29where
30    D: serde::de::Deserializer<'de>,
31    S: Source,
32{
33    match timestamp_f64::deserialize(de) {
34        Ok(valid) => Ok(Some(valid)),
35        Err(_) => {
36            // TODO: should match on DeserializeError with empty field only,
37            // otherwise, return error. The way this is written, anything
38            // will return a nan.
39            Ok(None)
40        }
41    }
42}