refactor(tests): reformat code and update dependency management
- Reorganized import statements in adapter tests for better readability - Replaced or_insert_with(Vec::new) with or_default() in test closures - Updated Cargo.lock with new dependency versions and checksums - Added TLS features to tonic dependency configuration - Included sqlx, chrono, and uuid dependencies with specific features - Added jsonwebtoken and arc-swap as project dependencies - Reformatted assertion statements to comply with line length limits - Adjusted base64 import order in engine codec module - Updated protobuf include statement formatting
This commit is contained in:
+31
-29
@@ -24,19 +24,18 @@ pub fn encode(packet: &Packet) -> String {
|
||||
|
||||
if let Some(ref data) = packet.data {
|
||||
if packet.has_binary() {
|
||||
let data_with_placeholders = replace_binary_with_placeholders(data, packet.attachment_count());
|
||||
let encoded_data = serde_json::to_string(&data_with_placeholders)
|
||||
.unwrap_or_else(|e| {
|
||||
tracing::error!("Failed to serialize socket packet data: {}", e);
|
||||
"null".to_string()
|
||||
});
|
||||
let data_with_placeholders =
|
||||
replace_binary_with_placeholders(data, packet.attachment_count());
|
||||
let encoded_data = serde_json::to_string(&data_with_placeholders).unwrap_or_else(|e| {
|
||||
tracing::error!("Failed to serialize socket packet data: {}", e);
|
||||
"null".to_string()
|
||||
});
|
||||
result.push_str(&encoded_data);
|
||||
} else {
|
||||
let encoded_data = serde_json::to_string(data)
|
||||
.unwrap_or_else(|e| {
|
||||
tracing::error!("Failed to serialize socket packet data: {}", e);
|
||||
"null".to_string()
|
||||
});
|
||||
let encoded_data = serde_json::to_string(data).unwrap_or_else(|e| {
|
||||
tracing::error!("Failed to serialize socket packet data: {}", e);
|
||||
"null".to_string()
|
||||
});
|
||||
result.push_str(&encoded_data);
|
||||
}
|
||||
}
|
||||
@@ -67,7 +66,8 @@ pub fn decode(input: &str) -> Result<Packet, PacketError> {
|
||||
let type_char = chars.next().ok_or(PacketError::Empty)?;
|
||||
let packet_type = PacketType::try_from(type_char)?;
|
||||
|
||||
let attachment_count = if matches!(packet_type, PacketType::BinaryEvent | PacketType::BinaryAck) {
|
||||
let attachment_count = if matches!(packet_type, PacketType::BinaryEvent | PacketType::BinaryAck)
|
||||
{
|
||||
let mut count_str = String::new();
|
||||
while let Some(&c) = chars.peek() {
|
||||
if c == '-' {
|
||||
@@ -126,7 +126,11 @@ pub fn decode(input: &str) -> Result<Packet, PacketError> {
|
||||
id,
|
||||
attachments: Vec::new(),
|
||||
// Store attachment_count for binary packets; actual attachments come via decode_with_attachments
|
||||
expected_attachments: if attachment_count > 0 { Some(attachment_count) } else { None },
|
||||
expected_attachments: if attachment_count > 0 {
|
||||
Some(attachment_count)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -144,10 +148,10 @@ pub fn decode_with_attachments(
|
||||
packet.attachments = attachments;
|
||||
packet.expected_attachments = None;
|
||||
|
||||
if packet.has_binary() {
|
||||
if let Some(ref data) = packet.data {
|
||||
packet.data = Some(replace_placeholders_with_binary(data, &packet.attachments));
|
||||
}
|
||||
if packet.has_binary()
|
||||
&& let Some(ref data) = packet.data
|
||||
{
|
||||
packet.data = Some(replace_placeholders_with_binary(data, &packet.attachments));
|
||||
}
|
||||
|
||||
Ok(packet)
|
||||
@@ -204,12 +208,12 @@ fn replace_binary_with_placeholders(value: &Value, total_attachments: usize) ->
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_binary_with_placeholders_inner(value: &Value, placeholder_idx: &mut usize) -> Value {
|
||||
fn replace_binary_with_placeholders_inner(value: &Value, _placeholder_idx: &mut usize) -> Value {
|
||||
match value {
|
||||
Value::Array(arr) => {
|
||||
let new_arr: Vec<Value> = arr
|
||||
.iter()
|
||||
.map(|v| replace_binary_with_placeholders_inner(v, placeholder_idx))
|
||||
.map(|v| replace_binary_with_placeholders_inner(v, _placeholder_idx))
|
||||
.collect();
|
||||
Value::Array(new_arr)
|
||||
}
|
||||
@@ -218,7 +222,7 @@ fn replace_binary_with_placeholders_inner(value: &Value, placeholder_idx: &mut u
|
||||
for (k, v) in map {
|
||||
new_map.insert(
|
||||
k.clone(),
|
||||
replace_binary_with_placeholders_inner(v, placeholder_idx),
|
||||
replace_binary_with_placeholders_inner(v, _placeholder_idx),
|
||||
);
|
||||
}
|
||||
Value::Object(new_map)
|
||||
@@ -236,15 +240,13 @@ fn replace_placeholders_with_binary(value: &Value, attachments: &[Vec<u8>]) -> V
|
||||
// Check if this is a placeholder object: { "_placeholder": true, "num": N }
|
||||
if let (Some(Value::Bool(true)), Some(Value::Number(num))) =
|
||||
(map.get("_placeholder"), map.get("num"))
|
||||
&& let Some(idx) = num.as_u64()
|
||||
&& let Some(attachment) = attachments.get(idx as usize)
|
||||
{
|
||||
if let Some(idx) = num.as_u64() {
|
||||
if let Some(attachment) = attachments.get(idx as usize) {
|
||||
return Value::String(base64::Engine::encode(
|
||||
&base64::engine::general_purpose::STANDARD,
|
||||
attachment,
|
||||
));
|
||||
}
|
||||
}
|
||||
return Value::String(base64::Engine::encode(
|
||||
&base64::engine::general_purpose::STANDARD,
|
||||
attachment,
|
||||
));
|
||||
}
|
||||
|
||||
let mut new_map = serde_json::Map::new();
|
||||
@@ -389,4 +391,4 @@ mod tests {
|
||||
assert_eq!(packet.expected_attachments, Some(1));
|
||||
assert_eq!(packet.namespace, "/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user