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
//!
//! Defines everything for the 0x0005 event.
//!
use bitvec::order::Lsb0;
use serde_json::Value;
use crate::protocol::prelude::common::{bits::BitEncoder, error::Error, event::EventEncoder};
///
/// Describe an interaction request.
///
pub struct InteractionRequest {
encoder: BitEncoder<Lsb0>,
request_id: u64,
function_name: String,
parent_name: String,
object_id: Option<i32>,
params: Option<Value>,
token: Option<String>,
}
impl InteractionRequest {
///
/// Creates a new [InteractionRequest].
///
/// # Arguments
/// * `request_id` - The unique request ID.
/// * `function_name` - The name of the function to call.
/// * `parent_name` - The name of the table to interact with.
/// * `object_id` - The ID of the object to interact with.
/// * `params` - The parameters to pass to the function.
/// * `token` - The token to authenticate the request.
///
/// # Returns
/// * [InteractionRequest] - The created [InteractionRequest].
///
/// # Example
/// ```rust
/// use shdp::prelude::client::versions::v1::c0x0005::InteractionRequest;
///
/// let request = InteractionRequest::new(
/// 0,
/// "function_name".to_string(),
/// "parent_name".to_string(),
/// Some(0),
/// Some(serde_json::json!({})),
/// Some("token".to_string()),
/// );
/// ```
///
pub fn new(
request_id: u64,
function_name: String,
parent_name: String,
object_id: Option<i32>,
params: Option<Value>,
token: Option<String>,
) -> Self {
if cfg!(feature = "debug") {
println!(
"[\x1b[38;5;187mSHDP\x1b[0m] \x1b[38;5;21m0x0005\x1b[0m created ({}:{}->{}={:#?}({:#?}, {:#?}))",
request_id,
parent_name,
function_name,
object_id,
params,
token
);
}
InteractionRequest {
encoder: BitEncoder::<Lsb0>::new(),
request_id,
function_name,
parent_name,
object_id,
params,
token,
}
}
}
impl EventEncoder<Lsb0> for InteractionRequest {
fn encode(&mut self) -> Result<(), Error> {
let request_id = self.request_id as u64;
let upper_id = ((request_id >> 32) & 0xffffffff) as u32;
let lower_id = (request_id & 0xffffffff) as u32;
self.encoder.add_data(upper_id, 32)?;
self.encoder.add_data(lower_id, 32)?;
self.encoder.add_bytes(self.function_name.as_bytes())?;
self.encoder.add_data(0, 8)?;
self.encoder.add_bytes(self.parent_name.as_bytes())?;
self.encoder.add_data(0, 8)?;
if self.token.is_some() {
self.encoder
.add_bytes(self.token.as_ref().unwrap().to_string().as_bytes())?;
}
self.encoder.add_data(0, 8)?;
if self.object_id.is_some() {
self.encoder
.add_bytes(self.object_id.unwrap().to_string().as_bytes())?;
}
self.encoder.add_data(0, 8)?;
if self.params.is_some() {
self.encoder
.add_bytes(self.params.as_ref().unwrap().to_string().as_bytes())?;
}
Ok(())
}
fn get_encoder(&self) -> &BitEncoder<Lsb0> {
&self.encoder
}
fn get_event(&self) -> u16 {
0x0005
}
}