This repository was archived by the owner on Sep 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypes.rs
More file actions
180 lines (142 loc) · 4.2 KB
/
types.rs
File metadata and controls
180 lines (142 loc) · 4.2 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
179
180
//! Extension traits for `Bytes` and `BytesMut` which support Minecraft types.
use bytes::Buf;
use bytes::{Bytes, BytesMut};
use nbt::Blob;
use thiserror::Error;
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BlockPosition {
pub x: i32,
pub y: i32,
pub z: i32,
}
#[derive(Debug, Clone)]
pub struct Node; // TODO
#[derive(Debug, Clone)]
pub struct Slot;
#[derive(Debug, Error)]
pub enum Error {
#[error("insufficient bytes left in buffer")]
NotEnoughBytes,
}
pub type Result<T> = std::result::Result<T, Error>;
pub trait BytesExt {
fn try_get_var_int(&mut self) -> Result<i32>;
fn try_get_string(&mut self) -> Result<String>;
fn try_get_bool(&mut self) -> Result<bool>;
fn try_get_position(&mut self) -> Result<BlockPosition>;
fn try_get_node(&mut self) -> Result<Node>;
fn try_get_uuid(&mut self) -> Result<Uuid>;
fn try_get_nbt(&mut self) -> Result<nbt::Blob>;
fn try_get_slot(&mut self) -> Result<Slot>;
fn try_get_i8(&mut self) -> Result<i8>;
fn try_get_i16(&mut self) -> Result<i16>;
fn try_get_i32(&mut self) -> Result<i32>;
fn try_get_i64(&mut self) -> Result<i64>;
fn try_get_f32(&mut self) -> Result<f32>;
fn try_get_f64(&mut self) -> Result<f64>;
fn try_get_u8(&mut self) -> Result<u8>;
fn try_get_u16(&mut self) -> Result<u16>;
fn try_get_u32(&mut self) -> Result<u32>;
fn try_get_u64(&mut self) -> Result<u64>;
}
pub trait BytesMutExt {
fn put_var_int(&mut self, x: i32);
fn put_string(&mut self, x: impl AsRef<str>);
fn put_bool(&mut self, x: bool);
fn put_position(&mut self, x: BlockPosition);
fn put_node(&mut self, x: Node);
fn put_uuid(&mut self, x: Uuid);
fn put_nbt(&mut self, x: nbt::Blob);
fn put_slot(&mut self, x: Slot);
}
macro_rules! try_get_impl {
($this:ident, $size:expr, $method:ident) => {{
if $this.remaining() < $size {
return Err(Error::NotEnoughBytes);
}
return Ok($this.$method());
}};
}
impl BytesExt for Bytes {
fn try_get_var_int(&mut self) -> Result<i32> {
unimplemented!()
}
fn try_get_string(&mut self) -> Result<String> {
unimplemented!()
}
fn try_get_bool(&mut self) -> Result<bool> {
unimplemented!()
}
fn try_get_position(&mut self) -> Result<BlockPosition> {
unimplemented!()
}
fn try_get_node(&mut self) -> Result<Node> {
unimplemented!()
}
fn try_get_uuid(&mut self) -> Result<Uuid> {
unimplemented!()
}
fn try_get_nbt(&mut self) -> Result<Blob> {
unimplemented!()
}
fn try_get_slot(&mut self) -> Result<Slot> {
unimplemented!()
}
fn try_get_i8(&mut self) -> Result<i8> {
try_get_impl!(self, 1, get_i8);
}
fn try_get_i16(&mut self) -> Result<i16> {
try_get_impl!(self, 2, get_i16);
}
fn try_get_i32(&mut self) -> Result<i32> {
try_get_impl!(self, 4, get_i32);
}
fn try_get_i64(&mut self) -> Result<i64> {
try_get_impl!(self, 8, get_i64);
}
fn try_get_f32(&mut self) -> Result<f32> {
try_get_impl!(self, 4, get_f32);
}
fn try_get_f64(&mut self) -> Result<f64> {
try_get_impl!(self, 8, get_f64);
}
fn try_get_u8(&mut self) -> Result<u8> {
try_get_impl!(self, 1, get_u8);
}
fn try_get_u16(&mut self) -> Result<u16> {
try_get_impl!(self, 2, get_u16);
}
fn try_get_u32(&mut self) -> Result<u32> {
try_get_impl!(self, 4, get_u32);
}
fn try_get_u64(&mut self) -> Result<u64> {
try_get_impl!(self, 8, get_u64);
}
}
impl BytesMutExt for BytesMut {
fn put_var_int(&mut self, _x: i32) {
unimplemented!()
}
fn put_string(&mut self, _x: impl AsRef<str>) {
unimplemented!()
}
fn put_bool(&mut self, _x: bool) {
unimplemented!()
}
fn put_position(&mut self, x: BlockPosition) {
unimplemented!()
}
fn put_node(&mut self, x: Node) {
unimplemented!()
}
fn put_uuid(&mut self, x: Uuid) {
unimplemented!()
}
fn put_nbt(&mut self, x: Blob) {
unimplemented!()
}
fn put_slot(&mut self, x: Slot) {
unimplemented!()
}
}