Skip to content

Commit 0272269

Browse files
committed
feat: replace .unwrap usages with ExpectedSome errors in protocol
1 parent e9c8074 commit 0272269

12 files changed

Lines changed: 79 additions & 93 deletions

crates/protocol/src/version/v662/packets/command_block_update.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ impl<V: ProtoVersion> ProtoCodec for CommandBlockUpdatePacket<V> {
2828
match &self.is_block {
2929
false => {
3030
<V::ActorRuntimeID as ProtoCodec>::serialize(
31-
self.target_runtime_id.as_ref().unwrap(),
31+
self.target_runtime_id.as_ref().ok_or(ProtoCodecError::ExpectedSome("target_runtime_id"))?,
3232
stream,
3333
)?;
3434
}
3535
true => {
3636
<V::NetworkBlockPosition as ProtoCodec>::serialize(
37-
self.block_position.as_ref().unwrap(),
37+
self.block_position.as_ref().ok_or(ProtoCodecError::ExpectedSome("block_position"))?,
3838
stream,
3939
)?;
4040
<V::CommandBlockMode as ProtoCodec>::serialize(
41-
self.command_block_mode.as_ref().unwrap(),
41+
self.command_block_mode.as_ref().ok_or(ProtoCodecError::ExpectedSome("command_block_mode"))?,
4242
stream,
4343
)?;
44-
<bool as ProtoCodec>::serialize(self.redstone_mode.as_ref().unwrap(), stream)?;
45-
<bool as ProtoCodec>::serialize(self.is_conditional.as_ref().unwrap(), stream)?;
44+
<bool as ProtoCodec>::serialize(self.redstone_mode.as_ref().ok_or(ProtoCodecError::ExpectedSome("redstone_mode"))?, stream)?;
45+
<bool as ProtoCodec>::serialize(self.is_conditional.as_ref().ok_or(ProtoCodecError::ExpectedSome("is_conditional"))?, stream)?;
4646
}
4747
}
4848
<String as ProtoCodec>::serialize(&self.command, stream)?;
@@ -108,10 +108,10 @@ impl<V: ProtoVersion> ProtoCodec for CommandBlockUpdatePacket<V> {
108108
fn size_hint(&self) -> usize {
109109
size_of::<bool>()
110110
+ match &self.is_block {
111-
false => self.target_runtime_id.as_ref().unwrap().size_hint(),
111+
false => self.target_runtime_id.as_ref().map_or(0, ProtoCodec::size_hint),
112112
true => {
113-
self.block_position.as_ref().unwrap().size_hint()
114-
+ self.command_block_mode.as_ref().unwrap().size_hint()
113+
self.block_position.as_ref().map_or(0, ProtoCodec::size_hint)
114+
+ self.command_block_mode.as_ref().map_or(0, ProtoCodec::size_hint)
115115
+ size_of::<bool>()
116116
+ size_of::<bool>()
117117
}

crates/protocol/src/version/v662/packets/player_auth_input.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,33 +123,33 @@ impl<V: ProtoVersion> ProtoCodec for PlayerAuthInputPacket<V> {
123123
<V::NewInteractionModel as ProtoCodec>::serialize(&self.new_interaction_model, stream)?;
124124
if let ClientPlayMode::Reality = &self.play_mode {
125125
<(f32, f32, f32) as ProtoCodecLE>::serialize(
126-
self.vr_gaze_direction.as_ref().unwrap(),
126+
self.vr_gaze_direction.as_ref().ok_or(ProtoCodecError::ExpectedSome("vr_gaze_direction"))?,
127127
stream,
128128
)?;
129129
}
130130
<u64 as ProtoCodecVAR>::serialize(&self.client_tick, stream)?;
131131
<(f32, f32, f32) as ProtoCodecLE>::serialize(&self.velocity, stream)?;
132132
if self.input_data & PlayerAuthInputFlags::PerformItemInteraction as u64 != 0 {
133133
<V::PackedItemUseLegacyInventoryTransaction as ProtoCodec>::serialize(
134-
self.item_use_transaction.as_ref().unwrap(),
134+
self.item_use_transaction.as_ref().ok_or(ProtoCodecError::ExpectedSome("item_use_transaction"))?,
135135
stream,
136136
)?;
137137
}
138138
if self.input_data & PlayerAuthInputFlags::PerformItemStackRequest as u64 != 0 {
139139
<PerformItemStackRequestData<V> as ProtoCodec>::serialize(
140-
self.item_stack_request.as_ref().unwrap(),
140+
self.item_stack_request.as_ref().ok_or(ProtoCodecError::ExpectedSome("item_stack_request"))?,
141141
stream,
142142
)?;
143143
}
144144
if self.input_data & PlayerAuthInputFlags::PerformBlockActions as u64 != 0 {
145145
<Vec<V::PlayerBlockActionData> as ProtoCodec>::serialize(
146-
self.player_block_actions.as_ref().unwrap(),
146+
self.player_block_actions.as_ref().ok_or(ProtoCodecError::ExpectedSome("player_block_actions"))?,
147147
stream,
148148
)?;
149149
}
150150
if self.input_data & PlayerAuthInputFlags::IsInClientPredictedVehicle as u64 != 0 {
151151
<ClientPredictedVehicleData<V> as ProtoCodec>::serialize(
152-
self.client_predicted_vehicle.as_ref().unwrap(),
152+
self.client_predicted_vehicle.as_ref().ok_or(ProtoCodecError::ExpectedSome("client_predicted_vehicle"))?,
153153
stream,
154154
)?;
155155
}
@@ -239,19 +239,19 @@ impl<V: ProtoVersion> ProtoCodec for PlayerAuthInputPacket<V> {
239239
+ ProtoCodecVAR::size_hint(&self.client_tick)
240240
+ ProtoCodecLE::size_hint(&self.velocity)
241241
+ match self.input_data & PlayerAuthInputFlags::PerformItemInteraction as u64 != 0 {
242-
true => self.item_use_transaction.size_hint(),
242+
true => self.item_use_transaction.as_ref().map_or(0, ProtoCodec::size_hint),
243243
false => 0,
244244
}
245245
+ match self.input_data & PlayerAuthInputFlags::PerformItemStackRequest as u64 != 0 {
246-
true => self.item_stack_request.size_hint(),
246+
true => self.item_stack_request.as_ref().map_or(0, ProtoCodec::size_hint),
247247
false => 0,
248248
}
249249
+ match self.input_data & PlayerAuthInputFlags::PerformBlockActions as u64 != 0 {
250-
true => self.player_block_actions.size_hint(),
250+
true => self.player_block_actions.as_ref().map_or(0, ProtoCodec::size_hint),
251251
false => 0,
252252
}
253253
+ match self.input_data & PlayerAuthInputFlags::IsInClientPredictedVehicle as u64 != 0 {
254-
true => self.client_predicted_vehicle.size_hint(),
254+
true => self.client_predicted_vehicle.as_ref().map_or(0, ProtoCodec::size_hint),
255255
false => 0,
256256
}
257257
+ ProtoCodecLE::size_hint(&self.analog_move_vector)

crates/protocol/src/version/v662/packets/sub_chunk.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ impl<V: ProtoVersion> ProtoCodec for SubChunkPacket<V> {
6060
if i.sub_chunk_request_result != SubChunkRequestResult::SuccessAllAir
6161
|| !self.cache_enabled
6262
{
63-
i.serialized_sub_chunk.as_ref().unwrap().serialize(stream)?;
63+
i.serialized_sub_chunk.as_ref().ok_or(ProtoCodecError::ExpectedSome("serialized_sub_chunk"))?.serialize(stream)?;
6464
}
6565
i.height_map_data_type.serialize(stream)?;
6666
if i.height_map_data_type == HeightMapDataType::HasData {
67-
let height_map = i.height_map_data.as_ref().unwrap();
67+
let height_map = i.height_map_data.as_ref().ok_or(ProtoCodecError::ExpectedSome("height_map_data"))?;
6868
for x in height_map {
6969
for y in x {
7070
y.serialize(stream)?;
7171
}
7272
}
7373
}
7474
if self.cache_enabled {
75-
<u64 as ProtoCodecLE>::serialize(i.blob_id.as_ref().unwrap(), stream)?;
75+
<u64 as ProtoCodecLE>::serialize(i.blob_id.as_ref().ok_or(ProtoCodecError::ExpectedSome("blob_id"))?, stream)?;
7676
}
7777
}
7878

@@ -150,14 +150,13 @@ impl<V: ProtoVersion> ProtoCodec for SubChunkPacket<V> {
150150
+ match i.sub_chunk_request_result != SubChunkRequestResult::SuccessAllAir
151151
|| !self.cache_enabled
152152
{
153-
true => i.serialized_sub_chunk.as_ref().unwrap().size_hint(),
153+
true => i.serialized_sub_chunk.as_ref().map_or(0, ProtoCodec::size_hint),
154154
false => 0,
155155
}
156156
+ i.height_map_data_type.size_hint()
157157
+ match i.height_map_data_type == HeightMapDataType::HasData {
158158
true => {
159-
let height_map = i.height_map_data.as_ref().unwrap();
160-
height_map.len() * height_map[0].len() * size_of::<i8>()
159+
i.height_map_data.as_ref().map_or(0, |height_map| height_map.len() * height_map[0].len() * size_of::<i8>())
161160
}
162161
false => 0,
163162
}

crates/protocol/src/version/v662/types/network_item_instance_descriptor.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ impl ProtoCodec for NetworkItemInstanceDescriptor {
1818
match &self.id {
1919
0 => {}
2020
_ => {
21-
ProtoCodecLE::serialize(self.stack_size.as_ref().unwrap(), stream)?;
22-
ProtoCodecVAR::serialize(self.aux_value.as_ref().unwrap(), stream)?;
23-
ProtoCodecVAR::serialize(self.block_runtime_id.as_ref().unwrap(), stream)?;
24-
ProtoCodec::serialize(self.user_data_buffer.as_ref().unwrap(), stream)?;
21+
ProtoCodecLE::serialize(self.stack_size.as_ref().ok_or(ProtoCodecError::ExpectedSome("stack_size"))?, stream)?;
22+
ProtoCodecVAR::serialize(self.aux_value.as_ref().ok_or(ProtoCodecError::ExpectedSome("aux_value"))?, stream)?;
23+
ProtoCodecVAR::serialize(self.block_runtime_id.as_ref().ok_or(ProtoCodecError::ExpectedSome("block_runtime_id"))?, stream)?;
24+
ProtoCodec::serialize(self.user_data_buffer.as_ref().ok_or(ProtoCodecError::ExpectedSome("user_data_buffer"))?, stream)?;
2525
}
2626
}
2727

@@ -62,10 +62,10 @@ impl ProtoCodec for NetworkItemInstanceDescriptor {
6262
+ match self.id {
6363
0 => 0,
6464
_ => {
65-
ProtoCodecLE::size_hint(self.stack_size.as_ref().unwrap())
66-
+ ProtoCodecVAR::size_hint(self.aux_value.as_ref().unwrap())
67-
+ ProtoCodecVAR::size_hint(self.block_runtime_id.as_ref().unwrap())
68-
+ ProtoCodec::size_hint(self.user_data_buffer.as_ref().unwrap())
65+
self.stack_size.as_ref().map_or(0, ProtoCodecLE::size_hint)
66+
+ self.aux_value.as_ref().map_or(0, ProtoCodecVAR::size_hint)
67+
+ self.block_runtime_id.as_ref().map_or(0, ProtoCodecVAR::size_hint)
68+
+ self.user_data_buffer.as_ref().map_or(0, ProtoCodec::size_hint)
6969
}
7070
}
7171
}

crates/protocol/src/version/v662/types/network_item_stack_descriptor.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ impl ProtoCodec for NetworkItemStackDescriptor {
1919
match &self.id {
2020
0 => {}
2121
_ => {
22-
ProtoCodecLE::serialize(self.stack_size.as_ref().unwrap(), stream)?;
23-
ProtoCodecVAR::serialize(self.aux_value.as_ref().unwrap(), stream)?;
22+
ProtoCodecLE::serialize(self.stack_size.as_ref().ok_or(ProtoCodecError::ExpectedSome("stack_size"))?, stream)?;
23+
ProtoCodecVAR::serialize(self.aux_value.as_ref().ok_or(ProtoCodecError::ExpectedSome("aux_value"))?, stream)?;
2424
<Option<i32> as ProtoCodecVAR>::serialize(
25-
self.net_id_variant.as_ref().unwrap(),
25+
self.net_id_variant.as_ref().ok_or(ProtoCodecError::ExpectedSome("net_id_variant"))?,
2626
stream,
2727
)?;
28-
ProtoCodecVAR::serialize(self.block_runtime_id.as_ref().unwrap(), stream)?;
29-
String::serialize(self.user_data_buffer.as_ref().unwrap(), stream)?;
28+
ProtoCodecVAR::serialize(self.block_runtime_id.as_ref().ok_or(ProtoCodecError::ExpectedSome("block_runtime_id"))?, stream)?;
29+
String::serialize(self.user_data_buffer.as_ref().ok_or(ProtoCodecError::ExpectedSome("user_data_buffer"))?, stream)?;
3030
}
3131
}
3232

@@ -70,13 +70,11 @@ impl ProtoCodec for NetworkItemStackDescriptor {
7070
+ match &self.id {
7171
0 => 0,
7272
_ => {
73-
ProtoCodecLE::size_hint(self.stack_size.as_ref().unwrap())
74-
+ ProtoCodecVAR::size_hint(self.aux_value.as_ref().unwrap())
75-
+ <Option<i32> as ProtoCodecVAR>::size_hint(
76-
self.net_id_variant.as_ref().unwrap(),
77-
)
78-
+ ProtoCodecVAR::size_hint(self.block_runtime_id.as_ref().unwrap())
79-
+ String::size_hint(self.user_data_buffer.as_ref().unwrap())
73+
self.stack_size.as_ref().map_or(0, ProtoCodecLE::size_hint)
74+
+ self.aux_value.as_ref().map_or(0, ProtoCodecVAR::size_hint)
75+
+ self.net_id_variant.as_ref().map_or(0, ProtoCodecVAR::size_hint)
76+
+ self.block_runtime_id.as_ref().map_or(0, ProtoCodecVAR::size_hint)
77+
+ self.user_data_buffer.as_ref().map_or(0, ProtoCodec::size_hint)
8078
}
8179
}
8280
}

crates/protocol/src/version/v662/types/packed_item_use_legacy_inventory_transaction.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<V: ProtoVersion> ProtoCodec for PackedItemUseLegacyInventoryTransaction<V>
3333
match &self.id {
3434
0 => {}
3535
_ => {
36-
let vec = self.container_slots.as_ref().unwrap();
36+
let vec = self.container_slots.as_ref().ok_or(ProtoCodecError::ExpectedSome("container_slots"))?;
3737
let len: u32 = vec.len().try_into()?;
3838
ProtoCodecVAR::serialize(&len, stream)?;
3939
for i in vec {
@@ -98,8 +98,7 @@ impl<V: ProtoVersion> ProtoCodec for PackedItemUseLegacyInventoryTransaction<V>
9898
+ match &self.id {
9999
0 => 0,
100100
_ => {
101-
let vec = self.container_slots.as_ref().unwrap();
102-
vec.len() + vec.iter().map(|i| i.size_hint()).sum::<usize>()
101+
self.container_slots.as_ref().map_or(0, |vec| vec.len() + vec.iter().map(|i| i.size_hint()).sum::<usize>())
103102
}
104103
}
105104
+ self.action.size_hint()

crates/protocol/src/version/v712/types/packed_item_use_legacy_inventory_transaction.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<V: ProtoVersion> ProtoCodec for PackedItemUseLegacyInventoryTransaction<V>
5454
match &self.id {
5555
0 => {}
5656
_ => {
57-
let vec = self.container_slots.as_ref().unwrap();
57+
let vec = self.container_slots.as_ref().ok_or(ProtoCodecError::ExpectedSome("container_slots"))?;
5858
let len: u32 = vec.len().try_into()?;
5959
ProtoCodecVAR::serialize(&len, stream)?;
6060
for i in vec {
@@ -125,8 +125,7 @@ impl<V: ProtoVersion> ProtoCodec for PackedItemUseLegacyInventoryTransaction<V>
125125
+ match &self.id {
126126
0 => 0,
127127
_ => {
128-
let vec = self.container_slots.as_ref().unwrap();
129-
vec.len() + vec.iter().map(|i| i.size_hint()).sum::<usize>()
128+
self.container_slots.as_ref().map_or(0, |vec| vec.len() + vec.iter().map(|i| i.size_hint()).sum::<usize>())
130129
}
131130
}
132131
+ self.action.size_hint()

crates/protocol/src/version/v748/packets/player_auth_input.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,25 @@ impl<V: ProtoVersion> ProtoCodec for PlayerAuthInputPacket<V> {
137137
<(f32, f32, f32) as ProtoCodecLE>::serialize(&self.velocity, stream)?;
138138
if self.input_data & PlayerAuthInputFlags::PerformItemInteraction as u64 != 0 {
139139
<V::PackedItemUseLegacyInventoryTransaction as ProtoCodec>::serialize(
140-
self.item_use_transaction.as_ref().unwrap(),
140+
self.item_use_transaction.as_ref().ok_or(ProtoCodecError::ExpectedSome("item_use_transaction"))?,
141141
stream,
142142
)?;
143143
}
144144
if self.input_data & PlayerAuthInputFlags::PerformItemStackRequest as u64 != 0 {
145145
<PerformItemStackRequestData<V> as ProtoCodec>::serialize(
146-
self.item_stack_request.as_ref().unwrap(),
146+
self.item_stack_request.as_ref().ok_or(ProtoCodecError::ExpectedSome("item_stack_request"))?,
147147
stream,
148148
)?;
149149
}
150150
if self.input_data & PlayerAuthInputFlags::PerformBlockActions as u64 != 0 {
151151
<Vec<V::PlayerBlockActionData> as ProtoCodec>::serialize(
152-
self.player_block_actions.as_ref().unwrap(),
152+
self.player_block_actions.as_ref().ok_or(ProtoCodecError::ExpectedSome("player_block_actions"))?,
153153
stream,
154154
)?;
155155
}
156156
if self.input_data & PlayerAuthInputFlags::IsInClientPredictedVehicle as u64 != 0 {
157157
<ClientPredictedVehicleData<V> as ProtoCodec>::serialize(
158-
self.client_predicted_vehicle.as_ref().unwrap(),
158+
self.client_predicted_vehicle.as_ref().ok_or(ProtoCodecError::ExpectedSome("client_predicted_vehicle"))?,
159159
stream,
160160
)?;
161161
}
@@ -242,19 +242,19 @@ impl<V: ProtoVersion> ProtoCodec for PlayerAuthInputPacket<V> {
242242
+ ProtoCodecVAR::size_hint(&self.client_tick)
243243
+ ProtoCodecLE::size_hint(&self.velocity)
244244
+ match self.input_data & PlayerAuthInputFlags::PerformItemInteraction as u64 != 0 {
245-
true => self.item_use_transaction.size_hint(),
245+
true => self.item_use_transaction.as_ref().map_or(0, ProtoCodec::size_hint),
246246
false => 0,
247247
}
248248
+ match self.input_data & PlayerAuthInputFlags::PerformItemStackRequest as u64 != 0 {
249-
true => self.item_stack_request.size_hint(),
249+
true => self.item_stack_request.as_ref().map_or(0, ProtoCodec::size_hint),
250250
false => 0,
251251
}
252252
+ match self.input_data & PlayerAuthInputFlags::PerformBlockActions as u64 != 0 {
253-
true => self.player_block_actions.size_hint(),
253+
true => self.player_block_actions.as_ref().map_or(0, ProtoCodec::size_hint),
254254
false => 0,
255255
}
256256
+ match self.input_data & PlayerAuthInputFlags::IsInClientPredictedVehicle as u64 != 0 {
257-
true => self.client_predicted_vehicle.size_hint(),
257+
true => self.client_predicted_vehicle.as_ref().map_or(0, ProtoCodec::size_hint),
258258
false => 0,
259259
}
260260
+ ProtoCodecLE::size_hint(&self.analog_move_vector)

0 commit comments

Comments
 (0)