|
1 |
| -use cgp::core::component::UseContext; |
2 | 1 | use cgp::prelude::*;
|
3 |
| -use hermes_encoding_components::impls::encode_mut::combine::CombineEncoders; |
4 |
| -use hermes_encoding_components::impls::encode_mut::field::EncodeField; |
5 |
| -use hermes_encoding_components::impls::encode_mut::from::DecodeFrom; |
6 |
| -use hermes_encoding_components::traits::decode_mut::MutDecoderComponent; |
7 |
| -use hermes_encoding_components::traits::encode_mut::MutEncoderComponent; |
8 |
| -use hermes_encoding_components::traits::transform::Transformer; |
9 |
| - |
10 |
| -#[derive(Debug, Clone, HasField)] |
11 |
| -pub struct Timestamp { |
12 |
| - pub timestamp: u64, |
13 |
| -} |
| 2 | +use hermes_encoding_components::traits::decode_mut::{CanDecodeMut, MutDecoder}; |
| 3 | +use hermes_encoding_components::traits::encode_mut::{CanEncodeMut, MutEncoder}; |
| 4 | +pub use ibc::primitives::Timestamp; |
14 | 5 |
|
15 | 6 | pub struct EncodeTimestamp;
|
16 | 7 |
|
17 |
| -delegate_components! { |
18 |
| - EncodeTimestamp { |
19 |
| - MutEncoderComponent: CombineEncoders< |
20 |
| - Product![ |
21 |
| - EncodeField<symbol!("timestamp"), UseContext>, |
22 |
| - ], |
23 |
| - >, |
24 |
| - MutDecoderComponent: DecodeFrom<Self, UseContext>, |
| 8 | +impl<Encoding, Strategy> MutEncoder<Encoding, Strategy, Timestamp> for EncodeTimestamp |
| 9 | +where |
| 10 | + Encoding: CanEncodeMut<Strategy, Product![u64]>, |
| 11 | +{ |
| 12 | + fn encode_mut( |
| 13 | + encoding: &Encoding, |
| 14 | + value: &Timestamp, |
| 15 | + buffer: &mut Encoding::EncodeBuffer, |
| 16 | + ) -> Result<(), Encoding::Error> { |
| 17 | + let unix_secs = value.nanoseconds() / 1_000_000_000; |
| 18 | + encoding.encode_mut(&product![unix_secs], buffer)?; |
| 19 | + Ok(()) |
25 | 20 | }
|
26 | 21 | }
|
27 | 22 |
|
28 |
| -impl Transformer for EncodeTimestamp { |
29 |
| - type From = u64; |
30 |
| - type To = Timestamp; |
31 |
| - |
32 |
| - fn transform(timestamp: Self::From) -> Timestamp { |
33 |
| - Timestamp { timestamp } |
| 23 | +impl<Encoding, Strategy> MutDecoder<Encoding, Strategy, Timestamp> for EncodeTimestamp |
| 24 | +where |
| 25 | + Encoding: CanDecodeMut<Strategy, Product![u64]>, |
| 26 | +{ |
| 27 | + fn decode_mut<'a>( |
| 28 | + encoding: &Encoding, |
| 29 | + buffer: &mut Encoding::DecodeBuffer<'a>, |
| 30 | + ) -> Result<Timestamp, Encoding::Error> { |
| 31 | + let product![unix_secs] = encoding.decode_mut(buffer)?; |
| 32 | + Ok(Timestamp::from_nanoseconds(unix_secs * 1_000_000_000)) |
34 | 33 | }
|
35 | 34 | }
|
0 commit comments