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
use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};

use super::*;

/// Error returned by [`SystemTime`] when transforming.
#[derive(Debug, Clone)]
pub enum SystemTimeTransformError {
  /// The buffer is too small to encode the value.
  EncodeBufferTooSmall,
  /// NotEnoughBytes binary data.
  NotEnoughBytes,
  /// Invalid system time.
  InvalidSystemTime(SystemTimeError),
}

impl core::fmt::Display for SystemTimeTransformError {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match self {
    Self::EncodeBufferTooSmall => write!(
      f,
      "buffer is too small, use `Transformable::encoded_len` to pre-allocate a buffer with enough space"
    ),
    Self::NotEnoughBytes => write!(f, "not enough bytes to decode system time"),
    Self::InvalidSystemTime(e) => write!(f, "{e}"),
  }
  }
}

#[cfg(feature = "std")]
impl std::error::Error for SystemTimeTransformError {}

impl Transformable for SystemTime {
  type Error = SystemTimeTransformError;

  fn encode(&self, dst: &mut [u8]) -> Result<usize, Self::Error> {
    if dst.len() < self.encoded_len() {
      return Err(Self::Error::EncodeBufferTooSmall);
    }

    let buf = encode_duration_unchecked(
      self
        .duration_since(UNIX_EPOCH)
        .map_err(Self::Error::InvalidSystemTime)?,
    );
    dst[..ENCODED_LEN].copy_from_slice(&buf);
    Ok(ENCODED_LEN)
  }

  #[cfg(feature = "std")]
  #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
  fn encode_to_writer<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<usize> {
    let mut buf = [0u8; ENCODED_LEN];
    self
      .encode(&mut buf)
      .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
    writer.write_all(&buf).map(|_| ENCODED_LEN)
  }

  #[cfg(feature = "async")]
  #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
  async fn encode_to_async_writer<W: futures_util::io::AsyncWrite + Send + Unpin>(
    &self,
    writer: &mut W,
  ) -> std::io::Result<usize>
  where
    Self::Error: Send + Sync + 'static,
  {
    use futures_util::AsyncWriteExt;

    let mut buf = [0u8; ENCODED_LEN];
    self
      .encode(&mut buf)
      .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
    writer.write_all(&buf).await.map(|_| ENCODED_LEN)
  }

  fn encoded_len(&self) -> usize {
    ENCODED_LEN
  }

  fn decode(src: &[u8]) -> Result<(usize, Self), Self::Error>
  where
    Self: Sized,
  {
    if src.len() < ENCODED_LEN {
      return Err(Self::Error::NotEnoughBytes);
    }

    let (readed, dur) = decode_duration_unchecked(src);
    Ok((readed, UNIX_EPOCH + dur))
  }

  #[cfg(feature = "std")]
  #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
  fn decode_from_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<(usize, Self)>
  where
    Self: Sized,
  {
    let mut buf = [0; ENCODED_LEN];
    reader.read_exact(&mut buf)?;
    let (readed, dur) = decode_duration_unchecked(&buf);
    Ok((readed, UNIX_EPOCH + dur))
  }

  #[cfg(feature = "async")]
  #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
  async fn decode_from_async_reader<R: futures_util::io::AsyncRead + Send + Unpin>(
    reader: &mut R,
  ) -> std::io::Result<(usize, Self)>
  where
    Self: Sized,
    Self::Error: Send + Sync + 'static,
  {
    use futures_util::AsyncReadExt;

    let mut buf = [0; ENCODED_LEN];
    reader.read_exact(&mut buf).await?;
    let (readed, dur) = decode_duration_unchecked(&buf);
    Ok((readed, UNIX_EPOCH + dur))
  }
}

test_transformable!(SystemTime => test_systemtime_transformable({
  let now = SystemTime::now();
  std::thread::sleep(std::time::Duration::from_millis(10));
  now
}));