thrift_codec/
message.rs

1//! RPC message.
2use crate::data::Struct;
3
4/// RPC message.
5#[derive(Debug, Clone, PartialEq)]
6#[cfg_attr(feature = "serde", derive(Serialize))]
7pub struct Message {
8    method_name: String,
9    kind: MessageKind,
10    sequence_id: i32,
11    body: Struct,
12}
13impl Message {
14    /// Makes a new `Message` instance.
15    pub fn new(method_name: &str, kind: MessageKind, sequence_id: i32, body: Struct) -> Self {
16        Message {
17            method_name: method_name.to_owned(),
18            kind,
19            sequence_id,
20            body,
21        }
22    }
23
24    /// Makes a new `Message` instance which has the kind `MessageKind::Call`.
25    pub fn call(method_name: &str, sequence_id: i32, body: Struct) -> Self {
26        Self::new(method_name, MessageKind::Call, sequence_id, body)
27    }
28
29    /// Makes a new `Message` instance which has the kind `MessageKind::Reply`.
30    pub fn reply(method_name: &str, sequence_id: i32, body: Struct) -> Self {
31        Self::new(method_name, MessageKind::Reply, sequence_id, body)
32    }
33
34    /// Makes a new `Message` instance which has the kind `MessageKind::Exception`.
35    pub fn exception(method_name: &str, sequence_id: i32, body: Struct) -> Self {
36        Self::new(method_name, MessageKind::Exception, sequence_id, body)
37    }
38
39    /// Makes a new `Message` instance which has the kind `MessageKind::Oneway`.
40    pub fn oneway(method_name: &str, sequence_id: i32, body: Struct) -> Self {
41        Self::new(method_name, MessageKind::Oneway, sequence_id, body)
42    }
43
44    /// Returns the method name of this message.
45    pub fn method_name(&self) -> &str {
46        &self.method_name
47    }
48
49    /// Returns the kind of this message.
50    pub fn kind(&self) -> MessageKind {
51        self.kind
52    }
53
54    /// Returns the sequence id of this message.
55    pub fn sequence_id(&self) -> i32 {
56        self.sequence_id
57    }
58
59    /// Returns the body of this message.
60    pub fn body(&self) -> &Struct {
61        &self.body
62    }
63}
64
65/// The kind of a message.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
67#[cfg_attr(feature = "serde", derive(Serialize))]
68#[allow(missing_docs)]
69pub enum MessageKind {
70    Call = 1,
71    Reply = 2,
72    Exception = 3,
73    Oneway = 4,
74}
75impl MessageKind {
76    pub(crate) fn from_u8(n: u8) -> Option<Self> {
77        match n {
78            1 => Some(MessageKind::Call),
79            2 => Some(MessageKind::Reply),
80            3 => Some(MessageKind::Exception),
81            4 => Some(MessageKind::Oneway),
82            _ => None,
83        }
84    }
85}