Skip to main content

memlink_msdk/
request.rs

1//! Request/Response types for memlink module calls.
2//!
3//! Defines Request, Response, and CallArgs structures for module method
4//! invocations with support for tracing, deadlines, and serialization.
5
6use serde::{Deserialize, Serialize};
7
8use crate::error::Result;
9use crate::serialize::{default_serializer, Serializer};
10
11#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12pub struct Request {
13    pub method_hash: u32,
14    pub args: Vec<u8>,
15    pub trace_id: u128,
16    pub deadline_ns: Option<u64>,
17}
18
19impl Request {
20    pub fn new(method_hash: u32, args: Vec<u8>) -> Self {
21        Request {
22            method_hash,
23            args,
24            trace_id: 0,
25            deadline_ns: None,
26        }
27    }
28
29    pub fn with_trace_id(mut self, trace_id: u128) -> Self {
30        self.trace_id = trace_id;
31        self
32    }
33
34    pub fn with_deadline(mut self, deadline_ns: u64) -> Self {
35        self.deadline_ns = Some(deadline_ns);
36        self
37    }
38
39    pub fn to_bytes(&self) -> Result<Vec<u8>> {
40        default_serializer().serialize(self)
41    }
42
43    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
44        default_serializer().deserialize(bytes)
45    }
46}
47
48#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
49pub struct Response {
50    pub data: Vec<u8>,
51    pub error_code: Option<i32>,
52}
53
54impl Response {
55    pub fn success(data: Vec<u8>) -> Self {
56        Response {
57            data,
58            error_code: None,
59        }
60    }
61
62    pub fn error(error_code: i32) -> Self {
63        Response {
64            data: vec![],
65            error_code: Some(error_code),
66        }
67    }
68
69    pub fn is_success(&self) -> bool {
70        self.error_code.is_none()
71    }
72
73    pub fn to_bytes(&self) -> Result<Vec<u8>> {
74        default_serializer().serialize(self)
75    }
76
77    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
78        default_serializer().deserialize(bytes)
79    }
80}
81
82#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
83pub struct CallArgs {
84    pub bytes: Vec<u8>,
85}
86
87impl CallArgs {
88    pub fn new(bytes: Vec<u8>) -> Self {
89        CallArgs { bytes }
90    }
91}