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
use std::str::FromStr;
use std::time::Duration;

use crate::{Error, Monotonic, Timestamp};

#[cfg(feature = "chrono")]
impl FromStr for Timestamp {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        #[cfg(not(feature = "as-float-secs"))]
        if let Ok(v) = s.parse::<u64>() {
            return Ok(v.into());
        }
        #[cfg(feature = "as-float-secs")]
        if let Ok(v) = s.parse::<f64>() {
            return Ok(v.into());
        }
        dateparser::parse(s)
            .map_err(|e| Error::Parse(e.to_string()))?
            .try_into()
    }
}

#[cfg(not(feature = "chrono"))]
impl FromStr for Timestamp {
    type Err = Error;
    #[cfg(not(feature = "as-float-secs"))]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(s.parse::<u64>()?.into())
    }
    #[cfg(feature = "as-float-secs")]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(s.parse::<f64>()?.into())
    }
}

impl FromStr for Monotonic {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(s.parse::<u64>()?.into())
    }
}

macro_rules! impl_common_convert {
    ($t: ty) => {
        // Duration
        //
        impl From<Duration> for $t {
            fn from(value: Duration) -> Self {
                Self(value)
            }
        }

        impl From<$t> for Duration {
            fn from(value: $t) -> Self {
                value.0
            }
        }

        // i64

        impl TryFrom<i64> for $t {
            type Error = Error;
            fn try_from(value: i64) -> Result<Self, Self::Error> {
                Ok(Self::from_nanos(u64::try_from(value)?))
            }
        }

        impl TryFrom<$t> for i64 {
            type Error = Error;
            fn try_from(value: $t) -> Result<Self, Self::Error> {
                value.as_nanos().try_into().map_err(Into::into)
            }
        }

        // u64

        impl From<u64> for $t {
            fn from(value: u64) -> Self {
                Self::from_nanos(value)
            }
        }

        impl TryFrom<$t> for u64 {
            type Error = Error;
            fn try_from(value: $t) -> Result<Self, Error> {
                value.as_nanos().try_into().map_err(Into::into)
            }
        }

        // u128

        impl From<$t> for u128 {
            fn from(value: $t) -> Self {
                value.as_nanos()
            }
        }

        impl TryFrom<u128> for $t {
            type Error = Error;
            fn try_from(value: u128) -> Result<Self, Self::Error> {
                Ok(<$t>::from_nanos(value.try_into()?))
            }
        }

        // f32

        impl From<f32> for $t {
            fn from(value: f32) -> Self {
                Self::from_secs_f32(value)
            }
        }

        impl From<$t> for f32 {
            fn from(value: $t) -> Self {
                value.as_secs_f32()
            }
        }

        // f64

        impl From<f64> for $t {
            fn from(value: f64) -> Self {
                Self::from_secs_f64(value)
            }
        }

        impl From<$t> for f64 {
            fn from(value: $t) -> Self {
                value.as_secs_f64()
            }
        }
    };
}

impl_common_convert!(Timestamp);
impl_common_convert!(Monotonic);