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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use faststr::FastStr;
use pilota::thrift::{DecodeError, EncodeError, Message, TAsyncInputProtocol};

use crate::{
    context::{ClientContext, ServerContext, ThriftContext},
    protocol::{
        TInputProtocol, TLengthProtocol, TMessageIdentifier, TMessageType, TOutputProtocol,
    },
    ApplicationError, ApplicationErrorKind, EntryMessage,
};

#[derive(Debug)]
pub struct MessageMeta {
    pub msg_type: TMessageType,
    pub(crate) method: FastStr,
    pub(crate) seq_id: i32,
}

#[derive(Debug)]
pub struct ThriftMessage<M> {
    pub data: Result<M, crate::Error>,
    pub meta: MessageMeta,
}

pub(crate) struct DummyMessage;

#[async_trait::async_trait]
impl EntryMessage for DummyMessage {
    #[inline]
    fn encode<T: TOutputProtocol>(&self, _protocol: &mut T) -> Result<(), EncodeError> {
        unreachable!()
    }

    #[inline]
    fn decode<T: TInputProtocol>(
        _protocol: &mut T,
        _msg_ident: &TMessageIdentifier,
    ) -> Result<Self, DecodeError> {
        unreachable!()
    }

    #[inline]
    async fn decode_async<T: TAsyncInputProtocol>(
        _protocol: &mut T,
        _msg_ident: &TMessageIdentifier,
    ) -> Result<Self, DecodeError> {
        unreachable!()
    }

    fn size<T: TLengthProtocol>(&self, _protocol: &mut T) -> usize {
        unreachable!()
    }
}

impl<M> ThriftMessage<M> {
    #[inline]
    pub fn mk_client_msg(
        cx: &ClientContext,
        msg: Result<M, crate::Error>,
    ) -> Result<Self, crate::Error> {
        let meta = MessageMeta {
            msg_type: cx.message_type,
            method: cx.rpc_info.method.clone().unwrap(),
            seq_id: cx.seq_id,
        };
        Ok(Self { data: msg, meta })
    }

    #[inline]
    pub fn mk_server_resp(
        cx: &ServerContext,
        msg: Result<M, crate::Error>,
    ) -> Result<Self, crate::Error> {
        let meta = MessageMeta {
            msg_type: match msg {
                Ok(_) => TMessageType::Reply,
                Err(_) => TMessageType::Exception,
            },
            method: cx.rpc_info.method.clone().unwrap_or_else(|| "".into()),
            seq_id: cx.seq_id.unwrap_or(0),
        };
        Ok(Self { data: msg, meta })
    }
}

impl<U> ThriftMessage<U>
where
    U: EntryMessage,
{
    pub(crate) fn size<T: TLengthProtocol>(&self, protocol: &mut T) -> usize {
        let ident = TMessageIdentifier::new(
            self.meta.method.clone(),
            self.meta.msg_type,
            self.meta.seq_id,
        );

        match &self.data {
            Ok(inner) => {
                protocol.write_message_begin_len(&ident)
                    + inner.size(protocol)
                    + protocol.write_message_end_len()
            }
            Err(inner) => match inner {
                crate::Error::Application(e) => {
                    protocol.write_message_begin_len(&ident)
                        + e.size(protocol)
                        + protocol.write_message_end_len()
                }
                _ => 0,
            },
        }
    }
}

impl<U> ThriftMessage<U>
where
    U: EntryMessage + Send,
{
    pub(crate) fn encode<T: TOutputProtocol>(&self, protocol: &mut T) -> Result<(), EncodeError> {
        let ident = TMessageIdentifier::new(
            self.meta.method.clone(),
            self.meta.msg_type,
            self.meta.seq_id,
        );
        match &self.data {
            Ok(v) => {
                protocol.write_message_begin(&ident)?;
                v.encode(protocol)?;
            }
            Err(e) => match e {
                crate::Error::Application(e) => {
                    protocol.write_message_begin(&ident)?;
                    e.encode(protocol)?;
                }
                crate::Error::Protocol(e) => {
                    protocol.write_message_begin(&ident)?;
                    let e = ApplicationError::new(
                        ApplicationErrorKind::ProtocolError,
                        e.message.clone(),
                    );
                    e.encode(protocol)?;
                }
                crate::Error::Transport(e) => {
                    panic!("should not call send when there is a transport error: {e:?}");
                }
            },
        }
        protocol.write_message_end()?;
        Ok(())
    }

    pub(crate) fn decode<Cx: ThriftContext, T: TInputProtocol>(
        protocol: &mut T,
        cx: &mut Cx,
    ) -> Result<Self, DecodeError> {
        let msg_ident = protocol.read_message_begin()?;

        cx.handle_decoded_msg_ident(&msg_ident);

        let res = match msg_ident.message_type {
            TMessageType::Exception => Err(crate::Error::Application(Message::decode(protocol)?)),
            _ => Ok(U::decode(protocol, &msg_ident)?),
        };
        protocol.read_message_end()?;
        Ok(ThriftMessage {
            data: res,
            meta: MessageMeta {
                msg_type: msg_ident.message_type,
                method: msg_ident.name,
                seq_id: msg_ident.sequence_number,
            },
        })
    }

    pub(crate) async fn decode_async<Cx: ThriftContext + Send, T: TAsyncInputProtocol>(
        protocol: &mut T,
        cx: &mut Cx,
    ) -> Result<Self, DecodeError> {
        let msg_ident = protocol.read_message_begin().await?;

        cx.handle_decoded_msg_ident(&msg_ident);

        let res = match msg_ident.message_type {
            TMessageType::Exception => Err(crate::Error::Application(
                Message::decode_async(protocol).await?,
            )),
            _ => Ok(U::decode_async(protocol, &msg_ident).await?),
        };
        protocol.read_message_end().await?;
        Ok(ThriftMessage {
            data: res,
            meta: MessageMeta {
                msg_type: msg_ident.message_type,
                method: msg_ident.name,
                seq_id: msg_ident.sequence_number,
            },
        })
    }
}