-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.rs
More file actions
178 lines (153 loc) · 5.12 KB
/
stream.rs
File metadata and controls
178 lines (153 loc) · 5.12 KB
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
use binary_codec::{FromBytes, ToBytes};
use serde::{Deserialize, Serialize};
use serde_with::base64::{Base64, UrlSafe};
use serde_with::formats::Unpadded;
use serde_with::serde_as;
/// Request body for stream operations, which can be either read or write (append) operations on a slot.
#[serde_as]
#[derive(Debug, FromBytes, ToBytes, Serialize, Deserialize, PartialEq, Clone)]
pub struct StreamRequestBody {
/// Optional data to be written/appended to the slot, present only in write mode
#[serde(default)]
#[dyn_length]
#[toggled_by = "write_mode"]
#[serde_as(as = "Option<Base64<UrlSafe, Unpadded>>")]
data: Option<Vec<u8>>,
/// The slot to be streamed, which can be identified by a numeric ID or a binary key
/// plus the range of bytes to select
#[variant_by = "binary_keys"]
range: SlotRange,
}
/// Response body for stream operations, which can include either the new size of the slot (for writes) or the data read from the slot (for reads).
#[serde_as]
#[derive(Debug, FromBytes, ToBytes, Serialize, Deserialize, PartialEq, Clone)]
pub struct StreamResponseBody {
/// Optional new size of the slot after a write operation, present only in write mode
#[serde(default)]
#[dyn_int]
#[toggled_by = "write_mode"]
new_size: Option<u64>,
/// Optional data read from the slot, present only in read mode
#[serde(default)]
#[toggled_by = "!write_mode"]
#[serde_as(as = "Option<Base64<UrlSafe, Unpadded>>")]
data: Option<Vec<u8>>,
}
/// Range of bytes within a slot to be streamed. Can be binary or numeric depending on the slot type
#[derive(Debug, FromBytes, ToBytes, Serialize, Deserialize, PartialEq, Clone)]
#[no_discriminator]
pub enum SlotRange {
Numeric(
u32,
#[serde(default)]
#[dyn_int]
Option<u64>,
#[serde(default)]
#[dyn_int]
Option<u64>,
),
Binary(
#[dyn_length] String,
#[serde(default)]
#[dyn_int]
Option<u64>,
#[serde(default)]
#[dyn_int]
Option<u64>,
),
}
#[cfg(test)]
mod tests {
use binary_codec::{BinaryDeserializer, BinarySerializer};
use crate::packets::{request::PlabbleRequestPacket, response::PlabbleResponsePacket};
#[test]
fn can_serialize_and_deserialize_stream_get_request() {
let packet: PlabbleRequestPacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Stream"
id = "AAAAAAAAAAAAAAAAAAAAAA"
[body]
# bucket with id AAA.. at slot 7, from byte 08 to byte 0f
range.Numeric = [7, 8, 0x0f]
"#,
)
.unwrap();
let bytes = packet.to_bytes(None).unwrap();
assert_eq!(
"01030000000000000000000000000000000000000007080f",
hex::encode(&bytes)
);
let deserialized = PlabbleRequestPacket::from_bytes(&bytes, None).unwrap();
assert_eq!(packet, deserialized);
}
#[test]
fn can_serialize_and_deserialize_stream_write_request() {
let packet: PlabbleRequestPacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Stream"
id = "AAAAAAAAAAAAAAAAAAAAAA"
binary_keys = true
write_mode = true
[body]
# bucket with id AAA.. at slot 'test'
data = "1KKeSJOs"
range.Binary = ["test"]
"#,
)
.unwrap();
let data = "d4a29e4893ac";
let bytes = packet.to_bytes(None).unwrap();
assert_eq!(
format!(
"01930000000000000000000000000000000006{}04{}",
data,
hex::encode(b"test")
),
hex::encode(&bytes)
);
let deserialized = PlabbleRequestPacket::from_bytes(&bytes, None).unwrap();
assert_eq!(packet, deserialized);
}
#[test]
fn can_serialize_and_deserialize_stream_get_response() {
let packet: PlabbleResponsePacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Stream"
request_counter = 2
[body]
data = "1KKeSJOs"
"#,
)
.unwrap();
let data = "d4a29e4893ac";
let bytes = packet.to_bytes(None).unwrap();
assert_eq!(format!("01030002{}", data), hex::encode(&bytes));
let deserialized = PlabbleResponsePacket::from_bytes(&bytes, None).unwrap();
assert_eq!(packet, deserialized);
}
#[test]
fn can_serialize_and_deserialize_stream_write_response() {
let packet: PlabbleResponsePacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Stream"
request_counter = 2
write_mode = true
[body]
new_size = 7
"#,
)
.unwrap();
let bytes = packet.to_bytes(None).unwrap();
assert_eq!("0113000207", hex::encode(&bytes));
let deserialized = PlabbleResponsePacket::from_bytes(&bytes, None).unwrap();
assert_eq!(packet, deserialized);
}
}