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
// TODO: change Vec<u8> to Bytes
// pin: https://github.com/danburkert/prost/pull/190

#[macro_use]
mod r#macro;
pub mod epoch;
pub mod primitive;
pub mod receipt;
#[cfg(test)]
mod tests;
pub mod transaction;

use std::error::Error;

use async_trait::async_trait;
use bytes::Bytes;
use derive_more::{Display, From};

use crate::{ProtocolError, ProtocolErrorKind, ProtocolResult};

pub use serde::{Deserialize, Serialize};

#[async_trait]
pub trait ProtocolCodec: Sized + Send + ProtocolCodecSync {
    // Note: We take mut reference so that it can be pinned. This removes Sync
    // requirement.
    async fn encode(&mut self) -> ProtocolResult<Bytes>;

    async fn decode<B: Into<Bytes> + Send>(bytes: B) -> ProtocolResult<Self>;
}

// Sync version is still useful in some cases, for example, use in Stream.
// This also work around #[async_trait] problem inside macro
#[doc(hidden)]
pub trait ProtocolCodecSync: Sized + Send {
    fn encode_sync(&self) -> ProtocolResult<Bytes>;

    fn decode_sync(bytes: Bytes) -> ProtocolResult<Self>;
}

#[async_trait]
impl<T: ProtocolCodecSync + 'static> ProtocolCodec for T {
    async fn encode(&mut self) -> ProtocolResult<Bytes> {
        <T as ProtocolCodecSync>::encode_sync(self)
    }

    async fn decode<B: Into<Bytes> + Send>(bytes: B) -> ProtocolResult<Self> {
        let bytes: Bytes = bytes.into();

        <T as ProtocolCodecSync>::decode_sync(bytes)
    }
}

#[derive(Debug, From, Display)]
pub enum CodecError {
    #[display(fmt = "prost encode: {}", _0)]
    ProtobufEncode(muta_vendor_prost::EncodeError),

    #[display(fmt = "prost decode: {}", _0)]
    ProtobufDecode(muta_vendor_prost::DecodeError),

    #[display(fmt = "{} missing field {}", r#type, field)]
    MissingField {
        r#type: &'static str,
        field:  &'static str,
    },

    #[display(fmt = "invalid contract type {}", _0)]
    InvalidContractType(i32),

    #[display(fmt = "wrong bytes length: {{ expect: {}, got: {} }}", expect, real)]
    WrongBytesLength { expect: usize, real: usize },

    #[display(fmt = "from string {}", _0)]
    FromStringUtf8(std::string::FromUtf8Error),
}

impl Error for CodecError {}

// TODO: derive macro
impl From<CodecError> for ProtocolError {
    fn from(err: CodecError) -> ProtocolError {
        ProtocolError::new(ProtocolErrorKind::Codec, Box::new(err))
    }
}